rails

https://github.com/rails/rails

Ruby

Ruby on Rails

ActiveJob::Exceptions::ClassMethods#retry_on

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.

==== Options
* <tt>:wait</tt> - Re-enqueues the job with a delay specified either in seconds (default: 3 seconds),
  as a computing proc that the number of executions so far as an argument, or as a symbol reference of
  <tt>:exponentially_longer<>, which applies the wait algorithm of <tt>(executions ** 4) + 2</tt>
  (first wait 3s, then 18s, then 83s, etc)
* <tt>:attempts</tt> - Re-enqueues the job the specified number of times (default: 5 attempts)
* <tt>:queue</tt> - Re-enqueues the job on a different queue
* <tt>:priority</tt> - Re-enqueues the job with a different priority

==== Examples

 class RemoteServiceJob < ActiveJob::Base
   retry_on CustomAppException # defaults to 3s wait, 5 attempts
   retry_on AnotherCustomAppException, wait: ->(executions) { executions * 2 }
   retry_on(YetAnotherCustomAppException) do |exception|
     ExceptionNotifier.caught(exception)
   end
   retry_on ActiveRecord::StatementInvalid, wait: 5.seconds, attempts: 3
   retry_on Net::OpenTimeout, wait: :exponentially_longer, attempts: 10

   def perform(*args)
     # Might raise CustomAppException, AnotherCustomAppException, or YetAnotherCustomAppException for something domain specific
     # Might raise ActiveRecord::StatementInvalid when a local db deadlock is detected
     # Might raise Net::OpenTimeout when the remote service is down
   end
 end

Source | Google | Stack overflow

Edit

git clone git@github.com:rails/rails.git

cd rails

open activejob/lib/active_job/exceptions.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-ActiveJob--Exceptions--ClassMethods-retry_on-for-pr


# Commit to git

git add activejob/lib/active_job/exceptions.rbgit commit -m "better docs for ActiveJob::Exceptions::ClassMethods#retry_on"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-ActiveJob--Exceptions--ClassMethods-retry_on-for-pr

hub pull-request


# Celebrate!