Specs

Buy Me A Coffee

EasyRetry

Easily retry a block of code a predetermined number of times.

Easy Retry adds a #tries method to the Numeric class. The #tries method takes a block of code and will retry the block a number of times equal to the number the method is called on. It also aliases #try to #tries in the odd case that you want to (~re~?)try a block of code only once.

Installation

Add this line to your application's Gemfile:

gem 'easy_retry'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install easy_retry

Usage

If you're not using Bundler, you'll need to require the gem in your code:

require 'easy_retry'

Basic Example

4.tries do |try|
  raise 'Something went wrong' if try < 4
  puts "Success!"
end

The code above will produce the following output:

  RuntimeError: Something went wrong (1/4)
  RuntimeError: Something went wrong (2/4)
  RuntimeError: Something went wrong (3/4)
  Success!

Only Retry on Certain Exceptions

Sometimes you want to only retry your code if a specific exception is raised. You can do this by passing a list of exceptions to the #tries method:

4.tries(rescue_from: [ZeroDivisionError, ArgumentError]) do |try|
  raise ZeroDivisionError, 'Whoops' if try < 2
  raise ActiveRecord::RecordInvalid if try < 4
  puts "Success!"
end

The code above will not rescue from the ActiveRecord::RecordInvalid error and produce the following output:

  ZeroDivisionError: Whoops (1/4)
  ActiveRecord::RecordInvalid: Record invalid

Passing an array is not necessary if you need to only rescue from a single error

4.tries(rescue_from: ZeroDivisionError) do |try|
  raise ZeroDivisionError if try < 2
  raise ActiveRecord::RecordInvalid if try < 4
  puts "Success!"
end

This will generate the same output.

Block results

EasyRetry gives you back the result of the first time the block you passed successfully runs. This can be useful when you need to use the result of the block for other tasks that you do not necessarily want to place in the block.

result = 2.tries do |try|
  raise 'Woops' if try < 2
  "This is try number #{try}"
end

puts result

The code above will produce the following output.

RuntimeError: Woops (1/2)
=> "This is try number 2"

Custom delay

EasyRetry allows you to set the delay algorithm you want to use every time you call the #tries method. The following predefined options exist: :none, :by_try, :default, :exponential.

:none

After a try fails, EasyRetry will not wait and try again immediately.

:by_try

After a try fails, EasyRetry will wait an equal amount of seconds to current try that failed. I.e. 1 second for try one, 2 for try two, 3 for try three, etc.

:default

After a try fails, EasyRetry will wait n^2, where n is the current try that failed.

:exponential

After a try fails, EasyRetry will wait 2^n, where n is the current try that failed.

Usage

You can use the predefined delay algorithms as follows:

3.tries(delay: :none) do
  raise StandardError
end

You can also define a custom lambda function if the predefined options are not meeting your needs. You can use it like this:

3.tries(delay: ->(current_try) { sleep current_try * 9.81 }) do
  raise StandardError
end

Configuration

You can configure EasyRetry by adding an initializer as follows:

EasyRetry.configure do |config|
  # configuration options
end

Logger

By default, EasyRetry uses logger for logging errors. You can add your custom logger in the configuration using the config.logger option.

# For Example, using Rails.logger
config.logger = Rails.logger

NB: The logger should follow Rails Logger conventions.

Default Delay Algorithm

By default the :default delay algorithm (n^2) is used, what's in a name you could say. You can configure the default delay algorithm through the config as follows:

config.delay_algorithm = :default # Or :none, :by_try, :exponential

Of course, also here you can instead use a custom lambda delay:

config.delay_algorithm = ->(current_try) { sleep current_try * 9.81 }

Retry delay

The delay for each retry is based on the iteration count. The delay after each failed attempt is n^2, where n is the current iteration that failed. E.g. after the first try, EasyRetry waits 1 second, after the second try it waits 4 seconds, then 9, then 16, then 25, then 36, etc.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run be rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/GoudekettingRM/easy_retry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the EasyRetry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.