retry_on
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7 (0)
- 5.2.3 (-1)
- 6.0.0 (38)
- 6.1.3.1 (38)
- 6.1.7.7 (0)
- 7.0.0 (19)
- 7.1.3.2 (25)
- 7.1.3.4 (0)
- What's this?
retry_on(*exceptions, wait: 3.seconds, attempts: 5, queue: nil, priority: nil, jitter: JITTER_DEFAULT)
public
Catch the exception and reschedule job for re-execution after so many seconds, for a specific number of attempts. If the exception keeps getting raised beyond the specified number of attempts, the exception is allowed to bubble up to the underlying queuing system, which may have its own retry mechanism or place it in a holding queue for inspection.
You can also pass a block that’ll be invoked if the retry attempts fail for custom logic rather than letting the exception bubble up. This block is yielded with the job instance as the first and the error instance as the second parameter.
`retry_on` and `discard_on` handlers are searched from bottom to top, and up the class hierarchy. The handler of the first class for which exception.is_a?(klass) holds true is the one invoked, if any.
Options
-
:wait - Re-enqueues the job with a delay specified either in seconds (default: 3 seconds), as a computing proc that takes the number of executions so far as an argument, or as a symbol reference of :polynomially_longer, which applies the wait algorithm of ((executions**4) + (Kernel.rand * (executions**4) * jitter)) + 2 (first wait ~3s, then ~18s, then ~83s, etc)
-
:attempts - Re-enqueues the job the specified number of times (default: 5 attempts) or a symbol reference of :unlimited to retry the job until it succeeds
-
:queue - Re-enqueues the job on a different queue
-
:priority - Re-enqueues the job with a different priority
-
:jitter - A random delay of wait time used when calculating backoff. The default is 15% (0.15) which represents the upper bound of possible wait time (expressed as a percentage)
Examples
class RemoteServiceJob < ActiveJob::Base retry_on CustomAppException # defaults to ~3s wait, 5 attempts retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 } retry_on CustomInfrastructureException, wait: 5.minutes, attempts: :unlimited retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3 retry_on Net::OpenTimeout, Timeout::Error, wait: :polynomially_longer, attempts: 10 # retries at most 10 times for Net::OpenTimeout and Timeout::Error combined # To retry at most 10 times for each individual exception: # retry_on Net::OpenTimeout, wait: :polynomially_longer, attempts: 10 # retry_on Net::ReadTimeout, wait: 5.seconds, jitter: 0.30, attempts: 10 # retry_on Timeout::Error, wait: :polynomially_longer, attempts: 10 retry_on(YetAnotherCustomAppException) do |job, error| ExceptionNotifier.caught(error) end def perform(*args) # Might raise CustomAppException, AnotherCustomAppException, or YetAnotherCustomAppException for something domain specific # Might raise ActiveRecord::Deadlocked when a local db deadlock is detected # Might raise Net::OpenTimeout or Timeout::Error when the remote service is down end end