Kernel#retryable, by Cheah Chu Yeow (, slightly enhanced and rebuilt as gem as a little Munich Hackday project.
Switch branches/tags
Pull request Compare This branch is 119 commits ahead, 1 commit behind carlo:master.
Latest commit 9a90d7d Apr 20, 2017 @nfedyashev committed on GitHub Merge pull request #16 from john-denisov/patch-1
Add syntax highlighting for code blocks in

retryable gem

Build Status Dependency Status


Runs a code block, and retries it when an exception occurs. It's great when working with flakey webservices (for example).

It's configured using several optional parameters :tries, :on, :sleep, :matching, :ensure, :exception_cb, :not, :sleep_method and runs the passed block. Should an exception occur, it'll retry for (n-1) times.

Should the number of retries be reached without success, the last exception will be raised.


Open an URL, retry up to two times when an OpenURI::HTTPError occurs.

require "open-uri"

Retryable.retryable(:tries => 3, :on => OpenURI::HTTPError) do
  xml = open("").read

Try the block forever.

Retryable.retryable(:tries => :infinite) do
  # some code

Do something, retry up to four times for either ArgumentError or TimeoutError exceptions.

Retryable.retryable(:tries => 5, :on => [ArgumentError, TimeoutError]) do
  # some crazy code

Ensure that block of code is executed, regardless of whether an exception was raised. It doesn't matter if the block exits normally, if it retries to execute block of code, or if it is terminated by an uncaught exception -- the ensure block will get run.

f ="testfile")

ensure_cb = do |retries|
  puts "total retry attempts: #{retries}"


Retryable.retryable(:ensure => ensure_cb) do
  # process file


:tries => 2, :on => StandardError, :sleep => 1, :matching  => /.*/, :ensure => { }, :exception_cb => { }, :not => [], :sleep_method => lambda { |n| Kernel.sleep(n) }

Retryable also could be configured globally to change those defaults:

Retryable.configure do |config|
  config.ensure       = {}
  config.exception_cb = {}
  config.matching     = /.*/
  config.on           = StandardError
  config.sleep        = 1
  config.tries        = 2
  config.not          = []
  config.sleep_method = Celluloid.method(:sleep)


By default Retryable waits for one second between retries. You can change this and even provide your own exponential backoff scheme.

Retryable.retryable(:sleep => 0) { }                     # don't pause at all between retries
Retryable.retryable(:sleep => 10) { }                    # sleep ten seconds between retries
Retryable.retryable(:sleep => lambda { |n| 4**n }) { }   # sleep 1, 4, 16, etc. each try

Matching error messages

You can also retry based on the exception message:

Retryable.retryable(:matching => /IO timeout/) do |retries, exception|
  raise "yo, IO timeout!" if retries == 0

Block Parameters

Your block is called with two optional parameters: the number of tries until now, and the most recent exception.

Retryable.retryable do |retries, exception|
  puts "try #{retries} failed with exception: #{exception}" if retries > 0

Callback to run after an exception is rescued

exception_cb = do |exception|
  ExceptionNotifier.notify_exception(exception, :data => {:message => "it failed"})

Retryable.retryable(:exception_cb => exception_cb) do
  # perform risky operation

You can temporary disable retryable blocks

=> true


=> false

Specify exceptions where a retry should NOT be performed

No more tries will be made if an exception listed in :not is raised. Takes precedence over :on.

class MyError < StandardError; end

Retryable.retryable(:tries => 5, :on => [StandardError], :not => [MyError]) do
  raise MyError "No retries!"

Specify the sleep method to use

This can be very useful when you are working with Celluloid which implements its own version of the method sleep.

Retryable.retryable(:sleep_method => Celluloid.method(:sleep)) do

Supported Ruby Versions

This library aims to support and is tested against the following Ruby versions:

  • Ruby 1.8.7
  • Ruby 1.9.2
  • Ruby 1.9.3
  • Ruby 2.0.0
  • Ruby 2.1.2
  • Ruby 2.2.0
  • Ruby 2.3.1
  • Ruby 2.4.0

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer.


Install the gem:

$ gem install retryable

Add it to your Gemfile:

gem 'retryable'


Chu Yeow for this nifty piece of code

Scott Bronson