librelist archives

« back to archive

Catch USR1 within perform loop

Catch USR1 within perform loop

From:
Andrew Vit
Date:
2013-09-13 @ 00:16
For one of our longer-running worker threads, I'd like a way to catch
signals and perform a clean shut-down in case of a USR1 or TERM. Is
there a way to access the "quiet" or "waiting for shutdown" status of
the controlling sidekiq process from the worker?

```
class MyWorker
  include Sidekiq::Worker

  def perform
    process_forever { throw :stop if Sidekiq.shutting_down? }
  end

  def process_forever(&control)
    catch(:stop) do
      loop { sleep 5; yield if block_given? }
      logger.info "Cleaning up"
    end
  end
end
```

I thought of setting my own Signal.trap but quickly realized there can
only be one for each signal, and that would probably break things.

Thanks,
Andrew Vit

Re: Catch USR1 within perform loop

From:
Andrew Vit
Date:
2013-09-16 @ 23:20
I'm checking `Sidekiq::Fetcher.done?` to see if my slow worker should
handle a clean shut down. That's perfect for when USR1 was received.

Is there an equivalent global flag for TERM, when Sidekiq goes into the
wait for timeout state?

Andrew Vit