Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'delayed_job_daemon' into guns

  • Loading branch information...
commit 3409b0d6e0d06840e9721888b12b0545d4035c19 2 parents c7bc0e5 + 980dacf
guns authored
Showing with 24 additions and 20 deletions.
  1. +24 −20 lib/delayed/daemon_tasks.rb
View
44 lib/delayed/daemon_tasks.rb
@@ -38,7 +38,7 @@
$0 = "delayed_worker.#{id}"
# reset all inherited traps from main process
- [:CLD, :HUP, :TERM, :INT, :QUIT].each { |sig| trap sig, 'DEFAULT' }
+ [:CLD, :HUP, :TERM].each { |sig| trap sig, 'DEFAULT' }
# lay quiet for a while before booting up if specified
sleep delay if delay
@@ -60,7 +60,7 @@
# simple logger; there is some overhead due to reopening the file for
# every write, but it's minor, and avoids headaches with open files
- rails_logger = lambda do |msg|
+ logger = lambda do |msg|
File.open logfile, 'a' do |f|
f.puts "#{Time.now.strftime '%FT%T%z'}: [#{$0}] #{msg}"
end
@@ -71,7 +71,7 @@
pid_file = "#{pid_dir}/#{$0}.pid"
if File.exists? pid_file
msg = "PID file #{pid_file} already exists!"
- rails_logger.call msg
+ logger.call msg
abort msg
else
# silence output like a proper daemon
@@ -83,7 +83,7 @@
# spawn the first workers
children, times_dead = {}, {}
worker_count = (ENV['WORKERS'] || 1).to_i
- rails_logger.call "Spawning #{worker_count} worker(s)"
+ logger.call "Spawning #{worker_count} worker(s)"
worker_count.times { |id| children[worker.call id, nil] = id }
# and respawn the failures
@@ -96,13 +96,13 @@
times_dead[id].reject! { |time| now - time > 60 }
if times_dead[id].size > 4
delay = 60 * 5 # time to tell the children to sleep before loading
- rails_logger.call %Q{
+ logger.call %Q{
delayed_worker.#{id} has died four times in the past minute!
Something is seriously wrong!
Restarting worker in #{delay} seconds.
}.strip.gsub /\s+/, ' '
else
- rails_logger.call "Restarting dead worker: delayed_worker.#{id}"
+ logger.call "Restarting dead worker: delayed_worker.#{id}"
end
children[worker.call id, delay] = id
@@ -110,26 +110,30 @@
# restart children on SIGHUP
trap :HUP do
- rails_logger.call 'SIGHUP received! Restarting workers.'
+ logger.call 'SIGHUP received! Restarting workers.'
Process.kill :TERM, *children.keys
end
+ # cleanup on exit
+ trap :EXIT do
+ rm_f pid_file
+ end
+
# terminate children on user termination
- [:TERM, :INT, :QUIT].each do |sig|
- trap sig do
- rails_logger.call "SIG#{sig} received! Shutting down workers."
+ trap :TERM do
+ logger.call 'SIGTERM received! Shutting down workers.'
- # reset trap handlers so we don't get caught in a trap loop
- [:CLD, sig].each { |s| trap s, 'DEFAULT' }
+ # reset trap handlers so we don't get caught in a trap loop
+ [:CLD, :HUP, :TERM].each { |s| trap s, 'DEFAULT' }
- # kill the children and reap them before terminating
- Process.kill :TERM, *children.keys
- Process.waitall
- rm_f pid_file
+ # kill the children and reap them before terminating
+ Process.kill :TERM, *children.keys
+ Process.waitall
+ logger.call 'All workers have shut down. Exiting.'
- # propagate the signal like a proper process should
- Process.kill sig, $$
- end
+ # TODO: investigate why some users are reporting that
+ # `Process.kill :TERM, $$' isn't working
+ exit
end
# NOTE: We want to block on something so that Process.waitall doesn't
@@ -168,7 +172,7 @@
pid = File.read(pid_file).to_i
puts "Sending #{signal} to #{pid}"
Process.kill signal, pid
- rescue Errno::ESRCH => e
+ rescue Errno::ESRCH => e # no such process
abort e.to_s
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.