Skip to content

Instantly share code, notes, and snippets.

@scosman
Created April 26, 2012 14:22
Show Gist options
  • Save scosman/2499922 to your computer and use it in GitHub Desktop.
Save scosman/2499922 to your computer and use it in GitHub Desktop.
Threaded delayed_job
# Threaded delayed_job. Useful for Heroku where each process/dyno costs money.
# run "rake jobs:threaded_work[4]" to start 4 worker threads
namespace :jobs do
desc "Start several delayed_job workers."
task :threaded_work, [:num_workers] => :environment do |t,args|
args.with_defaults(:num_workers => 1)
threads = []
workers = []
workersSemaphore = Mutex.new
(1..args.num_workers.to_i).each do |i|
puts "Starting worker #{i}"
threads << Thread.new() {
worker = Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY'])
worker.name_prefix = "thread_" + i.to_s + "_"
workersSemaphore.synchronize { workers << worker }
worker.start
}
puts "Worker #{i} started"
end
# wait for all the threads to start (since they also trap TERM/INT)
sleep 3
trap('TERM') { puts 'Exiting all...'; workers.each {|w| puts "exiting " + w.name; w.stop} }
trap('INT') { puts 'Exiting all...'; workers.each {|w| puts "exiting " + w.name; w.stop} }
threads.each { |aThread| aThread.join }
puts "all workers stopped"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment