How to set retry count for Sidekiq with ActiveJob?

scho picture scho · Oct 15, 2015 · Viewed 13.6k times · Source

From the Rails API, I found ActiveJob can retry_job interval:

my_job_instance.enqueue
my_job_instance.enqueue wait: 5.minutes
my_job_instance.enqueue queue: :important
my_job_instance.enqueue wait_until: Date.tomorrow.midnight

http://api.rubyonrails.org/classes/ActiveJob/Enqueuing.html

But if I want to set retry count, such as Sidekiq's:

include Sidekiq::Worker
sidekiq_options :retry => 5

https://github.com/mperham/sidekiq/wiki/Error-Handling

How to do in this sample code?

class SiteScrapperJob < ActiveJob::Base
  rescue_from(ErrorLoadingSite) do
    retry_job queue: :low_priority
  end

  def perform(*args)
    # raise ErrorLoadingSite if cannot scrape
  end
end

Now I added this to my job class:

Sidekiq.default_worker_options = { retry: 5 }

But it seems not very good.

Answer

Mike Perham picture Mike Perham · Oct 16, 2015

As of Sidekiq 6.0.4 you can use sidekiq_options in an ActiveJob to set the retry option.