Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created February 16, 2010 12:59
Show Gist options
  • Save simon-engledew/305503 to your computer and use it in GitHub Desktop.
Save simon-engledew/305503 to your computer and use it in GitHub Desktop.
A script to run Resque as a daemon.
#!/usr/bin/env ruby
if GC.respond_to?(:copy_on_write_friendly=); GC.copy_on_write_friendly = true; end
def pidfile
@pidfile ||= File.expand_path(File.join('..', '..', 'tmp', 'pids', 'consumer.pid'), __FILE__)
end
case ARGV.first
when 'start' then
$stdout.puts('starting consumer')
require 'rubygems'
require 'rake'
ENV['QUEUES'] ||= 'critical,source'
ENV['COUNT'] ||= '4'
ENV['RAILS_ENV'] ||= 'production'
load(File.expand_path(File.join('..', '..', 'Rakefile'), __FILE__))
Process.detach(consumer = Process.fork do
begin
r, w = IO.pipe
ENV['COUNT'].to_i.times do
Process.fork do
begin
w.close
Thread.new { r.read; exit }
Rake::Task['resque:work'].invoke
rescue Exception => e
$stdout.puts([e, *e.backtrace].join($/))
end
end
end
Process.waitall
rescue Exception => e
$stdout.puts([e, *e.backtrace].join($/))
end
end)
%x(echo "#{consumer}" > #{pidfile})
when 'stop' then
%x(kill `cat #{pidfile}`)
%x(rm #{pidfile})
else
puts 'usage: script/consumer start|stop'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment