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

Automatic pull, merge and push for git feature branches

We use this script to automatically merge all features into staging and re-merge those features with deploy-ed code. It will stop when there is a merge conflict -> resolve and start it again.

Automatic –>
often -> smaller conflicts.
no thinking/typing -> less mistakes/headaches.

Usage
e.g. store it in scripts folder or turn it into a rake task

scripts/push_and_merge_all
scripts/push_and_merge_all stage # to also deploy everything to staging

Code

#!/usr/bin/env ruby
require 'rubygems'
require 'rake'

sh "git status | grep 'nothing to commit' && echo 'you are clean''" # ensure we are not dirty
current_branch = `git branch | grep '*'`.split.last
sh "git fetch origin" # get up-to-date info about what needs pulling

def merge(branch, options)
  sh "git checkout #{branch}"
  push_and_pull
  options[:with].each do |merged_branch|
    sh "git checkout #{merged_branch}"
    push_and_pull
    sh "git checkout #{branch} && git merge #{merged_branch}"
  end
  push_and_pull
end

def push_and_pull
  status= `git status 2> /dev/null`
  remote_pattern = /# Your branch is (.*?) /
  diverge_pattern = /# Your branch and (.*) have diverged/
  if status =~ remote_pattern
    if $1 == 'ahead'
      sh "git push"
    else
      sh "git pull"
    end
  elsif status =~ diverge_pattern
    sh "git pull && git push"
  end
end

to_stage = ['master', 'feature_a', 'feature_b']
to_stage.each{|branch| merge(branch, :with => ['deploy']) }
merge('staging', :with => to_stage)

sh "cap deploy" if ARGV[0] == 'stage'
sh "git checkout #{current_branch}"