Skip to content

Kernel#retryable, by Cheah Chu Yeow (https://is.gd/faW9), slightly enhanced and rebuilt as gem as a little Munich Hackday project.

License

Notifications You must be signed in to change notification settings

toddsifleet/retryable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

retryable gem

Build Status

Description

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 four optional parameters :tries, :on, :sleep, :matching, :ensure 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.

Examples

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

require "open-uri"

retryable(:tries => 3, :on => OpenURI::HTTPError) do
  xml = open("https://example.com/test.xml").read
end

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

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

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 = File.open("testfile")

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

  f.close
end

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

Defaults

:tries => 2, :on => StandardError, :sleep => 1, :matching  => /.*/, :ensure => Proc.new { }

Sleeping

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

retryable(:sleep => 0) { }                # don't pause at all between retries
retryable(:sleep => 10) { }               # sleep ten seconds between retries
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:

Using Regex, don't raise if the exception message matches the supplied regex:

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

Using lambda/Proc, don't raise if the supplied lambda returns true:

retryable(:matching => lambda {|e| e.is_a?(RuntimeError) && e.message =~ /IO timeout/}) do |retries, exception|
  raise "yo, IO timeout!" if retries == 0
end

Block Parameters

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

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

You can temporary disable retryable blocks

Retryble.enabled?
=> true

Retryble.disable

Retryble.enabled?
=> false

Installation

Install the gem:

$ gem install retryable

Add it to your Gemfile:

gem 'retryable'

Thanks

Chu Yeow for this nifty piece of code

Scott Bronson

About

Kernel#retryable, by Cheah Chu Yeow (https://is.gd/faW9), slightly enhanced and rebuilt as gem as a little Munich Hackday project.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%