Created
March 27, 2017 14:51
-
-
Save kirs/fab79c476eb1f997cb18c29069b4b24d 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
class Runner | |
def initialize | |
@count = 0 | |
end | |
EXIT_AFTER = 5 | |
def perform | |
loop do | |
puts "Running #{@count}, shutdown: #{@shutdown.inspect}" | |
@count += 1 | |
sleep 1 | |
end | |
end | |
def register_signal_handlers | |
trap('TERM') { shutdown } | |
end | |
def shutdown | |
puts "Exiting with TERM in #{EXIT_AFTER} seconds..." | |
@shutdown = true | |
Thread.new do | |
sleep EXIT_AFTER | |
puts "Time to shutdown!" | |
exit 1 | |
end | |
end | |
end | |
$stdout.sync = true | |
runner = Runner.new | |
runner.register_signal_handlers | |
runner.perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment