Search code examples
rubysidekiq

Sidekiq transient vs fatal errors


Is there a way to err from a Sidekiq job in a way that tells Sidekiq that "this error is fatal and unrecoverable, do not retry, send it straight to dead job queue"?

Looking at Sidekiq Error Handling documentation, it seems like it interpret all errors as transient, and will retry a job (if retry is enabled) regardless of the error type.


Solution

  • If found on GitHub a solution for your problem. In that post they suggested to write a custom middleware that handles the exceptions you want to prevent retries for. This is a basic example:

    def call(worker, msg, queue)
      begin
        yield
      rescue ActiveRecord::RecordNotFound => e
        msg['retry'] = false
        raise
      end
    end
    

    You can extending that you get:

    def call(worker, msg, queue)
      begin
        yield
      rescue ActiveRecord::RecordNotFound => e
        msg['retry'] = false
        raise
      rescue Exception => e
        if worker.respond_to?(:handle_error)
          worker.handle_error(e)
        else
          raise
        end
      end
    end