GithubHelp home page GithubHelp logo

isabella232 / retryable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudfoundry-attic/retryable

0.0 0.0 0.0 149 KB

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

License: MIT License

Ruby 100.00%

retryable's Introduction

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.retryable(:tries => 3, :on => OpenURI::HTTPError) do
  xml = open("http://example.com/test.xml").read
end

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

Retryable.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.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.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
end

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
  pick_up_soap
end

You can temporary disable retryable blocks

Retryble.enabled?
=> true

Retryble.disable

Retryble.enabled?
=> false

Monkey patching Kernel

To add retryable to both Kernel and the global namespace:

require "retryable/core_ext/kernel"
require "open-uri"

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

# Or..

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

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

retryable's People

Contributors

adsummos avatar czottmann avatar fb3 avatar kml avatar nfedyashev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.