How to set retry count for Sidekiq with ActiveJob?

后端 未结 7 2037
走了就别回头了
走了就别回头了 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条回答
  •  情书的邮戳
    2021-02-07 02:32

    You also might be interested in this solution which uses serialize and deserialize api to store the number of attempts.

    class DeliverWebhookJob < ActiveJob::Base
      def serialize
        super.merge('attempt_number' => (@attempt_number || 0) + 1)
      end
    
      def deserialize(job_data)
        super
        @attempt_number = job_data['attempt_number']
      end
    
      rescue_from(ErrorLoadingSite) do |exception|
        retry_job(wait: 10) if @attempt_number < 5
      end
    
      def perform(*args)
        # raise ErrorLoadingSite if cannot scrape
      end
    end
    

    Take it from here.

提交回复
热议问题