Skip to content

Instantly share code, notes, and snippets.

@tlux
Last active December 16, 2015 14:49
Show Gist options
  • Save tlux/5451917 to your computer and use it in GitHub Desktop.
Save tlux/5451917 to your computer and use it in GitHub Desktop.
Restarting delayed_job if it has been unexpectedly stopped or crashed using "rake jobs:rescue" in production environment
namespace :jobs do
desc "Restarts delayed_job if it has been unexpectedly stopped or crashed"
task :rescue do
raise "Not in production environment" unless Rails.env.production?
pid_file = "#{Rails.root}/tmp/pids/delayed_job.pid"
restart = false
if File.exist?(pid_file)
pid = File.read(pid_file).strip.to_i
begin
Process.kill(0, pid)
rescue Errno::ESRCH # no such process
File.unlink(pid_file)
restart = true
end
puts "Delayed Job is running"
else
puts "Delayed Job is not running"
restart = true
end
run "cd #{Rails.root};RAILS_ENV=#{Rails.env} script/delayed_job start" if restart
end
end
every 12.hours do
rake "jobs:rescue"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment