Sidekiq: Getting retry count for current job?

Created on 11 Apr 2013  路  4Comments  路  Source: mperham/sidekiq

Is there a way to get the retry count for the current job? I want to know whether it's retry #1, #2, #3, etc.

Most helpful comment

@mperham is there any reason do not give access to the retry_count?

I found a few workarounds (1 and 2) but I don't understand why retry_count is not a part of standard API

I use Sidekiq to deliver web_hooks to clients and if some web_hook fails the delivery should be retried 30 times (for 60 days). But I want to notify a client about the issue starting from 5th attempt (don't bother the client if he has a short downtime) and I would expect something like this:

class WebhookWorker
  include Sidekiq::Worker
  sidekiq_options queue: :webhook

  def perform(*args)
    # send webhook
  rescue WebHook::Error => ex
    ApplicationErrors.notify_client(...) if retry_count > 4
    raise ex
  end
end

Do I do something wrong?

Thank you

All 4 comments

Nope. Retry is designed to be orthogonal to the job. You can access it from a custom middleware.

@mperham is there any reason do not give access to the retry_count?

I found a few workarounds (1 and 2) but I don't understand why retry_count is not a part of standard API

I use Sidekiq to deliver web_hooks to clients and if some web_hook fails the delivery should be retried 30 times (for 60 days). But I want to notify a client about the issue starting from 5th attempt (don't bother the client if he has a short downtime) and I would expect something like this:

class WebhookWorker
  include Sidekiq::Worker
  sidekiq_options queue: :webhook

  def perform(*args)
    # send webhook
  rescue WebHook::Error => ex
    ApplicationErrors.notify_client(...) if retry_count > 4
    raise ex
  end
end

Do I do something wrong?

Thank you

Sidekiq's internal retry mechanism is not designed for application access, otherwise there would be a thousand configuration knobs for every dev who wanted to customize every aspect of it. If your app has custom error handling and notification logic, you need to build it in your app code.

https://github.com/mperham/sidekiq/wiki/Error-Handling#no-more-bike-shedding

@palexvs maybe you could pass the current time as one of the arguments to your worker and send the notification if sufficient time has passed rather than counting retries?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aglushkov picture aglushkov  路  3Comments

davidcelis picture davidcelis  路  3Comments

edgarjs picture edgarjs  路  3Comments

jlecour picture jlecour  路  4Comments

rajcybage picture rajcybage  路  3Comments