An expressive, fully spec'd gem to add the Retry Pattern to your methods and/or objects. With DefRetry
you can define methods with retry logic built-in or you can wrap your code in a
retry
and specify options to customize the behavior.
Add this line to your application's Gemfile:
gem 'def_retry'
And then execute:
$ bundle
Or install it yourself as:
$ gem install def_retry
require 'def_retry'
class ApiWrapper
include DefRetry
def_retry :get_data, on: ApiError do |*args|
do_api_call
end
end
This will define an instance method named get_data
and rescue the exception
ApiError
and retry the block:
do |*args|
do_api_call
end
3 times (the default).
Any arguments passed to the get_data(*args)
method will get passed to the block.
require 'def_retry'
class ApiWrapper
include DefRetry
def get_data
@some_state = 'start'
retryable on: ApiError do
@some_state = 'working'
do_api_call
@some_state = 'done'
end
end
end
This will retry just that block of code.
Use DefRetry.retry
directly:
require 'def_retry'
DefRetry.retry on: ApiError do
do_api_call
end
# config/intializers/retrier.rb
Retrier = DefRetry::Retrier.new({
on: [ApiError, Timeout],
tries: 7,
on_retry: ->(exception, try_count) { Logger.debug exception },
on_ensure: ->(value, try_count) { Logger.debug value, try_count },
sleep: :exponential,
re_raise: false
})
# later...
Retrier.run { do_api_call }
These apply to .def_retry
, #retryable
, DefRetry.retry
, and DefRetry::Retrier.new
:
:on
: A single class or an array of exception classes to be rescued.:tries
: Integer number of maximum retries to run. DefRetry will stop retrying if the retry count reaches this number.:sleep
: Either an Integer to pass tosleep
, a Proc that receives the current try count as its only argument or a Symbol naming one of these sleep strategies: constant, linear, exponential (see:DefRetry::Retrier::SLEEP_STRATEGIES
).:on_retry
: A callback to run every time a retry happens i.e. the specified exception(s) are rescued. It will receive the exception that was rescued and the current try count as arguments, respectively.:on_ensure
: A callback to run at the end before returning the block's value. It will receive the block's return value and the current try count as arguments, respectively.:re_raise
: (default true) re raise the exception after done retrying.
- Fork it ( https://github.com/DiegoSalazar/def_retry/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request