Skip to content

Instantly share code, notes, and snippets.

@jpayne
Created September 8, 2012 17:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jpayne/3677675 to your computer and use it in GitHub Desktop.
Save jpayne/3677675 to your computer and use it in GitHub Desktop.
Start multiple Resque workers with Upstart
Thanks to Jason Roelofs for his article at http://jasonroelofs.com/2012/03/12/manage-and-monitor-resque-with-upstart-and-monit/ which got me most of the way there.
description "Start a Resque worker by supplying an ID. For example: start resque ID=1. See also: resque-workers.conf"
respawn
respawn limit 5 20
instance $ID
script
APP_PATH=/apps/my_app/current
PIDFILE=$APP_PATH/tmp/pids/resque-$ID.pid
LOGFILE=$APP_PATH/log/resque_workers.log
su -c "cd $APP_PATH; bundle exec rake resque:work QUEUE=* PIDFILE=$PIDFILE RAILS_ENV=production >> $LOGFILE 2>&1" - my_user
end script
description "Start multiple Resque workers. Change NUM_WORKERS based on your needs."
start on (local-filesystems and net-device-up IFACE=eth0)
task
env NUM_WORKERS=5
script
for i in `seq 1 $NUM_WORKERS`
do
start resque-worker ID=$i
done
end script
@kesor
Copy link

kesor commented Oct 23, 2012

How would you restart all these workers?

@jpayne
Copy link
Author

jpayne commented Dec 10, 2012

Sorry I didn't see your comment sooner, kesor. To restart the workers gracefully, I send a USR signal to the Resque workers via a Rake task, and then let Upstart bring them back up. Here's an example of the Rake task:

task :restart_workers => :environment do
  pids = Array.new
  Resque.workers.each do |worker|
    pids << worker.to_s.split(/:/).second
  end
  if pids.size > 0
    system("kill -QUIT #{pids.join(' ')}")
  end
end

@jpayne
Copy link
Author

jpayne commented Dec 10, 2012

Correction: QUIT is the signal for a graceful shutdown. USR1 immediately kills the child.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment