Skip to content

Instantly share code, notes, and snippets.

@slayer
Forked from morhekil/gist:179070
Created September 2, 2009 08:35
Show Gist options
  • Save slayer/179611 to your computer and use it in GitHub Desktop.
Save slayer/179611 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require File.dirname(__FILE__) + "/../../config/environment"
$running = true
$working = false
Signal.trap("TERM") do
$stderr.puts "#{Time.now.to_s(:db)}\tGot TERM, exiting..."
$running = false
if $working
$stderr.puts "#{Time.now.to_s(:db)}\tWaiting for the worker to finish..." if $working
else
exit(0)
end
end
Signal.trap("INT") do
$stderr.puts "#{Time.now.to_s(:db)}\tGot INT, exiting right now..."
$running = false
exit(0)
end
while($running) do
begin
#
# doing something not critical and potentially time-consuming here,
# like waiting for a message to appear on the queue, etc - we can
# stop doing it at any time without problems
#
$working = true
#
# here comes the critical stuff - we don't want it to be interrupted
# and forcibly killed by signals, etc
#
rescue SystemExit => e
# we don't want to report these exceptions to Exceptional, just print them
$stderr.puts "#{Time.now.to_s(:db)}\t#{ e } (#{ e.class })!"
rescue Exception => e
# All other exceptions must be reported up to Exceptional
Exceptional.catch(e) if Exceptional.enabled?
# and printed into the error log
$stderr.puts "#{Time.now.to_s(:db)}\t#{ e } (#{ e.class })!"
$stderr.puts e.backtrace
ensure
# reset the working flag and wait a bit
$working = false
sleep 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment