Truck 26: Boot the Resque Scheduler from Capistrano
The Resque Scheduler gem provides cron-like features for Resque workers. Resque Scheduler has two primary components: a simple YAML file to define what jobs to run and when, and the scheduler itself, which spawns jobs according to the schedule. To launch the Resque Scheduler manually, run its rake task:
$ rake resque:scheduler 2012-05-18 15:26:04 Loading Schedule 2012-05-18 15:26:04 Scheduling poll_job 2012-05-18 15:26:04 Scheduling promote_job 2012-05-18 15:26:04 Schedules LoadedThe scheduler never exits. Press Control-C to kill the daemon or Control-Z and bg to continue the job in the background. To launch or restart the Resque Scheduler after a Capistrano deploy, add the following to deploy.rb:
require 'bundler/capistrano' after 'deploy', 'my:launch_scheduler' namespace :my do desc 'Launch the resque scheduler if it is not running.' task :launch_scheduler do rake = fetch(:rake, 'rake') rails_env = fetch(:rails_env, 'production') run "start-stop-daemon --oknodo --stop " \ "--pidfile #{current_path}/tmp/resque_scheduler.pid" run "cd '#{current_path}' && PIDFILE=#{current_path}/tmp/resque_scheduler.pid " \ "#{rake} resque:scheduler RAILS_ENV=#{rails_env} BACKGROUND=yes" end endThe command…
"PIDFILE=#{current_path}/tmp/resque_scheduler.pid " \ "#{rake} resque:scheduler RAILS_ENV=#{rails_env} BACKGROUND=yes"… boots the Resque Scheduler in the background and saves its process ID in a file. Meanwhile, the command…
start-stop-daemon --oknodo --stop \ --pidfile #{current_path}/tmp/resque_scheduler.pid… kills the process named in the file, if it’s running. Thus, after each deploy, Capistrano stops the running Resque scheduler and restarts it. Only one Resque Scheduler should run at any one time.