Last active
December 16, 2015 14:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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