Custom response for weird http verbs like PROPFIND / PURGE / OPTIONS etc

Getting lots of strange errors about weird http verbs being used, this rack middleware will help 🙂

Setup

config.middleware.use 'HttpVerbResponder',
  'PROPFIND' => [404, {}, 'Not supported'],
  'PURGE' => [404, {}, 'Not supported'], # e.g. varnish
  'OPTIONS' => [200, {"Access-Control-Allow-Origin" => "*", "Access-Control-Max-Age" => '1000'},'OK"] # CORS

Code

class HttpVerbResponder
  def initialize(app, options={})
    @app = app
    @options = options
  end

  def call(env, options={})
    if response = @options[env['REQUEST_METHOD']]
      response
    else
      @app.call(env)
    end
  end
end

Test

curl -X PURGE your-url.com

Block Resque Queue from processing

Stops workers from processing jobs in these queues, so you can e.g. restart indexing/mail/… servers safely

Usage

REDIS.sadd 'blocked-resque-queues', 'low'

Code

class Resque::Worker
  def queues_with_blocked
    blocked = REDIS.smembers('blocked-resque-queues') || []
    queues_without_blocked - blocked
  end
  alias_method_chain :queues, :blocked
end

Tracking Solr Replication Delay via Scout or Sheriff

We now keep track of our solr replication delay, maybe you should too 😉

Works with scout-app or sheriff(free/self-hosted)

class SolrReplication < Scout::Plugin
  needs 'open-uri'

  OPTIONS=<<-EOS
    master:
      default: http://192.168.2.114:8983
    slave:
      default: http://localhost:8765
  EOS

  def build_report
    replication_path = '/solr/admin/replication/index.jsp'
    rex = /Generation: (\d+)/
    master = open(option(:master)+replication_path).read.match(rex)[1]
    slave = open(option(:slave)+replication_path).read.match(rex)[1]
    if master and slave
      report 'delay' => master.to_i - slave.to_i
    else
      error "Incorrect values found master:#{master} slave:#{slave}"
    end
  end
end

Testing against multiple gem versions with bundler

Example setup to test a gem or plugin against rails 2 and 3:

Version 1

# Gemfile
gem 'activerecord', ENV['AR']

# Rakefile
task :default do
  sh "rspec spec"
end

task :all do
  sh "AR=2.3.14 && (bundle || bundle install) && bundle exec rake"
  sh "AR=3.0.10 && (bundle || bundle install) && bundle exec rake"
  sh "AR=3.1.1 && (bundle || bundle install) && bundle exec rake"
end

Version 2 (if 1 is not possible…)

# Gemfile
...
gem 'rails', '~>3'

# spec/rails2/Gemfile
...
gem 'rails', '~>2'

# Rakefile
task :spec do
  sh "bundle exec rspec spec"
end

task :rails2 do
  sh "cd spec/rails2 && bundle exec rspec ../../spec"
end

task :default do
  sh "rake spec && rake rails2"
end