Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 68 lines (35 sloc) 1.615 kb
5d602cb @carlo Initial import.
authored
1 # Kernel#retryable
2
4b54d36 @carlo Added note re new home/maintainer.
authored
3
4 **PlEASE NOTE: Kernel#retryable has a [new home!](https://github.com/nfedyashev/retryable)**
5
6
0ab535d @carlo Text changes.
authored
7 ## Description
5d602cb @carlo Initial import.
authored
8
9 Runs a code block, and retries it when an exception occurs. It's great when
10 working with flakey webservices (for example).
11
12 It's configured using two optional parameters --`:tries` and `:on`--, and
13 runs the passed block. Should an exception occur, it'll retry for (n-1) times.
14
15 Should the number of retries be reached without success, the last exception
0ca32dc @carlo More meat.
authored
16 will be raised.
5d602cb @carlo Initial import.
authored
17
18
0ab535d @carlo Text changes.
authored
19 ## Examples
5d602cb @carlo Initial import.
authored
20
97f18ba @carlo Fixed the formatting.
authored
21 Open an URL, retry up to two times when an `OpenURI::HTTPError` occurs.
5d602cb @carlo Initial import.
authored
22
8bdb98c @carlo Minor text changes.
authored
23 require "retryable"
a48a0fe @carlo Clarified example.
authored
24 require "open-uri"
25
2a5e520 @carlo Fixed a typo in the examples.
authored
26 retryable( :tries => 3, :on => OpenURI::HTTPError ) do
a48a0fe @carlo Clarified example.
authored
27 xml = open( "http://example.com/test.xml" ).read
5d602cb @carlo Initial import.
authored
28 end
29
97f18ba @carlo Fixed the formatting.
authored
30 Do _something_, retry up to four times for either `ArgumentError` or
31 `TimeoutError` exceptions.
5d602cb @carlo Initial import.
authored
32
8bdb98c @carlo Minor text changes.
authored
33 require "retryable"
34
5d602cb @carlo Initial import.
authored
35 retryable( :tries => 5, :on => [ ArgumentError, TimeoutError ] ) do
36 # some crazy code
37 end
38
39
0ab535d @carlo Text changes.
authored
40 ## Defaults
5d602cb @carlo Initial import.
authored
41
42 :tries => 1, :on => Exception
43
0ab535d @carlo Text changes.
authored
44
45 ## Installation
5d602cb @carlo Initial import.
authored
46
47 First, [make sure GitHub is a gem source](http://gems.github.com/). Then, install the gem:
48
49 sudo gem install carlo-retryable
50
51
0ab535d @carlo Text changes.
authored
52 ## Todo
0ca32dc @carlo More meat.
authored
53
54 * Unit tests
55
56
6cab093 @carlo 1.2, bugfix release.
authored
57 ## Changelog
58
6df0391 @carlo Markup upfuckery.
authored
59 * v1.2: FIX -- block would run twice when `:tries` was set to `0`. (Thanks for the
6cab093 @carlo 1.2, bugfix release.
authored
60 heads-up to [Tuker](http://github.com/tuker).)
61
62
0ab535d @carlo Text changes.
authored
63 ## Thanks
0ca32dc @carlo More meat.
authored
64
65 Many thanks to [Chu Yeow for this nifty piece of code](http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/). Look, I liked it
66 enough to enhance it a little bit and build a gem from it! :)
67
Something went wrong with that request. Please try again.