https://github.com/restaurant-cheetah/take2
Easily define Take2 🎬 to retry API calls, methods, or just a block of code.
https://github.com/restaurant-cheetah/take2
error-recovery retry retry-pattern retrying ruby ruby-on-rails
Last synced: 7 months ago
JSON representation
Easily define Take2 🎬 to retry API calls, methods, or just a block of code.
- Host: GitHub
- URL: https://github.com/restaurant-cheetah/take2
- Owner: restaurant-cheetah
- License: mit
- Created: 2018-09-12T13:04:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-08-14T17:00:08.000Z (over 5 years ago)
- Last Synced: 2025-04-22T01:22:42.499Z (7 months ago)
- Topics: error-recovery, retry, retry-pattern, retrying, ruby, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 62.5 KB
- Stars: 12
- Watchers: 15
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Take2
[](https://circleci.com/gh/restaurant-cheetah/take2/tree/master)



1. Define rules for retrying behavior.
2. Yield block of code into the with_retry method.
3. Things getting take two :)
## Install
```ruby
gem install take2
```
## Examples
```ruby
class Service
include Take2
number_of_retries 3
# Could be configured globally or on class level.
retriable_errors Net::HTTPRetriableError, Errno::ECONNRESET
# Retry unless the response status is 5xx. The implementation is dependent of the http lib in use.
retriable_condition proc { |error| error.response.code < 500 }
# Defines callable code to run before next retry. Could be an out put to some logger.
on_retry proc { |error, tries| puts "#{name} - Retrying.. #{tries} of #{retriable_configuration[:retries]} (#{error})" }
# The available strategies are:
# type :constant, start: 2 => [2, 2, 2, 2 ... ]
# type :linear, start: 3, factor: 2 => [3, 6, 12, 24 ... ]
# type :fibonacci, start: 2 => [2, 3, 5, 8, 13 ... ]
# type :exponential, start: 3 => [3, 7, 12, 28, 47 ... ]
backoff_strategy type: :fibonacci, start: 3
class << self
def call
with_retry do
# Some logic that might raise..
# If it will raise retriable, magic happens.
# If not the original error re raised
raise Net::HTTPRetriableError.new('Release the Kraken...many times!!', nil)
end
end
# Pass custom options per method call
# The class defaults will not be overwritten
def read(file)
with_retry(retries: 2, retriable: [IOError], retry_proc: proc {}, retry_condition_proc: proc {}) do
# Some logic that might raise..
end
end
end
end
Service.call
#=> KratosService - Retrying.. 3 of 3 (Release the Kraken...many times!!)
#=> KratosService - Retrying.. 2 of 3 (Release the Kraken...many times!!)
#=> KratosService - Retrying.. 1 of 3 (Release the Kraken...many times!!)
# After the retrying is done, original error re-raised
#=> Net::HTTPRetriableError: Release the Kraken...many times!!
# Current configuration hash
Service.retriable_configuration
```
## Configurations
#### could be implemented as rails initializer
```ruby
# config/initializers/take2.rb
Take2.configure do |config|
config.retries = 3
config.retriable = [
Net::HTTPRetriableError,
Errno::ECONNRESET,
IOError
].freeze
config.retry_condition_proc = proc { false }
config.retry_proc = proc { Rails.logger.info "Retry message" }
config.backoff_intervals = Take2::Backoff.new(:linear, 1).intervals
end
```