Last active
July 8, 2016 03:26
-
-
Save divoxx/5784686 to your computer and use it in GitHub Desktop.
This file contains 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
def stop | |
@exit = 1 | |
raise "ERROR" | |
end | |
def start | |
i = 0 | |
trap("TERM") { stop } | |
loop do | |
i += 1 | |
perform(i) | |
sleep 0.1 | |
break if @exit | |
end | |
end | |
def perform(i) | |
puts "run #{i}" | |
sleep 0.1 | |
rescue RuntimeError | |
puts "Got error" | |
end | |
pid = Process.pid | |
Thread.new do | |
sleep 1 | |
Process.kill("TERM", pid) | |
end | |
start |
This file contains 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
def stop | |
raise "ERROR" | |
end | |
def start | |
i = 0 | |
loop do | |
i += 1 | |
begin | |
trap("TERM") { stop } | |
perform(i) | |
rescue RuntimeError | |
shutdown(i) | |
break | |
ensure | |
trap("TERM", "DEFAULT") | |
end | |
end | |
end | |
def perform(n) | |
puts "run #{n}" | |
sleep 0.1 | |
end | |
def shutdown(n) | |
return if @shutdowning | |
@shutdowning = true | |
puts "gracefully shutdown #{n}" | |
end | |
pid = Process.pid | |
Thread.new do | |
sleep 1 | |
Process.kill("TERM", pid) | |
end | |
start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment