Skip to content

Instantly share code, notes, and snippets.

@kirs
Created March 27, 2017 14:51
Show Gist options
  • Save kirs/fab79c476eb1f997cb18c29069b4b24d to your computer and use it in GitHub Desktop.
Save kirs/fab79c476eb1f997cb18c29069b4b24d to your computer and use it in GitHub Desktop.
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