Skip to content

Instantly share code, notes, and snippets.

@mattyoho
Forked from ryanbriones/gist:282988
Created January 21, 2010 18:56
Show Gist options
  • Save mattyoho/283071 to your computer and use it in GitHub Desktop.
Save mattyoho/283071 to your computer and use it in GitHub Desktop.
namespace :resque do
desc "Process exactly one job and quit"
task :work_one => :setup do
require 'resque'
worker = nil
queues = (ENV['QUEUES'] || ENV['QUEUE']).to_s.split(',')
begin
worker = Resque::Worker.new(*queues)
worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
worker.very_verbose = ENV['VVERBOSE']
rescue Resque::NoQueueError
abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work"
end
interval = ENV['INTERVAL'] || 5
puts "*** Starting worker #{worker}"
$0 = "resque: Starting"
worker.startup
loop do
break if worker.instance_eval {@shutdown}
if job = worker.reserve
puts "got: #{job.inspect}"
procline = "resque: Processing #{job.queue} since #{Time.now.to_i}"
$0 = procline
worker.log! procline
worker.process(job)
puts "finished: #{job.inspect}"
break
else
break if interval.to_i == 0
puts "Sleeping for #{interval.to_i}; waiting for #{worker.queues.join(',')}"
$0 = "resque: Waiting for #{worker.queues.join(',')}"
sleep interval.to_i
end
end
worker.unregister_worker
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment