7

I am trying to send an alert every time retry_count of a sidekiq job reaches 5(to warn an engineer to check why the worker is failing) and then continued being retried as usual.

Is there a way to get the retry count for a particular job from inside the job?

I could just use:

sidekiq_retry_in do |count|
  (warn engineer here)
  10 * (count + 1) # (i.e. 10, 20, 30, 40)
end

and send a message from in there, but I think its a bit of a hack.

Any ideas? googling didn't surface any results.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Totic
  • 1,281
  • 1
  • 10
  • 21

2 Answers2

5

There is no way to get the retry count from within the job, by design.

Mike Perham
  • 21,300
  • 6
  • 59
  • 61
0

Add retry middleware:

class Sidekiq::RetryMiddleware
  def call(_, job, _)
    if job['retry_count']
      job['args'].first['retry_count'] = job['retry_count']
    end
    yield
  end
end

Then add this middleware to the server:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add(Sidekiq::RetryMiddleware)
  end
end

Then in ApplicationJob add retry_count:

class ApplicationJob < ActiveJob::Base
  attr_accessor :retry_count

  def deserialize(job_data)
    self.retry_count = job_data['retry_count'] || 0
    super
  end
end

Seems like that is the only way to do that. Might be better to change executions, but for me retry_count is enough.

Works with sidekiq 7.1.2

Dmitry Polushkin
  • 3,283
  • 1
  • 38
  • 44