Skip to content

Instantly share code, notes, and snippets.

@jimeh
Created March 26, 2010 22:42
Show Gist options
  • Save jimeh/345494 to your computer and use it in GitHub Desktop.
Save jimeh/345494 to your computer and use it in GitHub Desktop.
delayed_job.rb: Capistrano tasks to "properly" start/stop/restart the delayed_job daemon.
#
# NOTICE: The stop/restart tasks won't work properly due to a bug in the daemons gem
# unless you use the ghazel-daemons gem by putting this in your environment.rb file:
#
# config.gem "ghazel-daemons", :lib => "daemons"
# gem "ghazel-daemons"
# require "daemons"
#
# This will force-load the 'ghazel-daemons' gem and make sure it's used instead of
# the 'daemons' gem. It works even with the 'daemons' gem installed, so you won't
# have any dependency issues with gems that specifically depend upon daemons.
#
after "deploy:start", "dj:start"
after "deploy:stop", "dj:stop"
before "deploy:update_code", "dj:stop"
after "deploy:restart", "dj:restart"
# delayed_job
namespace :dj do
desc "Start delayed_job daemon."
task :start, :roles => :app do
run "if [ -d #{current_path} ]; then cd #{current_path} && sudo RAILS_ENV=#{rails_env} script/delayed_job start; fi"
end
desc "Stop delayed_job daemon."
task :stop, :roles => :app do
run "if [ -d #{current_path} ]; then cd #{current_path} && sudo RAILS_ENV=#{rails_env} script/delayed_job stop; fi"
end
desc "Restart delayed_job daemon."
task :restart, :roles => :app do
run "if [ -d #{current_path} ]; then cd #{current_path} && sudo RAILS_ENV=#{rails_env} script/delayed_job restart; fi"
end
desc "Show delayed_job daemon status."
task :status, :roles => :app do
run "if [ -d #{current_path} ]; then cd #{current_path} && sudo RAILS_ENV=#{rails_env} script/delayed_job status; fi"
end
desc "List the PIDs of all running delayed_job daemons."
task :pids, :roles => :app do
run "sudo lsof | grep '#{deploy_to}/shared/log/delayed_job.log' | cut -c 1-21 | uniq | awk '/^ruby/ {if(NR > 0){system(\"echo \" $2)}}'"
end
desc "Kill all running delayed_job daemons."
task :kill, :roles => :app do
run "sudo lsof | grep '#{deploy_to}/shared/log/delayed_job.log' | cut -c 1-21 | uniq | awk '/^ruby/ {if(NR > 0){system(\"kill -9 \" $2)}}'"
run "if [-d #{current_path} ]; then cd #{current_path} && sudo RAILS_ENV=#{rails_env} script/delayed_job stop; fi" # removes orphaned pid file(s)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment