Skip to content

Instantly share code, notes, and snippets.

@betamatt
betamatt / gist:151370
Created July 21, 2009 14:55
Example of a future proof migration
# A migration that will continue to work after refactoring
class FutureProofMigration < ActiveRecord::Migration
# Redefine models used in the migration as empty inner classes. This removes all validations and magic.
class Model < ActiveRecord::Base; end
# Either don't rely on associations or declare them as referring to the new inner class
class DependentModel < ActiveRecord::Base
belongs_to :model, :class_name => "FutureProofMigration::Model"
end
@betamatt
betamatt / Example of a compressed file cache store
Created February 21, 2010 04:07
Example of a compressed file-based cache store
ActiveSupport::Cache::FileStore.class_eval do
def read(name, options = nil)
super
File.open(real_file_path(name), 'rb') do |f|
gz = Zlib::GzipReader.new(f)
value = Marshal.load(gz)
gz.close
value
end
rescue => e
@betamatt
betamatt / mem_cache_wrapper.rb
Created April 20, 2010 14:32
A wrapper for memcache-client that swallows cache errors
class MemCacheWrapper
def initialize(repository)
@repository = repository
end
def self.handle_errors_for(*args)
options = args.extract_options!
args.each do |method|
define_method method do |*x|
@betamatt
betamatt / cash_money_extensions.rb
Created April 20, 2010 14:41
Handle cache inconsistency by clearing affected models. Allow use of counter caches.
# include Cash::Extensions after you declare is_cached for a class
module Cash
module Extensions
def self.included(base)
base.send(:extend, ClassMethods)
base.send(:include, InstanceMethods)
end
module ClassMethods
# Iterate a list
a = [5,6,7,8]
a.each_with_index{ |x, i| puts "#{i} => #{x}" }
# Conditionally end a loop
stop = false
while(!stop) do
value = rand(5)
puts "Picked #{value}"
stop = true if 0 == value
@betamatt
betamatt / benchmarks_multi.rb
Created December 1, 2010 19:48 — forked from bkeepers/benchmarks_multi.rb
Mult-worker DJ benchmark
require 'spec/spec_helper'
require 'logger'
require 'benchmark'
workers = 20
puts "#{workers} workers"
#Delayed::Job.logger = Logger.new('/tmp/delayed_job.log')
pids = []
@betamatt
betamatt / gist:739373
Created December 13, 2010 18:31
Testing Struct GC
def foo; 10000.times{x = Struct.new(:field).new("1")}; end
GC.enable_stats
GC.start
ObjectSpace.live_objects
=> 174939
foo
GC.start
ObjectSpace.live_objects
=> 174947
@betamatt
betamatt / faye.rb
Created April 10, 2011 20:23
Faye monit wrapper
#!/usr/env ruby
base_dir = File.expand_path("../..", __FILE__)
NAME="faye"
PID="#{base_dir}/tmp/pids/#{NAME}.pid"
COMMAND="bundle exec rackup -s thin -E production -p 3001 faye.ru"
case ARGV[0]
when "start"
@betamatt
betamatt / delayed_mailer.rb
Created July 11, 2011 14:25
Delay Rails 2.x mailers
# Handle mail delivery in the background. Only the minimum amount of data should go in the job not
# the fully rendered email content.
# I haven't tested this but some variant should work.
ActionMailer::Base.class_eval do
def self.create_and_deliver!(message, *parameters)
new(message, *parameters).deliver!
end
def method_missing_with_delay(method_symbol, *parameters)#:nodoc:
case method_symbol.id2name
@betamatt
betamatt / Procfile
Created January 26, 2012 23:29
Running delayed_job with foreman
queue: ./queue.sh