ruby-mqtt icon indicating copy to clipboard operation
ruby-mqtt copied to clipboard

How to handle sigterm?

Open 9mm opened this issue 3 years ago • 2 comments

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

9mm avatar May 28 '22 16:05 9mm

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.

softwaregravy avatar Jul 01 '22 15:07 softwaregravy

awesome thank you

9mm avatar Jul 01 '22 17:07 9mm