retryable 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +26 -5
- data/lib/retryable.rb +14 -2
- metadata +22 -6
data/README.markdown
CHANGED
@@ -43,7 +43,7 @@ end
|
|
43
43
|
|
44
44
|
## Defaults
|
45
45
|
|
46
|
-
:tries => 2, :on => Exception, :sleep => 1
|
46
|
+
:tries => 2, :on => Exception, :sleep => 1, :matching => /.*/
|
47
47
|
|
48
48
|
Sleeping
|
49
49
|
--------
|
@@ -54,14 +54,33 @@ retryable(:sleep => 0) { } # don't pause at all between retries
|
|
54
54
|
retryable(:sleep => 10) { } # sleep ten seconds between retries
|
55
55
|
retryable(:sleep => lambda { |n| 4**n }) { } # sleep 1, 4, 16, etc. each try
|
56
56
|
```
|
57
|
-
|
57
|
+
|
58
|
+
Matching error messages
|
59
|
+
--------
|
60
|
+
You can also retry based on the exception message:
|
61
|
+
```
|
62
|
+
:retryable(:matching => /IO timeout/) do |retries, exception|
|
63
|
+
raise "yo, IO timeout!" if retries == 0
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
Block Parameters
|
68
|
+
--------
|
69
|
+
Your block is called with two optional parameters: the number of tries until now, and the most recent exception.
|
70
|
+
```
|
71
|
+
retryable do |retries, exception|
|
72
|
+
puts "try #{retries} failed with exception: #{exception}" if retries > 0
|
73
|
+
pick_up_soap
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
58
77
|
Installation
|
59
78
|
-------
|
60
79
|
|
61
80
|
Install the gem:
|
62
81
|
|
63
82
|
``` bash
|
64
|
-
$ gem install retryable
|
83
|
+
$ gem install retryable
|
65
84
|
```
|
66
85
|
|
67
86
|
Add it to your Gemfile:
|
@@ -73,8 +92,10 @@ gem 'retryable'
|
|
73
92
|
|
74
93
|
## Changelog
|
75
94
|
|
76
|
-
* v1.4
|
77
|
-
* v1.3:
|
95
|
+
* v1.2.4: added :matching option + better options validation
|
96
|
+
* v1.2.3: fixed dependencies
|
97
|
+
* v1.2.2: added :sleep option
|
98
|
+
* v1.2.1: stability -- Thoroughly unit-tested
|
78
99
|
* v1.2: FIX -- block would run twice when `:tries` was set to `0`. (Thanks for the heads-up to [Tuker](http://github.com/tuker).)
|
79
100
|
|
80
101
|
|
data/lib/retryable.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
|
2
2
|
module Kernel
|
3
|
+
class InvalidRetryableOptions < RuntimeError; end
|
4
|
+
|
3
5
|
def retryable(options = {}, &block)
|
4
|
-
opts = { :tries => 2, :sleep => 1, :on => Exception }
|
6
|
+
opts = { :tries => 2, :sleep => 1, :on => Exception, :matching => /.*/ }
|
7
|
+
check_for_invalid_options(options, opts)
|
8
|
+
opts.merge!(options)
|
5
9
|
|
6
10
|
return nil if opts[:tries] == 0
|
7
11
|
|
@@ -10,11 +14,19 @@ module Kernel
|
|
10
14
|
|
11
15
|
begin
|
12
16
|
return yield retries
|
13
|
-
rescue *retry_exception
|
17
|
+
rescue *retry_exception => exception
|
18
|
+
raise unless exception.message =~ opts[:matching]
|
14
19
|
raise if retries+1 >= opts[:tries]
|
15
20
|
sleep opts[:sleep].respond_to?(:call) ? opts[:sleep].call(retries) : opts[:sleep]
|
16
21
|
retries += 1
|
17
22
|
retry
|
18
23
|
end
|
19
24
|
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def check_for_invalid_options(custom_options, default_options)
|
29
|
+
invalid_options = default_options.merge(custom_options).keys - default_options.keys
|
30
|
+
raise InvalidRetryableOptions.new("Invalid options: #{invalid_options.join(", ")}") unless invalid_options.empty?
|
31
|
+
end
|
20
32
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retryable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 4
|
10
|
+
version: 1.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nikita Fedyashev
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-05-
|
20
|
+
date: 2011-05-17 00:00:00 Z
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
23
|
name: rspec
|
@@ -36,9 +36,25 @@ dependencies:
|
|
36
36
|
type: :development
|
37
37
|
version_requirements: *id001
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
|
-
name:
|
39
|
+
name: autotest
|
40
40
|
prerelease: false
|
41
41
|
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 35
|
47
|
+
segments:
|
48
|
+
- 4
|
49
|
+
- 4
|
50
|
+
- 6
|
51
|
+
version: 4.4.6
|
52
|
+
type: :development
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: bundler
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
58
|
none: false
|
43
59
|
requirements:
|
44
60
|
- - ">="
|
@@ -48,7 +64,7 @@ dependencies:
|
|
48
64
|
- 0
|
49
65
|
version: "0"
|
50
66
|
type: :development
|
51
|
-
version_requirements: *
|
67
|
+
version_requirements: *id003
|
52
68
|
description: Kernel#retryable, allow for retrying of code blocks.
|
53
69
|
email: loci.master@gmail.com
|
54
70
|
executables: []
|