Skip to content

Instantly share code, notes, and snippets.

View kares's full-sized avatar
💤

Karol Bucek kares

💤
View GitHub Profile
# Monkeypatch to disable connection pooling in ActiveRecord
module ActiveRecord
module ConnectionAdapters
class ConnectionPool
def checkout
c = ActiveRecord::Base.send(spec.adapter_method, spec.config.dup)
c.verify!
c
end
@kares
kares / response_process.rb
Last active August 29, 2015 14:04
ActionController::Live::Response partial <code>process</code> that happens on every "live" request
error = nil
# This processes the action in a child thread. It lets us return the
# response code and headers back up the rack stack, and still process
# the body in parallel with sending data to the client
Thread.new {
begin
super(name)
rescue => e
if @_response.committed?
begin
@kares
kares / serial_port_jruby.rb
Last active August 29, 2015 14:04
re-implementing "native" gem APIs (sample)
# Emulates the SerialPort class that uses a native C extension.
class SerialPort < IO
load "java/jna.jar"
load "java/purejavacomm-0.0.21.jar"
java_import 'purejavacomm.CommPortIdentifier'
def initialize(port)
# do what SerialPort C does on Windows (for compat): 0 -> 'COM1'
port = "COM#{port + 1}" if port.is_a?(Integer) && windows?
@kares
kares / jruby-profile-to-string.rb
Last active August 29, 2015 13:58 — forked from danlucraft/jruby-profile-to-string.rb
printing JRuby profiler data output (examples)
require 'jruby/profiler'
profile_data = JRuby::Profiler.profile do
# code to be profiled....
end
# print data in flat format to STDOUT :
profile_printer = JRuby::Profiler::FlatProfilePrinter.new(profile_data)
profile_printer.printProfile(STDOUT)
@kares
kares / Gemfile
Created March 7, 2014 09:36
Gemfile header for using JRuby (on Heroku) as well as MRI locally (first line to override RVM's parsing)
#ruby=1.9.3
# Gemfile header for using JRuby (on Heroku) as well as MRI locally
source 'https://rubygems.org'
if ENV["JRUBY"] || RUBY_PLATFORM == "java"
# https://devcenter.heroku.com/articles/ruby-support#ruby-versions
ruby '1.9.3', engine: 'jruby', engine_version: '1.7.10'
else
ruby '1.9.3'
end
@kares
kares / talkaholic_logger.rb
Last active August 29, 2015 13:57
Silencing a Rails.logger is (still) not thread-safe ... these initializers will try it's best to avoid your loggers being silenced (and more)! Must have for JRuby.
# put this under **#{Rails.root}/config/initializers** directory
# `silence` is like Chuck Norris you can't kill it, this biuty got yet again
# re-invented in Rails 4 right after Logger#silence got deprecated in 3.2 :)
begin
require 'active_support/logger'
require 'active_support/logger_silence'
ActiveSupport::Logger.silencer = false # silence simply yields
# if 'active_support/core_ext/logger' is loaded
@kares
kares / thread_dump.rb
Last active October 19, 2015 13:09
Thread dump-ing for JRuby (using MX)
require 'java'
module ThreadDump
def self.thread_dump(output = nil)
output ||= ''
thread_mbean = java.lang.management.ManagementFactory.getThreadMXBean
thread_ids = thread_mbean.getAllThreadIds # long[]
threads = thread_mbean.getThreadInfo(thread_ids, false, false) # ThreadInfo[]
threads.each do |thread_info|
# require 'active_record/connection_adapters/abstract_adapter'
# Adapter (connection) compatibility methods for AR 2.3 as required
# by the (AR 3.2) back-ported ConnectionPool implementation.
class ActiveRecord::ConnectionAdapters::AbstractAdapter
include MonitorMixin
# HACK: to initialize the monitor, in 3.2 #initialize calls the
# MonitorMixin#initialize by doing super() but in 2.3 it does not
# we do about the same here by overriding Adapter#new :
/**
* @version 1.20 25 Mar 1998
* @author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MousePanel extends JPanel implements MouseMotionListener {
@kares
kares / override.rb
Created November 12, 2013 17:14
#override (method) annotation for Ruby
# Allows for an #override annotation for your methods.
# Thus might help you during refactoring (method renaming) to assure methods
# expected to call a super will fail early during loading instead of runtime.
#
# Sample :
#
# class Message
# extend Override
#
# def do_send; end