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 --
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
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
require "retryable" retryable( :tries => 5, :on => [ ArgumentError, TimeoutError ] ) do # some crazy code end
:tries => 1, :on => Exception
Install the gem:
$ gem install retryable
Add it to your Gemfile:
- v1.3: stability -- Thoroughly unit-tested
- v1.2: FIX -- block would run twice when
:trieswas set to
0. (Thanks for the heads-up to Tuker.)
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! :)