retryable 1.2 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/Gemfile +9 -0
- data/README.markdown +75 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/retryable.rb +4 -4
- data/retryable.gemspec +59 -0
- data/spec/lib/retryable_spec.rb +53 -0
- data/spec/spec_helper.rb +9 -0
- metadata +115 -18
- data/test/tests.rb +0 -1
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
retryable gem
|
2
|
+
=====
|
3
|
+
|
4
|
+
![travis-ci](http://travis-ci.org/nfedyashev/retryable.png)
|
5
|
+
|
6
|
+
Description
|
7
|
+
--------
|
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
|
16
|
+
will be raised.
|
17
|
+
|
18
|
+
|
19
|
+
Examples
|
20
|
+
--------
|
21
|
+
|
22
|
+
Open an URL, retry up to two times when an `OpenURI::HTTPError` occurs.
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
require "retryable"
|
26
|
+
require "open-uri"
|
27
|
+
|
28
|
+
retryable( :tries => 3, :on => OpenURI::HTTPError ) do
|
29
|
+
xml = open( "http://example.com/test.xml" ).read
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Do _something_, retry up to four times for either `ArgumentError` or
|
34
|
+
`TimeoutError` exceptions.
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
require "retryable"
|
38
|
+
|
39
|
+
retryable( :tries => 5, :on => [ ArgumentError, TimeoutError ] ) do
|
40
|
+
# some crazy code
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Defaults
|
45
|
+
|
46
|
+
:tries => 1, :on => Exception
|
47
|
+
|
48
|
+
|
49
|
+
Installation
|
50
|
+
-------
|
51
|
+
|
52
|
+
Install the gem:
|
53
|
+
|
54
|
+
``` bash
|
55
|
+
$ gem install retryable
|
56
|
+
```
|
57
|
+
|
58
|
+
Add it to your Gemfile:
|
59
|
+
|
60
|
+
``` ruby
|
61
|
+
gem 'retryable'
|
62
|
+
```
|
63
|
+
|
64
|
+
|
65
|
+
## Changelog
|
66
|
+
|
67
|
+
* v1.3: stability -- Thoroughly unit-tested
|
68
|
+
* v1.2: FIX -- block would run twice when `:tries` was set to `0`. (Thanks for the heads-up to [Tuker](http://github.com/tuker).)
|
69
|
+
|
70
|
+
|
71
|
+
## Thanks
|
72
|
+
|
73
|
+
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
|
74
|
+
enough to enhance it a little bit and build a gem from it! :)
|
75
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
gem.name = "retryable"
|
17
|
+
gem.homepage = "http://github.com/nfedyashev/retryable"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{Kernel#retryable, allow for retrying of code blocks.}
|
20
|
+
gem.description = %Q{Kernel#retryable, allow for retrying of code blocks.}
|
21
|
+
gem.email = "loci.master@gmail.com"
|
22
|
+
gem.authors = ["Nikita Fedyashev", "Carlo Zottmann", "Chu Yeow"]
|
23
|
+
|
24
|
+
gem.add_development_dependency 'bundler', ['>= 1.0.0']
|
25
|
+
gem.add_development_dependency 'rspec', '~> 2.5.0'
|
26
|
+
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rspec/core'
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
RSpec::Core::RakeTask.new(:spec)
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
39
|
+
|
40
|
+
rdoc.rdoc_dir = 'rdoc'
|
41
|
+
rdoc.title = "retryable #{version}"
|
42
|
+
rdoc.rdoc_files.include('README*')
|
43
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
44
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.1
|
data/lib/retryable.rb
CHANGED
@@ -2,18 +2,18 @@
|
|
2
2
|
# http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/
|
3
3
|
# Slightly rewritten to allow for passing of more than just type of exception.
|
4
4
|
|
5
|
-
def retryable(
|
5
|
+
def retryable(options = {}, &block)
|
6
6
|
opts = { :tries => 1, :on => Exception }.merge(options)
|
7
7
|
|
8
8
|
return nil if opts[:tries] == 0
|
9
|
-
|
9
|
+
|
10
10
|
retry_exception, tries = [ opts[:on] ].flatten, opts[:tries]
|
11
|
-
|
11
|
+
|
12
12
|
begin
|
13
13
|
return yield
|
14
14
|
rescue *retry_exception
|
15
15
|
retry if (tries -= 1) > 0
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
yield
|
19
19
|
end
|
data/retryable.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{retryable}
|
8
|
+
s.version = "1.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nikita Fedyashev", "Carlo Zottmann", "Chu Yeow"]
|
12
|
+
s.date = %q{2011-05-10}
|
13
|
+
s.description = %q{Kernel#retryable, allow for retrying of code blocks.}
|
14
|
+
s.email = %q{loci.master@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".travis.yml",
|
20
|
+
"Gemfile",
|
21
|
+
"README.markdown",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/retryable.rb",
|
25
|
+
"retryable.gemspec",
|
26
|
+
"spec/lib/retryable_spec.rb",
|
27
|
+
"spec/spec_helper.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/nfedyashev/retryable}
|
30
|
+
s.licenses = ["MIT"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.7.1}
|
33
|
+
s.summary = %q{Kernel#retryable, allow for retrying of code blocks.}
|
34
|
+
|
35
|
+
if s.respond_to? :specification_version then
|
36
|
+
s.specification_version = 3
|
37
|
+
|
38
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
39
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
40
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
41
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
|
42
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
43
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
46
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
47
|
+
s.add_dependency(%q<rspec>, ["~> 2.5.0"])
|
48
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
49
|
+
s.add_dependency(%q<rspec>, ["~> 2.5.0"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 2.5.0"])
|
55
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
56
|
+
s.add_dependency(%q<rspec>, ["~> 2.5.0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'retryable' do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@attempt = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should make another try on exception" do
|
10
|
+
retryable(:tries => 1, :on => RuntimeError) do
|
11
|
+
@attempt = @attempt + 1
|
12
|
+
raise RuntimeError unless @attempt == 2
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should make another try if exception is in whitelist" do
|
17
|
+
retryable(:tries => 1, :on => [StandardError, ArgumentError, RuntimeError]) do
|
18
|
+
@attempt = @attempt + 1
|
19
|
+
raise ArgumentError unless @attempt == 2
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not try on unexpected exception" do
|
24
|
+
lambda do
|
25
|
+
retryable(:tries => 1, :on => RuntimeError) do
|
26
|
+
@attempt = @attempt + 1
|
27
|
+
raise StandardError unless @attempt == 2
|
28
|
+
end
|
29
|
+
end.should raise_error StandardError
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should make always retry on default exception" do
|
33
|
+
retryable(:tries => 1) do
|
34
|
+
@attempt = @attempt + 1
|
35
|
+
raise StandardError unless @attempt == 2
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should retry once unless :tries explicitly set" do
|
40
|
+
retryable do
|
41
|
+
@attempt = @attempt + 1
|
42
|
+
raise StandardError unless @attempt == 2
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should retry twice" do
|
47
|
+
retryable(:tries => 2, :on => RuntimeError) do
|
48
|
+
@attempt = @attempt + 1
|
49
|
+
raise RuntimeError unless @attempt == 3
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,57 +1,154 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retryable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 1.2.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
13
|
+
- Nikita Fedyashev
|
7
14
|
- Carlo Zottmann
|
8
15
|
- Chu Yeow
|
9
16
|
autorequire:
|
10
17
|
bindir: bin
|
11
18
|
cert_chain: []
|
12
19
|
|
13
|
-
date:
|
14
|
-
|
15
|
-
|
16
|
-
|
20
|
+
date: 2011-05-10 00:00:00 Z
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
type: :development
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
version_requirements: *id001
|
36
|
+
name: bundler
|
37
|
+
prerelease: false
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
type: :development
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 15
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 6
|
49
|
+
- 0
|
50
|
+
version: 1.6.0
|
51
|
+
version_requirements: *id002
|
52
|
+
name: jeweler
|
53
|
+
prerelease: false
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
type: :development
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 27
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 5
|
65
|
+
- 0
|
66
|
+
version: 2.5.0
|
67
|
+
version_requirements: *id003
|
68
|
+
name: rspec
|
69
|
+
prerelease: false
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
type: :development
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 23
|
78
|
+
segments:
|
79
|
+
- 1
|
80
|
+
- 0
|
81
|
+
- 0
|
82
|
+
version: 1.0.0
|
83
|
+
version_requirements: *id004
|
84
|
+
name: bundler
|
85
|
+
prerelease: false
|
86
|
+
- !ruby/object:Gem::Dependency
|
87
|
+
type: :development
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 27
|
94
|
+
segments:
|
95
|
+
- 2
|
96
|
+
- 5
|
97
|
+
- 0
|
98
|
+
version: 2.5.0
|
99
|
+
version_requirements: *id005
|
100
|
+
name: rspec
|
101
|
+
prerelease: false
|
17
102
|
description: Kernel#retryable, allow for retrying of code blocks.
|
18
|
-
email:
|
103
|
+
email: loci.master@gmail.com
|
19
104
|
executables: []
|
20
105
|
|
21
106
|
extensions: []
|
22
107
|
|
23
|
-
extra_rdoc_files:
|
24
|
-
|
108
|
+
extra_rdoc_files:
|
109
|
+
- README.markdown
|
25
110
|
files:
|
111
|
+
- .travis.yml
|
112
|
+
- Gemfile
|
113
|
+
- README.markdown
|
114
|
+
- Rakefile
|
115
|
+
- VERSION
|
26
116
|
- lib/retryable.rb
|
27
|
-
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
117
|
+
- retryable.gemspec
|
118
|
+
- spec/lib/retryable_spec.rb
|
119
|
+
- spec/spec_helper.rb
|
120
|
+
homepage: http://github.com/nfedyashev/retryable
|
121
|
+
licenses:
|
122
|
+
- MIT
|
32
123
|
post_install_message:
|
33
124
|
rdoc_options: []
|
34
125
|
|
35
126
|
require_paths:
|
36
127
|
- lib
|
37
128
|
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
38
130
|
requirements:
|
39
131
|
- - ">="
|
40
132
|
- !ruby/object:Gem::Version
|
133
|
+
hash: 3
|
134
|
+
segments:
|
135
|
+
- 0
|
41
136
|
version: "0"
|
42
|
-
version:
|
43
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
44
139
|
requirements:
|
45
140
|
- - ">="
|
46
141
|
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
143
|
+
segments:
|
144
|
+
- 0
|
47
145
|
version: "0"
|
48
|
-
version:
|
49
146
|
requirements: []
|
50
147
|
|
51
148
|
rubyforge_project:
|
52
|
-
rubygems_version: 1.
|
149
|
+
rubygems_version: 1.7.1
|
53
150
|
signing_key:
|
54
151
|
specification_version: 3
|
55
152
|
summary: Kernel#retryable, allow for retrying of code blocks.
|
56
|
-
test_files:
|
57
|
-
|
153
|
+
test_files: []
|
154
|
+
|
data/test/tests.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# nothing to see here yet, move along
|