Build Status Code Climate Test Coverage Gem Version

PerfectRetry

Implement to handle retry kit.

Usage and Config

PerfectRetry.with_retry do
  do_something_have_possibilities_errors_task()
end
# in setup.rb

require "timeout"

PerfectRetry.register(:timeout_handling) do |config|
  # Try 4 times retry.
  # default: 3
  config.limit = 4

  # Rescue these error in a block.
  # default: [StandardError]
  config.rescues = [Timeout::Error, StandardError]

  # Don't rescue, don't retry when these error raised.
  # default: []
  config.dont_rescues = [SyntaxError]

  # Sleep this seconds before next retry. `n` is a retry times (1-origin).
  # Infinity retry if `nil` is set.
  # default: proc{|n| n ** 2}
  config.sleep = proc{|n| n * 5 }

  # Logger for something information e.g. '[2/5] Retrying after 3 seconds blah blah'.
  # default: Logger.new(STDERR)
  config.logger = Logger.new("/var/log/agent.log")

  # Logger's log level. Don't change that if `nil` is given. That is useful for pre-configured logger set.
  # default: :info
  config.log_level = :debug

  # Ensure block. Call this block after with_retry block finished with and without any errors.
  # default: proc {}
  config.ensure = proc { puts "finished" }
end

# in main.rb

require "open-uri"

PerfectRetry.new(:timeout_handling).with_retry do
  open("http://example.com")
end

# or

PerfectRetry.with_retry(:timeout_handling) do
  open("http://example.com")
end

Custom config without register

pr = PerfectRetry.new do |config|
  # based on default config
  config.sleep = 1
  config.rescues = [Timeout::Error]
end

pr.with_retry do
  open("http://example.com")
end

# Also you can extend registered config
pr = PerfectRetry.new(:some_registered) do |config|
  config.ensure = proc{ puts "done" }
end

pr.with_retry do
  # something to do
end

Manually retry

PerfectRetry.register(:dont_retry_automatically) do |config|
  config.limit = 0
end

PerfectRetry.with_retry(:dont_retry_automatically) do
  response = HTTPClient.get("http://example.com")
  if response.code == 500
    sleep 3
    throw :retry
  end
end

throw :retry redo the block at first without config.limit checking. In above case, infinity retry while example.com returns 500.

Installation

Add this line to your application's Gemfile:

gem 'perfect_retry'

And then execute:

$ bundle

Or install it yourself as:

$ gem install perfect_retry

See also

License

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