ruby-mqtt
ruby-mqtt copied to clipboard
How to handle sigterm?
I'm using this in a sidekiq job (not the best place for it I realize, in the name of time I'm not daemonizing)
The only thing is, is there a way I can check something inside the local loop in the gem?
This obviously only works when there's a message, but otherwise it will get SIGKILL
client.get do |topic, message|
break if $sidekiq_shutdown_pending
puts topic, message
end
You can rescue interrupts in Ruby. Here's a blogpost (not mine) talking a bit about it. So you'd have to wrap the code in a begin block and then rescue the interrupt.
If instead of what you have there, you were to do:
MQTT::Client.connect(...) do |c|
...
end
Then your code would be wrapped in an ensure block which should exit cleanly.
awesome thank you