Resque tasks, worker, web, GenericJob setup for Rails

Usage

resque-web
QUEUE='*' COUNT=3 rake resque:workers &

GenericJob.publish(Product, :reindex_all, :args=> [{:limit=>1000}]

Setup

#append to Rakefile
require 'resque/tasks'
namespace :resque do
  task :setup => :environment
end

class GenericJob
  @queue = :generic

  def self.perform(klass, method, options={})
    options = options.with_indifferent_access
    if options.has_key?(:args)
      klass.constantize.send(method, *options[:args])
    else
      klass.constantize.send(method)
    end
  end

  def self.publish(klass, method, options={})
    Resque.enqueue(self, klass.to_s, method.to_s, options)
  end
end


If you plan to process instances of AR with this setup, be sure to add serialize_ar / deserialize_ar from solving background processing with a single generic background-job

Ruby True / False Comparison / sort with <=>

Using the Spaceship operator with TrueClass / FalseClass is not possible normally:

NoMethodError: undefined method '<=>' for true:TrueClass

But we can change this…

Code

module TrueFalseComparison
  def <=>(other)
    raise ArgumentError unless [TrueClass, FalseClass].include?(other.class)
    other ? (self ? 0 : -1) : (self ? 1 : 0)
  end
end

TrueClass.send(:include, TrueFalseComparison)
FalseClass.send(:include, TrueFalseComparison)

Usage

(true <=> false) == 1
(false <=> true) == -1
[true,false,true].sort == [false,true,true]
[true,'x'].sort -> ArgumentError

Add PATH= to beginning of a users crontab

Puts the correct PATH into a users crontab, so you no longer have to use absolute paths inside cron.
We executed it on all servers via puppet / console.

sudo su root
echo 'PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin' > /tmp/cron
crontab -l -u deploy  | grep -v PATH= >> /tmp/cron
cat /tmp/cron | crontab - -u deploy

Open-uri without ssl / https verification

Usage

require 'open-uri'
require 'openssl'
OpenURI.without_ssl_verification do
  open('https://foo').read
end

Code

module OpenURI
  def self.without_ssl_verification
    old = ::OpenSSL::SSL::VERIFY_PEER
    silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE }
    yield
  ensure
    silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, old }
  end
end

If you are not in Rails you may need silence_warnings

Advanced sed regex

Some sed regex stuff I finally understood:

  • always use single quotes or you will go insane with escaping or strange errors
  • there is no \d and no +(at least on mac, on gnu its \+ (thx tobi!)), but you can use \{1,\} or repeat the pattern
    echo a1235b | sed 's/[0-9]\{1,\}//' --> ab
    echo a1235b | sed 's/[0-9][0-9]*//' --> ab
  • () and {} are escaped by default
    echo a{()}b | sed 's/{()}//' --> ab
  • escape ( and { to use them normal
    echo acccb | sed 's/\(c\{3\}\)/\1-\1/' --> accc-cccb
  • the matched string is &, matches from braces are \x
    echo acccb | sed 's/c\(c\)\(c\)/-&-\1-\2/' --> a-ccc-c-cb
  • To only print matched lines, use -n and /p
    echo 123 | sed -n 's/5/x/p' --> nothing
    echo 123 | sed -n 's/1/x/p' --> 1x3