Search code examples
ruby-on-railsrubymiddlewaresidekiq

how to made sidekiq_retries_exhausted available globally for all workers


so I want to Implement sidekiq_retries_exhausted in such a way that it triggers automatically when all retries failed Globally and this behavior should be for all workers in my codebase currently, I have to write sidekiq_retries_exhausted every time in each worker but what I want to understand is there any way so that sidekiq_retries_exhausted triggers when all retries failed and I don't have to write this function on each worker

so in short I want to overwrite local sidekiq_retries_exhausted and making it globally available like a kind of middleware so that it triggers automatically when the worker failed it all retries and I don't have to write this in every worker

currently, it is implemented like this in every worker

    sidekiq_retries_exhausted do |msg, error|
      AlertOnRetriesExhaustedUtils.send_alert_on_retries_exhausted(msg, error)
    end

Solution

  • In sidekiq v5.1 introduced global callback death_handlers when job dies

    Death Notification

    The sidekiq_retries_exhausted callback is specific to a Worker class. Starting in v5.1, Sidekiq can also fire a global callback when a job dies:

    # this goes in your initializer
    Sidekiq.configure_server do |config|
      config.death_handlers << ->(job, ex) do
        puts "Uh oh, #{job['class']} #{job["jid"]} just died with error #{ex.message}."
      end
    end