Created
September 1, 2009 12:09
-
-
Save morhekil/179070 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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