retriable 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +4 -0
- data/README.md +32 -13
- data/lib/retriable/retriable.rb +9 -15
- data/lib/retriable/version.rb +1 -1
- data/test/retriable_test.rb +2 -2
- metadata +6 -7
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 1.3.2
|
2
|
+
* Clean up option defaults.
|
3
|
+
* By default, rescue StandardError and Timeout::Error instead of [Exception](http://www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception).
|
4
|
+
|
1
5
|
## 1.3.1
|
2
6
|
* Add `rake` dependency for travis-ci.
|
3
7
|
* Update gemspec summary and description.
|
data/README.md
CHANGED
@@ -25,18 +25,6 @@ In your Gemfile:
|
|
25
25
|
gem 'retriable'
|
26
26
|
```
|
27
27
|
|
28
|
-
By default, requiring 'retriable' will include the #retriable method into th Kernel so that you can use it anywhere. If you don't want this behaviour, you can load a non-kernel included version:
|
29
|
-
|
30
|
-
```ruby
|
31
|
-
gem 'retriable', require => 'retriable/no_kernel'
|
32
|
-
```
|
33
|
-
|
34
|
-
Or in your ruby script:
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
require 'retriable/no_kernel'
|
38
|
-
```
|
39
|
-
|
40
28
|
Usage
|
41
29
|
---------------
|
42
30
|
|
@@ -58,9 +46,13 @@ end
|
|
58
46
|
Here are the available options:
|
59
47
|
|
60
48
|
`tries` (default: 3) - Number of attempts to make at running your code block
|
49
|
+
|
61
50
|
`interval` (default: 0) - Number of seconds to sleep between attempts
|
51
|
+
|
62
52
|
`timeout` (default: 0) - Number of seconds to allow the code block to run before raising a Timeout::Error
|
53
|
+
|
63
54
|
`on` (default: Exception) - Exception or array of exceptions to rescue for each attempt
|
55
|
+
|
64
56
|
`on_retry` - (default: nil) - Proc to call after each attempt is rescued
|
65
57
|
|
66
58
|
You can pass options via an options `Hash`. This example will only retry on a `Timeout::Error`, retry 3 times and sleep for a full second before each attempt.
|
@@ -98,9 +90,13 @@ end
|
|
98
90
|
Retriable also provides a callback called `:on_retry` that will run after an exception is rescued. This callback provides the number of `tries`, and the `exception` that was raised in the current attempt. As these are specified in a `Proc`, unnecessary variables can be left out of the parameter list.
|
99
91
|
|
100
92
|
```ruby
|
101
|
-
|
93
|
+
do_this_on_each_retry = Proc.new do |exception, tries|
|
102
94
|
log "#{exception.class}: '#{exception.message}' - #{tries} attempts."}
|
103
95
|
end
|
96
|
+
|
97
|
+
retriable :on_retry => do_this_on_each_retry do
|
98
|
+
# code here...
|
99
|
+
end
|
104
100
|
```
|
105
101
|
|
106
102
|
What if I want to execute a code block at the end, whether or not an exception was rescued ([ensure](http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-ensure))? Or, what if I want to execute a code block if no exception is raised ([else](http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-else))? Instead of providing more callbacks, I recommend you just wrap retriable in a begin/retry/else/ensure block:
|
@@ -119,6 +115,29 @@ ensure
|
|
119
115
|
end
|
120
116
|
```
|
121
117
|
|
118
|
+
Non Kernel included version
|
119
|
+
---------------------------
|
120
|
+
By default, requiring 'retriable' will include the #retriable method into th Kernel so that you can use it anywhere. If you don't want this behaviour, you can load a non-kernel included version:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
gem 'retriable', require => 'retriable/no_kernel'
|
124
|
+
```
|
125
|
+
|
126
|
+
Or in your ruby script:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
require 'retriable/no_kernel'
|
130
|
+
```
|
131
|
+
|
132
|
+
In this case, you'll just execute a retriable block from the Retriable module:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
Retriable.retriable do
|
136
|
+
# code here...
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
|
122
141
|
Credits
|
123
142
|
-------
|
124
143
|
|
data/lib/retriable/retriable.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
# encoding: utf-8
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
require 'timeout'
|
4
4
|
|
5
5
|
module Retriable
|
6
|
+
extend self
|
7
|
+
|
6
8
|
class Retry
|
7
9
|
attr_accessor :tries
|
8
10
|
attr_accessor :interval
|
@@ -14,7 +16,7 @@ module Retriable
|
|
14
16
|
@tries = 3
|
15
17
|
@interval = 0
|
16
18
|
@timeout = nil
|
17
|
-
@on =
|
19
|
+
@on = [StandardError, Timeout::Error]
|
18
20
|
@on_retry = nil
|
19
21
|
|
20
22
|
yield self if block_given?
|
@@ -42,21 +44,13 @@ module Retriable
|
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
|
-
def retriable(
|
46
|
-
|
47
|
-
:tries => 3,
|
48
|
-
:on => Exception,
|
49
|
-
:interval => 1
|
50
|
-
}
|
51
|
-
|
52
|
-
opts.merge!(options)
|
53
|
-
|
54
|
-
raise 'No block given' unless block_given?
|
47
|
+
def retriable(opts = {}, &block)
|
48
|
+
raise 'no block given' unless block_given?
|
55
49
|
|
56
50
|
Retry.new do |r|
|
57
|
-
r.tries = opts[:tries]
|
58
|
-
r.on = opts[:on]
|
59
|
-
r.interval = opts[:interval]
|
51
|
+
r.tries = opts[:tries] if opts[:tries]
|
52
|
+
r.on = opts[:on] if opts[:on]
|
53
|
+
r.interval = opts[:interval] if opts[:interval]
|
60
54
|
r.timeout = opts[:timeout] if opts[:timeout]
|
61
55
|
r.on_retry = opts[:on_retry] if opts[:on_retry]
|
62
56
|
end.perform(&block)
|
data/lib/retriable/version.rb
CHANGED
data/test/retriable_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retriable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70283922934020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70283922934020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &70283922933200 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70283922933200
|
36
36
|
description: Retriable is an simple DSL to retry code if an exception is raised. This
|
37
37
|
is especially useful when interacting external api/services or file system calls.
|
38
38
|
email:
|
@@ -81,4 +81,3 @@ specification_version: 3
|
|
81
81
|
summary: Retriable is an simple DSL to retry code if an exception is raised.
|
82
82
|
test_files:
|
83
83
|
- test/retriable_test.rb
|
84
|
-
has_rdoc:
|