Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable automatic retry with ActiveJob, used with Sidekiq

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 => false

as mentioned here : https://github.com/mperham/sidekiq/wiki/Error-Handling#configuration

but it doesn't seem to work with ActiveJob and Sidekiq.

I also know the solution to entierly disable the retry as proposed here : https://stackoverflow.com/a/28216822/2431728

But it's not the behavior I need.

like image 230
Jules Ivanic Avatar asked Feb 09 '15 15:02

Jules Ivanic


4 Answers

As of sidekiq 6.0.1, it is possible to pass the following to an ActiveJob worker to prevent it from retrying:

class ExampleJob < ActiveJob::Base
  sidekiq_options retry: false

  def perform(*args)
    # Perform Job
  end
end

More information: https://github.com/mperham/sidekiq/wiki/Active-Job#customizing-error-handling

EDIT:

According to this this requires Rails 6.0.1 or later as well.

like image 189
hananamar Avatar answered Oct 10 '22 02:10

hananamar


Ok thanks for the answer.

Just for information, I also asked the question in an issue related to this subject on ActiveJob Github repository : https://github.com/rails/activejob/issues/47

DHH answered me a solution I haven't tested but that can do the job.

Personnally, I finally put this in an initializer in order to disable Sidekiq retries globally and it works well :

Sidekiq.configure_server do |config|
   config.server_middleware do |chain|
      chain.add Sidekiq::Middleware::Server::RetryJobs, :max_retries => 0
   end
end
like image 40
Jules Ivanic Avatar answered Oct 10 '22 01:10

Jules Ivanic


There is no way to configure anything about Sidekiq with ActiveJob. Use a Sidekiq Worker if you don't want to use the defaults.

like image 8
Mike Perham Avatar answered Oct 10 '22 02:10

Mike Perham


You can catch up the exception and to do nothing instead retry or to configure retry:

  class ExampleJob < ActiveJob::Base
    rescue_from(StandardError) do |exception|
      Rails.logger.error "[#{self.class.name}] Hey, something was wrong with you job #{exception.to_s}"       
    end

    def perform
      raise StandardError, "error_message"
    end
  end

  class ExampleJob < ActiveJob::Base
    rescue_from(StandardError) do |exception|
      retry_job wait: 5.minutes, queue: :low_priority     
    end

    def perform
      raise StandardError, "error_message"
    end
  end

For running retrying you can use retry_on method retry_on method doc

Sidekiq wiki for retries with Active Job integration

like image 7
ilgam Avatar answered Oct 10 '22 01:10

ilgam