Skip to content

Commit

Permalink
Merge branch 'delayed_job_daemon' into guns
Browse files Browse the repository at this point in the history
  • Loading branch information
guns committed Sep 22, 2010
2 parents c7bc0e5 + 980dacf commit 3409b0d
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lib/delayed/daemon_tasks.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -96,40 +96,44 @@
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
end

# 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3409b0d

Please sign in to comment.