Giter Club home page Giter Club logo

retryable's Introduction

Kernel#retryable

PlEASE NOTE: Kernel#retryable has a new home!

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 two optional parameters --:tries and :on--, 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 "retryable"
require "open-uri"

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.

require "retryable"

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

Defaults

:tries => 1, :on => Exception

Installation

First, make sure GitHub is a gem source. Then, install the gem:

sudo gem install carlo-retryable

Todo

  • Unit tests

Changelog

  • v1.2: FIX -- block would run twice when :tries was set to 0. (Thanks for the heads-up to Tuker.)

Thanks

Many thanks to Chu Yeow for this nifty piece of code. Look, I liked it enough to enhance it a little bit and build a gem from it! :)

retryable's People

Contributors

czottmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

retryable's Issues

number of tries

shouldn't it be retry if (tries -= 1) > 1 instead of retry if (tries -= 1) > 0 ?

As it's written now, it does one extra try

>> retryable(:tries => 3) { puts "."; puts a }
.
.
.
.
NameError: undefined local variable or method `a' for #<Object:0x100177298>
    from (irb):5
    from /Users/barmstrong/NetBeansProjects/viper/lib/retryable.rb:14:in `retryable'
    from (irb):5

In any event, thanks for posting this we've found it super useful!

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.