DEV Community

Felice Forby
Felice Forby

Posted on

Sidekiq's sidekiq_retries_exhausted hook

sidekiq_retries_exhausted is a Sidekiq hook that you can use to do something after Sidekiq has used up all of its retries but the job still failed. It's a good way to make sure you send a failure notification or log an error that might be causing the job to fail instead of it just failing silently.

In the worker, you can set a specific number of retries if you want with sidekiq_options retry: <some_number>. The default is 25 times.

Here are a couple of examples but there are ton of ways to configure this which can be found in the documentation.

# Retry 3 times before giving up
sidekiq_options retry: 3
# Don't retry at all
sidekiq_options retry: false
Enter fullscreen mode Exit fullscreen mode

If the job has used up (exhausted) all of its retries, Sidekiq moves it to the "Dead set" and it will eventually get discarded.

Sidekiq will call the sidekiq_retries_exhausted hook right before moving the job to the dead set if you define it in the worker. sidekiq_retries_exhausted takes a block and receives the queued message as an argument so you can use it to compose a log message. See below for an example:

class MyWorker
  include Sidekiq::Worker
  sidekiq_options retry: 3

  sidekiq_retries_exhausted do |msg, exception|
    # example with using Rails' logger
    Rails.logger.warn("Failed #{msg['class']} with #{msg['args']}: #{msg['error_message']}", error: exception)
  end

  def perform(my_arguments)
    # do some work
  end
end
Enter fullscreen mode Exit fullscreen mode

This hook is worker specific, so you would define sidekiq_retries_exhausted for each worker you need. There is also a way to send a job "death" notice for all workers globally by adding some configuration in the initializer (in Rails that would be the config/initializers/sidekiq.rb file).

Sidekiq.configure_server do |config|
  # other config stuff...

  config.death_handlers << ->(job, ex) do
    Rails.logger.error "Uh oh, #{job['class']} #{job["jid"]} just died with error #{ex.message}."
  end
end
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)