Sidekiq retry count in job

别来无恙 提交于 2019-12-05 01:15:18
Derek

This can be accomplished by adding a sidekiq middleware to set the msg['retry_count'] as an instance variable of the job class.

Add a middleware (in Rails, it's usually a file in /config/initializers/ folder) like so:

class SidekiqMiddleware
    def call(worker, job, queue)
        worker.retry_count = job['retry_count'] if worker.respond_to?(:retry_count)
        yield
    end
end

Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
        chain.add SidekiqMiddleware
    end
end

In your job:

include Sidekiq::Worker
attr_accessor :retry_count

def retry_count
  @retry_count || 0
end

def perform(args)
  return if retry_count > 5
  ...
end

you dont need to deal with this logic directly to accomplish what you want. simply add some configs to your worker as such..note the sidekiq_options . based on your comment below " prevent Sidekiq from moving the job to the dead jobs queue"

 class MyWorker  
     include Sidekiq::Worker
     sidekiq_options :retry => 5, :dead => false

      def perform
          #do some stuff
      end
 end

then the job should just retry 5 times and fail gracefully. also if you want to execute a code block once 5 retries are spent, the worker has a method called sidekiq_retries_exhausted where you could do some custom logging, etc.

aledalgrande

You can access the retries with the Sidekiq API:

https://github.com/mperham/sidekiq/wiki/API#retries

Find the job you need and use job['retry_count'] to get the number of retries.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!