Skip to content

Instantly share code, notes, and snippets.

View JoshMcKin's full-sized avatar

Joshua T. Mckinney JoshMcKin

  • New Braunfels, TX
View GitHub Profile
@JoshMcKin
JoshMcKin / stack_queue.rb
Created February 5, 2014 21:11
Stack Queue; Last in first out Queue
require 'thread'
# Borrowed from https://github.com/ruby/ruby/blob/ruby_1_9_3/lib/thread.rb#L140
# and modified for a last in first out processing
#
#
class StackQueue < Queue
# Last in first out Queue
#
# Retrieves data from the queue. If the queue is empty, the calling thread is
# suspended until data is pushed onto the queue. If +non_block+ is true, the
@JoshMcKin
JoshMcKin / gist:2663599
Created May 12, 2012 01:32
AWS Core SessionSigner Fiber Mutex
require 'em-synchrony/thread'
module AWS
module Core
class SessionSigner
@create_mutex = EM::Synchrony::Thread::Mutex.new
end
end
end
@JoshMcKin
JoshMcKin / gist:1648242
Created January 20, 2012 16:25
Recurring Delayed Job 2.1 in Rails 3 without patching
# Tested on DelayedJob 2.1
class MyRecurringDelayedJob
def perform
# ...some slow code
end
def success(job)
MyRecurringDelayedJob.schedule_job(job)
end
@JoshMcKin
JoshMcKin / gist:1058838
Created July 1, 2011 16:05
NoMethodError on class
class FoosController < ApplicationController
# GET /foos
# GET /foos.xml
def index
@foos = Foo.all
Foo.bad_method #should raise NoMethodError
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @foos }
end
@JoshMcKin
JoshMcKin / gist:1058782
Created July 1, 2011 15:32
EM-mysql2 dead fiber from Rails exception
/Users/joshmckin/test_mysq_gem/config/initializers/em_mysql2_adapter.rb:16:in `resume': dead fiber called (FiberError)
from /Users/joshmckin/test_mysq_gem/config/initializers/em_mysql2_adapter.rb:16:in `block in query'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `call'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/eventmachine-0.12.10/lib/em/deferrable.rb:141:in `set_deferred_status'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/eventmachine-0.12.10/lib/em/deferrable.rb:180:in `fail'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.11/lib/active_record/connection_adapters/em_mysql2_adapter.rb:38:in `rescue in notify_readable'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.11/lib/active_record/connection_adapters/em_mysql2_adapter.rb:33:in `notify_readable'
from /Users/joshmckin/.rvm/gems/ruby-1.9.2-p180/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /Users/joshmckin/.rvm/gem
@JoshMcKin
JoshMcKin / gist:1058768
Created July 1, 2011 15:24
EM-mysql2 dead fiber from Rails exception patch
module Mysql2
module Fibered
class Client < ::Mysql2::Client
def query(sql, opts={})
if ::EM.reactor_running?
super(sql, opts.merge(:async => true))
deferrable = ::EM::DefaultDeferrable.new
::EM.watch(self.socket, Watcher, self, deferrable).notify_readable = true
fiber = Fiber.current
deferrable.callback do |result|
@JoshMcKin
JoshMcKin / gist:1012605
Created June 7, 2011 16:30
Queued AWS Queue writes
class QueuedMessage < Aws::Sqs
def queued_messages
@queued_messages ||= []
@queued_messages
end
def initialize(aws_access_key_id = AWS_ACCESS_KEY_ID, aws_secret_access_key = AWS_SECRET_ACCESS_KEY, params = {})
super(aws_access_key_id, aws_secret_access_key,params)
end
@JoshMcKin
JoshMcKin / gist:983763
Created May 20, 2011 20:47
Rake 0.9.0 Gem build fail
rake aborted!
undefined method `desc' for #<Bundler::GemHelper:0x00000101d83ab8>
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.10/lib/bundler/gem_helper.rb:24:in `install'
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.10/lib/bundler/gem_helper.rb:9:in `install_tasks'
/Users/joshmckin/Ventanex/vtx_operations/Rakefile:21:in `<top (required)>'
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.0/lib/rake/rake_module.rb:25:in `load'
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.0/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.0/lib/rake/application.rb:495:in `raw_load_rakefile'
/Users/joshmckin/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.0/lib/rake/application.rb:78:in `block in load_rakefile'
@JoshMcKin
JoshMcKin / gist:981561
Created May 19, 2011 19:43
Guess the number of each of the supplied values necessary to get the desired total
def self.guessing_game(values=[2.5,10.95,55.25],multi_sum=4,total_goal=79.65)
results = nil
total_amount = 0.0
length = values.length
((multi_sum ** length) / (multi_sum)).times do |i|
total = 0
possible = []
total_amount = 0.0.to_d # use decimal for accuracy
in the layout file
<% cache("header_cache_#{session_user.id}") do %>
some dynamic navigation and large select box
<%end%>
in the controller
expire_fragment("header_cache_#{session_user.id}")