Created
August 2, 2018 13:42
-
-
Save deemytch/6d3f8fd35a796f6b1cdc1e10a4911cc8 to your computer and use it in GitHub Desktop.
Graceful shutdown of a ruby amqp (bunny) worker
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
#!/usr/bin/ruby | |
# правильный пример мягкого завершения обработки сообщений | |
require 'bunny' | |
$gameover = false | |
$nonstop = Mutex.new | |
trap('INT') do | |
STDERR.puts "Wanted interrupt." | |
$gameover = true | |
end | |
trap('TERM') do | |
STDERR.puts "Wanted termination." | |
$gameover = true | |
end | |
$consumer = Bunny.new.start.create_channel.queue('trap').subscribe(block: false) do |di, props, body| | |
$nonstop.lock | |
puts "di: #{ di.inspect}\n props: #{ props.inspect }\n#{ body }\n" | |
sleep(10) | |
puts "КЦ" | |
$nonstop.unlock | |
end | |
loop do | |
if $gameover | |
puts "So, let me finish this." | |
sleep(0.5) while $nonstop.locked? | |
$consumer.cancel | |
break | |
end | |
print '.' | |
sleep 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment