How to set retry count for Sidekiq with ActiveJob?

后端 未结 7 2040
走了就别回头了
走了就别回头了 2021-02-07 01:50

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: :imp         


        
7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 02:39

    Since Rails 5.1, there is a built-in way to do this using the retry_on method. It's a general ActiveJob method, so it will work with any queuing backend, not just Sidekiq.

    For example, for your specific job you could do:

    class SiteScraperJob < ActiveJob::Base
      retry_on ErrorLoadingSite, queue: :low_priority, attempts: 5
    
      def perform(*args)
        # raise ErrorLoadingSite if cannot scrape
      end
    end
    

    You can also set a constant wait interval or an exponential wait strategy, as explained in the docs.

提交回复
热议问题