Disable automatic retry with ActiveJob, used with Sidekiq

后端 未结 6 1037
名媛妹妹
名媛妹妹 2021-02-03 23:58

Is there a way to disable automatic retry with ActiveJob and Sidekiq ?

I know that with Sidekiq only, we just have to put

sidekiq_options :retry => f         


        
6条回答
  •  终归单人心
    2021-02-04 00:23

    I had this same need, ie ActiveJob wrapping Sidekiq but wanting to support max_retries. I put this in an initializer. If #max_retries is defined on an ActiveJob job, it will be used to set retries. If #ephemeral? is defined and returns true, job will not be rerun and will not be transferred to 'dead' if it fails.

    class Foobar::SidekiqClientMiddleware
      def call(worker_class, msg, queue, redis_pool)
        aj_job = ActiveJob::Base.deserialize(msg['args'][0]) rescue nil
        msg['retry'] = aj_job.respond_to?(:max_retries) ? aj_job.max_retries : 5
        msg['retry'] = false if aj_job.respond_to?(:ephemeral?) && aj_job.ephemeral?
        yield
      end
    end
    
    Sidekiq.configure_client do |config|
      config.redis = { url: "redis://#{redis_host}:6379/12" }
      config.client_middleware do |chain|
        chain.add Foobar::SidekiqClientMiddleware
      end
    end
    
    Sidekiq.configure_server do |config|
      config.redis = { url: "redis://#{redis_host}:6379/12" }
      config.client_middleware do |chain|
        chain.add Foobar::SidekiqClientMiddleware
      end
    end
    

    Note: it actually is important to add this to the middleware chain for both client and server if any of your jobs create new jobs themselves as they are executed.

提交回复
热议问题