Skip to content

Instantly share code, notes, and snippets.

@divoxx
Last active July 8, 2016 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divoxx/5784686 to your computer and use it in GitHub Desktop.
Save divoxx/5784686 to your computer and use it in GitHub Desktop.
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
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