retriable 2.0.0.beta4 → 2.0.0.beta5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +6 -6
- data/lib/retriable.rb +8 -8
- data/lib/retriable/config.rb +2 -2
- data/lib/retriable/exponential_backoff.rb +16 -12
- data/lib/retriable/version.rb +1 -1
- data/spec/config_spec.rb +2 -2
- data/spec/exponential_backoff_spec.rb +4 -4
- data/spec/retriable_spec.rb +10 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7f89877bb9506abc02a59892b97c55a0f995b38
|
4
|
+
data.tar.gz: 7b9e325e86386e6e0e739bb4a51df12882327c1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fc3260da9272d6fe2a8b29c939ea661e49cb45d28b7a082021a44d6823c96d1612860d6a8cdfe5acc0973c0bdbbeeaa55869f0831999396826b2653c47a29c2
|
7
|
+
data.tar.gz: 11113a95d8b63b1bed523f0fd98755dd9b4a176435b40a3cbf877d5eabd136e30aec8eba5ee9b3369d8c1af3311f8883910f7965d19747b077e3dbf9ab867b50
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
## 2.0.0.beta5
|
2
|
+
* Change :max_tries back to :tries.
|
3
|
+
|
1
4
|
## 2.0.0.beta4
|
2
5
|
* Change #retry back to #retriable. Didn't like the idea of defining a method that is also a reserved word.
|
3
6
|
* Add ability for `:on` argument to accept a `Hash` where the keys are exception types and the values are a single or list of `Regexp` pattern(s) to match against exception messages for retrial.
|
data/README.md
CHANGED
@@ -23,7 +23,7 @@ gem install retriable
|
|
23
23
|
In your ruby script:
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
require 'retriable'
|
26
|
+
require 'retriable', '~> 2.0'
|
27
27
|
```
|
28
28
|
|
29
29
|
In your Gemfile:
|
@@ -66,7 +66,7 @@ end
|
|
66
66
|
|
67
67
|
Here are the available options:
|
68
68
|
|
69
|
-
`
|
69
|
+
`tries` (default: 3) - Number of attempts to make at running your code block.
|
70
70
|
|
71
71
|
`base_interval` (default: 0.5) - The initial interval in seconds between attempts.
|
72
72
|
|
@@ -82,7 +82,7 @@ randomized_interval = retry_interval * (random value in range [1 - randomization
|
|
82
82
|
|
83
83
|
`max_elapsed_time` (default: 900 (15 min)) - The maximum amount of total time that code is allowed to keep being retried.
|
84
84
|
|
85
|
-
`intervals` (default: nil) - Skip generated intervals and provide your own array of intervals in seconds. Setting this option will ignore `
|
85
|
+
`intervals` (default: nil) - Skip generated intervals and provide your own array of intervals in seconds. Setting this option will ignore `tries`, `base_interval`, `max_interval`, `rand_factor`, and `multiplier` values.
|
86
86
|
|
87
87
|
`timeout` (default: 0) - Number of seconds to allow the code block to run before raising a Timeout::Error
|
88
88
|
|
@@ -96,7 +96,7 @@ You can change the global defaults with a `#configure` block:
|
|
96
96
|
|
97
97
|
```ruby
|
98
98
|
Retriable.configure do |c|
|
99
|
-
c.
|
99
|
+
c.tries = 5
|
100
100
|
c.max_elapsed_time = 3600 # 1 hour
|
101
101
|
end
|
102
102
|
```
|
@@ -106,7 +106,7 @@ end
|
|
106
106
|
`Retriable.retriable` accepts custom arguments. This example will only retry on a `Timeout::Error`, retry 3 times and sleep for a full second before each attempt.
|
107
107
|
|
108
108
|
```ruby
|
109
|
-
Retriable.retriable on: Timeout::Error,
|
109
|
+
Retriable.retriable on: Timeout::Error, tries: 3, base_interval: 1 do
|
110
110
|
# code here...
|
111
111
|
end
|
112
112
|
```
|
@@ -149,7 +149,7 @@ end
|
|
149
149
|
|
150
150
|
### Custom Interval Array
|
151
151
|
|
152
|
-
You can also bypass the built-in interval generation and provide your own array of intervals. Supplying your own intervals overrides the `
|
152
|
+
You can also bypass the built-in interval generation and provide your own array of intervals. Supplying your own intervals overrides the `tries`, `base_interval`, `max_interval`, `rand_factor`, and `multiplier` parameters.
|
153
153
|
|
154
154
|
```ruby
|
155
155
|
Retriable.retriable intervals: [0.5, 1.0, 2.0, 2.5] do
|
data/lib/retriable.rb
CHANGED
@@ -17,7 +17,7 @@ module Retriable
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def retriable(
|
20
|
-
|
20
|
+
tries: config.tries,
|
21
21
|
base_interval: config.base_interval,
|
22
22
|
max_interval: config.max_interval,
|
23
23
|
rand_factor: config.rand_factor,
|
@@ -33,14 +33,14 @@ module Retriable
|
|
33
33
|
elapsed_time = -> { Time.now - start_time }
|
34
34
|
|
35
35
|
if intervals
|
36
|
-
|
36
|
+
tries = intervals.size
|
37
37
|
else
|
38
38
|
intervals = ExponentialBackoff.new(
|
39
|
-
|
40
|
-
base_interval:
|
41
|
-
multiplier:
|
42
|
-
max_interval:
|
43
|
-
rand_factor:
|
39
|
+
tries: tries,
|
40
|
+
base_interval: base_interval,
|
41
|
+
multiplier: multiplier,
|
42
|
+
max_interval: max_interval,
|
43
|
+
rand_factor: rand_factor
|
44
44
|
).intervals
|
45
45
|
end
|
46
46
|
|
@@ -67,7 +67,7 @@ module Retriable
|
|
67
67
|
end
|
68
68
|
|
69
69
|
on_retry.call(exception, attempt, elapsed_time.call, interval) if on_retry
|
70
|
-
raise if attempt >=
|
70
|
+
raise if attempt >= tries || (elapsed_time.call + interval) > max_elapsed_time
|
71
71
|
sleep interval if config.sleep_disabled != true
|
72
72
|
end
|
73
73
|
end
|
data/lib/retriable/config.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Retriable
|
2
2
|
class Config
|
3
3
|
attr_accessor :sleep_disabled
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :tries
|
5
5
|
attr_accessor :base_interval
|
6
6
|
attr_accessor :max_interval
|
7
7
|
attr_accessor :rand_factor
|
@@ -14,7 +14,7 @@ module Retriable
|
|
14
14
|
|
15
15
|
def initialize
|
16
16
|
@sleep_disabled = false
|
17
|
-
@
|
17
|
+
@tries = 3
|
18
18
|
@base_interval = 0.5
|
19
19
|
@max_interval = 60
|
20
20
|
@rand_factor = 0.5
|
@@ -1,24 +1,28 @@
|
|
1
1
|
module Retriable
|
2
2
|
class ExponentialBackoff
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :tries
|
4
|
+
attr_accessor :base_interval
|
5
|
+
attr_accessor :multiplier
|
6
|
+
attr_accessor :max_interval
|
7
|
+
attr_accessor :rand_factor
|
4
8
|
|
5
9
|
def initialize(
|
6
|
-
|
7
|
-
base_interval:
|
8
|
-
multiplier:
|
9
|
-
max_interval:
|
10
|
-
rand_factor:
|
10
|
+
tries: Retriable.config.tries,
|
11
|
+
base_interval: Retriable.config.base_interval,
|
12
|
+
multiplier: Retriable.config.multiplier,
|
13
|
+
max_interval: Retriable.config.max_interval,
|
14
|
+
rand_factor: Retriable.config.rand_factor
|
11
15
|
)
|
12
16
|
|
13
|
-
@
|
14
|
-
@base_interval
|
15
|
-
@multiplier
|
16
|
-
@max_interval
|
17
|
-
@rand_factor
|
17
|
+
@tries = tries
|
18
|
+
@base_interval = base_interval
|
19
|
+
@multiplier = multiplier
|
20
|
+
@max_interval = max_interval
|
21
|
+
@rand_factor = rand_factor
|
18
22
|
end
|
19
23
|
|
20
24
|
def intervals
|
21
|
-
intervals = Array.new(
|
25
|
+
intervals = Array.new(tries) do |iteration|
|
22
26
|
[base_interval * multiplier ** iteration, max_interval].min
|
23
27
|
end
|
24
28
|
|
data/lib/retriable/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -9,8 +9,8 @@ describe Retriable::Config do
|
|
9
9
|
subject.new.sleep_disabled.must_equal false
|
10
10
|
end
|
11
11
|
|
12
|
-
it "
|
13
|
-
subject.new.
|
12
|
+
it "tries defaults to 3" do
|
13
|
+
subject.new.tries.must_equal 3
|
14
14
|
end
|
15
15
|
|
16
16
|
it "max interval defaults to 60" do
|
@@ -5,8 +5,8 @@ describe Retriable::ExponentialBackoff do
|
|
5
5
|
Retriable::ExponentialBackoff
|
6
6
|
end
|
7
7
|
|
8
|
-
it "
|
9
|
-
subject.new.
|
8
|
+
it "tries defaults to 3" do
|
9
|
+
subject.new.tries.must_equal 3
|
10
10
|
end
|
11
11
|
|
12
12
|
it "max interval defaults to 60" do
|
@@ -22,7 +22,7 @@ describe Retriable::ExponentialBackoff do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "generates randomized intervals" do
|
25
|
-
i = subject.new(
|
25
|
+
i = subject.new(tries: 9).intervals
|
26
26
|
i[0].between?(0.25, 0.75).must_equal true
|
27
27
|
i[1].between?(0.375, 1.125).must_equal true
|
28
28
|
i[2].between?(0.562, 1.687).must_equal true
|
@@ -36,7 +36,7 @@ describe Retriable::ExponentialBackoff do
|
|
36
36
|
|
37
37
|
it "generates 5 non-randomized intervals" do
|
38
38
|
subject.new(
|
39
|
-
|
39
|
+
tries: 5,
|
40
40
|
rand_factor: 0.0
|
41
41
|
).intervals.must_equal([
|
42
42
|
0.5,
|
data/spec/retriable_spec.rb
CHANGED
@@ -56,11 +56,11 @@ describe Retriable do
|
|
56
56
|
end.must_raise TestError
|
57
57
|
end
|
58
58
|
|
59
|
-
it "#retriable
|
59
|
+
it "#retriable tries 10 times" do
|
60
60
|
attempts = 0
|
61
61
|
|
62
62
|
subject.retriable(
|
63
|
-
|
63
|
+
tries: 10
|
64
64
|
) do
|
65
65
|
attempts += 1
|
66
66
|
raise EOFError.new if attempts < 10
|
@@ -91,7 +91,7 @@ describe Retriable do
|
|
91
91
|
on: [EOFError, ArgumentError],
|
92
92
|
on_retry: handler,
|
93
93
|
rand_factor: 0.0,
|
94
|
-
|
94
|
+
tries: 9
|
95
95
|
) do
|
96
96
|
@attempts += 1
|
97
97
|
raise ArgumentError.new
|
@@ -111,7 +111,7 @@ describe Retriable do
|
|
111
111
|
|
112
112
|
describe "retries with an on_#retriable handler, 6 max retries, and a 0.0 rand_factor" do
|
113
113
|
before do
|
114
|
-
|
114
|
+
tries = 6
|
115
115
|
@attempts = 0
|
116
116
|
@time_table = {}
|
117
117
|
|
@@ -124,10 +124,10 @@ describe Retriable do
|
|
124
124
|
on: [EOFError, ArgumentError],
|
125
125
|
on_retry: handler,
|
126
126
|
rand_factor: 0.0,
|
127
|
-
|
127
|
+
tries: tries
|
128
128
|
) do
|
129
129
|
@attempts += 1
|
130
|
-
raise ArgumentError.new if @attempts <
|
130
|
+
raise ArgumentError.new if @attempts < tries
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
@@ -147,7 +147,7 @@ describe Retriable do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
it "#retriable has a max interval of 1.5 seconds" do
|
150
|
-
|
150
|
+
tries = 6
|
151
151
|
attempts = 0
|
152
152
|
time_table = {}
|
153
153
|
|
@@ -159,11 +159,11 @@ describe Retriable do
|
|
159
159
|
on: EOFError,
|
160
160
|
on_retry: handler,
|
161
161
|
rand_factor: 0.0,
|
162
|
-
|
162
|
+
tries: tries,
|
163
163
|
max_interval: 1.5
|
164
164
|
) do
|
165
165
|
attempts += 1
|
166
|
-
raise EOFError.new if attempts <
|
166
|
+
raise EOFError.new if attempts < tries
|
167
167
|
end
|
168
168
|
|
169
169
|
time_table.must_equal({
|
@@ -226,7 +226,7 @@ describe Retriable do
|
|
226
226
|
end
|
227
227
|
|
228
228
|
e = -> do
|
229
|
-
subject.retriable
|
229
|
+
subject.retriable tries: 4, on: { EOFError => nil, TestError => [/foo/, /bar/] }, on_retry: handler do
|
230
230
|
attempts += 1
|
231
231
|
case attempts
|
232
232
|
when 1
|