retriable 2.0.0.beta2 → 2.0.0.beta3
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -3
- data/README.md +14 -0
- data/Rakefile +4 -4
- data/lib/retriable.rb +34 -43
- data/lib/retriable/config.rb +2 -0
- data/lib/retriable/core_ext/kernel.rb +1 -1
- data/lib/retriable/exponential_backoff.rb +39 -0
- data/lib/retriable/version.rb +1 -1
- data/retriable.gemspec +7 -7
- data/spec/config_spec.rb +47 -0
- data/spec/exponential_backoff_spec.rb +49 -0
- data/spec/retriable_spec.rb +98 -24
- data/spec/spec_helper.rb +4 -4
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3b8d447babc7584997f6e2756375d2d48d3b4dc
|
4
|
+
data.tar.gz: 3c14a6becbd1708ee8dc4f2cb83c10fb74489141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f04b74b90611da941a4bf425ef06383aa9140043d8b32bebec64fab29d2445db0819be5c1a40e135dee0ff0289c5c56a7061793fd46b1a307bcfc6ade287e5d7
|
7
|
+
data.tar.gz: 18282a856bb96a2dd2b32a564ab2093c3620c09a38f95fdc58054084e4410626ecf18daf2a8f275a81d7893d02b0cac06e0a3cba8f818d90c3388866ff62dbc6
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
+
## 2.0.0.beta3
|
2
|
+
* Accept `intervals` array argument to provide your own custom intervals.
|
3
|
+
* Refactor the exponential backoff code into it's own class.
|
4
|
+
* Add specs for exponential backoff, randomization, and config.
|
5
|
+
|
1
6
|
## 2.0.0.beta2
|
2
|
-
* Raise not return on max elapsed time, also check for elapsed time after next interval is calculated and it goes over the max elapsed time
|
3
|
-
* Add specs for max_elapsed_time and max_interval
|
7
|
+
* Raise not return on max elapsed time, also check for elapsed time after next interval is calculated and it goes over the max elapsed time.
|
8
|
+
* Add specs for `max_elapsed_time` and `max_interval`.
|
4
9
|
|
5
10
|
## 2.0.0.beta1
|
6
11
|
* Require ruby 2.0+.
|
7
12
|
* Default to random exponential backoff, removes the `interval` option. Exponential backoff is configurable via arguments.
|
8
13
|
* Allow configurable defaults via `#configure`.
|
9
|
-
* Change `Retriable.retriable` to `Retriable.retry
|
14
|
+
* Change `Retriable.retriable` to `Retriable.retry`.
|
10
15
|
* Support `max_elapsed_time` termination.
|
11
16
|
|
12
17
|
## 1.4.1
|
data/README.md
CHANGED
@@ -10,6 +10,8 @@ Ruby 2.0+
|
|
10
10
|
|
11
11
|
If you need 1.9.x support, use the [1.x branch](https://github.com/kamui/retriable/tree/1.x).
|
12
12
|
|
13
|
+
WARNING: 2.x isn't API compatible with 1.x.
|
14
|
+
|
13
15
|
## Installation
|
14
16
|
|
15
17
|
via command line:
|
@@ -80,6 +82,8 @@ randomized_interval = retry_interval * (random value in range [1 - randomization
|
|
80
82
|
|
81
83
|
`max_elapsed_time` (default: 900 (15 min)) - The maximum amount of total time that code is allowed to keep being retried
|
82
84
|
|
85
|
+
`intervals` (default: nil) - Skip generated intervals and provide your own array of intervals in seconds. Setting this option will ignore `max_tries`, `base_interval`, `max_interval`, `rand_factor`, and `multiplier` values.
|
86
|
+
|
83
87
|
`timeout` (default: 0) - Number of seconds to allow the code block to run before raising a Timeout::Error
|
84
88
|
|
85
89
|
`on` (default: [StandardError]) - An array of exceptions to rescue for each attempt, also accepts a single Exception type
|
@@ -131,6 +135,16 @@ Retriable.retry base_interval: (200/1000.0), timeout: (500/1000.0) do
|
|
131
135
|
end
|
132
136
|
```
|
133
137
|
|
138
|
+
### Custom Interval Array
|
139
|
+
|
140
|
+
You can also bypass the built-in interval generation and provide your own array of intervals. Supplying your own intervals overrides the `max_tries`, `base_interval`, `max_interval`, `rand_factor`, and `multiplier` parameters.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
Retriable.retry intervals: [0.5, 1.0, 2.0, 2.5] do
|
144
|
+
# code here...
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
134
148
|
### Turn off Exponential Backoff
|
135
149
|
|
136
150
|
Exponential backoff is enabled by default, if you want to simply execute code every second, you can do this:
|
data/Rakefile
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require "bundler"
|
4
4
|
Bundler::GemHelper.install_tasks
|
5
5
|
|
6
|
-
require
|
6
|
+
require "rake/testtask"
|
7
7
|
task default: :test
|
8
8
|
|
9
9
|
desc "Run tests"
|
10
10
|
task :test do
|
11
11
|
Rake::TestTask.new do |t|
|
12
|
-
t.libs <<
|
13
|
-
t.pattern =
|
12
|
+
t.libs << "lib" << "spec"
|
13
|
+
t.pattern = "spec/**/*_spec.rb"
|
14
14
|
t.verbose = true
|
15
15
|
end
|
16
16
|
end
|
data/lib/retriable.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "timeout"
|
2
|
+
require "retriable/config"
|
3
|
+
require "retriable/exponential_backoff"
|
4
|
+
require "retriable/version"
|
4
5
|
|
5
6
|
module Retriable
|
6
7
|
extend self
|
7
8
|
|
8
|
-
|
9
|
+
attr_reader :config
|
9
10
|
|
10
11
|
def self.configure
|
11
|
-
self.config ||= Config.new
|
12
12
|
yield(config)
|
13
13
|
end
|
14
14
|
|
15
|
+
def config
|
16
|
+
@config ||= Config.new
|
17
|
+
end
|
18
|
+
|
15
19
|
def retry(
|
16
20
|
max_tries: config.max_tries,
|
17
21
|
base_interval: config.base_interval,
|
@@ -19,52 +23,39 @@ module Retriable
|
|
19
23
|
rand_factor: config.rand_factor,
|
20
24
|
multiplier: config.multiplier,
|
21
25
|
max_elapsed_time: config.max_elapsed_time,
|
26
|
+
intervals: config.intervals,
|
22
27
|
timeout: config.timeout,
|
23
28
|
on: config.on,
|
24
29
|
on_retry: config.on_retry,
|
25
|
-
&block
|
30
|
+
&block
|
31
|
+
)
|
26
32
|
|
27
33
|
raise LocalJumpError unless block_given?
|
28
34
|
|
29
|
-
attempt = 0
|
30
|
-
interval = base_interval
|
31
35
|
start_time = Time.now
|
32
|
-
elapsed_time =
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
else
|
55
|
-
interval * multiplier
|
36
|
+
elapsed_time = -> { Time.now - start_time }
|
37
|
+
|
38
|
+
max_tries = intervals.size if intervals
|
39
|
+
intervals ||= ExponentialBackoff.new(
|
40
|
+
max_tries: max_tries,
|
41
|
+
base_interval: base_interval,
|
42
|
+
multiplier: multiplier,
|
43
|
+
max_interval: max_interval,
|
44
|
+
rand_factor: rand_factor
|
45
|
+
).intervals
|
46
|
+
|
47
|
+
intervals.each.with_index(1) do |interval, attempt|
|
48
|
+
begin
|
49
|
+
if timeout
|
50
|
+
Timeout::timeout(timeout) { return block.call(attempt) }
|
51
|
+
else
|
52
|
+
return block.call(attempt)
|
53
|
+
end
|
54
|
+
rescue *[*on] => exception
|
55
|
+
on_retry.call(exception, attempt, Time.now - start_time, interval) if on_retry
|
56
|
+
raise if attempt >= max_tries || (elapsed_time.call + interval) > max_elapsed_time
|
57
|
+
sleep interval if interval > 0 && config.sleep_disabled != true
|
56
58
|
end
|
57
|
-
|
58
|
-
retry
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
62
|
-
private
|
63
|
-
def randomized_interval(rand_factor, interval)
|
64
|
-
return interval if rand_factor == 0
|
65
|
-
delta = rand_factor * interval * 1.0
|
66
|
-
min_interval = interval - delta
|
67
|
-
max_interval = interval + delta
|
68
|
-
rand(min_interval..max_interval)
|
69
|
-
end
|
70
61
|
end
|
data/lib/retriable/config.rb
CHANGED
@@ -7,6 +7,7 @@ module Retriable
|
|
7
7
|
attr_accessor :rand_factor
|
8
8
|
attr_accessor :multiplier
|
9
9
|
attr_accessor :max_elapsed_time
|
10
|
+
attr_accessor :intervals
|
10
11
|
attr_accessor :timeout
|
11
12
|
attr_accessor :on
|
12
13
|
attr_accessor :on_retry
|
@@ -19,6 +20,7 @@ module Retriable
|
|
19
20
|
@rand_factor = 0.5
|
20
21
|
@multiplier = 1.5
|
21
22
|
@max_elapsed_time = 900 # 15 minn
|
23
|
+
@intervals = nil
|
22
24
|
@timeout = nil
|
23
25
|
@on = [StandardError]
|
24
26
|
@on_retry = nil
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Retriable
|
2
|
+
class ExponentialBackoff
|
3
|
+
attr_accessor :max_tries, :base_interval, :multiplier, :max_interval, :rand_factor
|
4
|
+
|
5
|
+
def initialize(
|
6
|
+
max_tries: Retriable.config.max_tries,
|
7
|
+
base_interval: Retriable.config.base_interval,
|
8
|
+
multiplier: Retriable.config.multiplier,
|
9
|
+
max_interval: Retriable.config.max_interval,
|
10
|
+
rand_factor: Retriable.config.rand_factor
|
11
|
+
)
|
12
|
+
|
13
|
+
@max_tries = max_tries
|
14
|
+
@base_interval = base_interval
|
15
|
+
@multiplier = multiplier
|
16
|
+
@max_interval = max_interval
|
17
|
+
@rand_factor = rand_factor
|
18
|
+
end
|
19
|
+
|
20
|
+
def intervals
|
21
|
+
intervals = Array.new(max_tries) do |iteration|
|
22
|
+
[base_interval * multiplier ** iteration, max_interval].min
|
23
|
+
end
|
24
|
+
|
25
|
+
return intervals if rand_factor == 0
|
26
|
+
|
27
|
+
intervals.map { |i| randomize(i) }
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
def randomize(interval)
|
32
|
+
return interval if rand_factor == 0
|
33
|
+
delta = rand_factor * interval * 1.0
|
34
|
+
min_interval = interval - delta
|
35
|
+
max_interval = interval + delta
|
36
|
+
rand(min_interval..max_interval)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/retriable/version.rb
CHANGED
data/retriable.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require "retriable/version"
|
5
5
|
|
@@ -22,10 +22,10 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
|
-
s.add_development_dependency
|
26
|
-
s.add_development_dependency
|
27
|
-
s.add_development_dependency
|
28
|
-
s.add_development_dependency
|
29
|
-
s.add_development_dependency
|
30
|
-
s.add_development_dependency
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
s.add_development_dependency "minitest", ">= 5.0"
|
27
|
+
s.add_development_dependency "minitest-focus"
|
28
|
+
s.add_development_dependency "pry"
|
29
|
+
s.add_development_dependency "guard"
|
30
|
+
s.add_development_dependency "guard-minitest"
|
31
31
|
end
|
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Retriable::Config do
|
4
|
+
subject do
|
5
|
+
Retriable::Config
|
6
|
+
end
|
7
|
+
|
8
|
+
it "sleep defaults to enabled" do
|
9
|
+
subject.new.sleep_disabled.must_equal false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "max tries defaults to 3" do
|
13
|
+
subject.new.max_tries.must_equal 3
|
14
|
+
end
|
15
|
+
|
16
|
+
it "max interval defaults to 60" do
|
17
|
+
subject.new.max_interval.must_equal 60
|
18
|
+
end
|
19
|
+
|
20
|
+
it "randomization factor defaults to 0.5" do
|
21
|
+
subject.new.base_interval.must_equal 0.5
|
22
|
+
end
|
23
|
+
|
24
|
+
it "multiplier defaults to 1.5" do
|
25
|
+
subject.new.multiplier.must_equal 1.5
|
26
|
+
end
|
27
|
+
|
28
|
+
it "max elapsed time defaults to 900" do
|
29
|
+
subject.new.max_elapsed_time.must_equal 900
|
30
|
+
end
|
31
|
+
|
32
|
+
it "intervals defaults to nil" do
|
33
|
+
subject.new.intervals.must_be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "timeout defaults to nil" do
|
37
|
+
subject.new.timeout.must_be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it "on defaults to [StandardError]" do
|
41
|
+
subject.new.on.must_equal [StandardError]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "on retry handler defaults to nil" do
|
45
|
+
subject.new.on_retry.must_be_nil
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Retriable::ExponentialBackoff do
|
4
|
+
subject do
|
5
|
+
Retriable::ExponentialBackoff
|
6
|
+
end
|
7
|
+
|
8
|
+
it "max tries defaults to 3" do
|
9
|
+
subject.new.max_tries.must_equal 3
|
10
|
+
end
|
11
|
+
|
12
|
+
it "max interval defaults to 60" do
|
13
|
+
subject.new.max_interval.must_equal 60
|
14
|
+
end
|
15
|
+
|
16
|
+
it "randomization factor defaults to 0.5" do
|
17
|
+
subject.new.base_interval.must_equal 0.5
|
18
|
+
end
|
19
|
+
|
20
|
+
it "multiplier defaults to 1.5" do
|
21
|
+
subject.new.multiplier.must_equal 1.5
|
22
|
+
end
|
23
|
+
|
24
|
+
it "generates randomized intervals" do
|
25
|
+
i = subject.new(max_tries: 9).intervals
|
26
|
+
i[0].between?(0.25, 0.75).must_equal true
|
27
|
+
i[1].between?(0.375, 1.125).must_equal true
|
28
|
+
i[2].between?(0.562, 1.687).must_equal true
|
29
|
+
i[3].between?(0.8435, 2.53).must_equal true
|
30
|
+
i[4].between?(1.265, 3.795).must_equal true
|
31
|
+
i[5].between?(1.897, 5.692).must_equal true
|
32
|
+
i[6].between?(2.846, 8.538).must_equal true
|
33
|
+
i[7].between?(4.269, 12.807).must_equal true
|
34
|
+
i[8].between?(6.403, 19.210).must_equal true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "generates 5 non-randomized intervals" do
|
38
|
+
subject.new(
|
39
|
+
max_tries: 5,
|
40
|
+
rand_factor: 0.0
|
41
|
+
).intervals.must_equal([
|
42
|
+
0.5,
|
43
|
+
0.75,
|
44
|
+
1.125,
|
45
|
+
1.6875,
|
46
|
+
2.53125
|
47
|
+
])
|
48
|
+
end
|
49
|
+
end
|
data/spec/retriable_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "spec_helper"
|
2
2
|
|
3
3
|
class TestError < Exception; end
|
4
4
|
|
@@ -7,20 +7,29 @@ describe Retriable do
|
|
7
7
|
Retriable
|
8
8
|
end
|
9
9
|
|
10
|
-
describe
|
10
|
+
describe "with sleep disabled" do
|
11
11
|
before do
|
12
12
|
Retriable.configure do |c|
|
13
13
|
c.sleep_disabled = true
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
18
|
-
|
17
|
+
it "stops at first attempt if the block does not raise an exception" do
|
18
|
+
attempts = 0
|
19
|
+
subject.retry do
|
20
|
+
attempts += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
attempts.must_equal 1
|
24
|
+
end
|
25
|
+
|
26
|
+
it "raises a LocalJumpError if retry is not given a block" do
|
27
|
+
-> do
|
19
28
|
subject.retry on: EOFError
|
20
29
|
end.must_raise LocalJumpError
|
21
30
|
end
|
22
31
|
|
23
|
-
describe
|
32
|
+
describe "retry block of code raising EOFError with no arguments" do
|
24
33
|
before do
|
25
34
|
@attempts = 0
|
26
35
|
|
@@ -30,20 +39,20 @@ describe Retriable do
|
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
|
-
it
|
42
|
+
it "uses exponential backoff" do
|
34
43
|
@attempts.must_equal 3
|
35
44
|
end
|
36
45
|
end
|
37
46
|
|
38
|
-
it
|
39
|
-
|
47
|
+
it "retry on custom exception and re-raises the exception" do
|
48
|
+
-> do
|
40
49
|
subject.retry on: TestError do
|
41
50
|
raise TestError.new
|
42
51
|
end
|
43
52
|
end.must_raise TestError
|
44
53
|
end
|
45
54
|
|
46
|
-
it
|
55
|
+
it "retry with 10 max tries" do
|
47
56
|
attempts = 0
|
48
57
|
|
49
58
|
subject.retry(
|
@@ -56,21 +65,53 @@ describe Retriable do
|
|
56
65
|
attempts.must_equal 10
|
57
66
|
end
|
58
67
|
|
59
|
-
it
|
60
|
-
|
68
|
+
it "retry will timeout after 1 second" do
|
69
|
+
-> do
|
61
70
|
subject.retry timeout: 1 do
|
62
71
|
sleep 2
|
63
72
|
end
|
64
73
|
end.must_raise Timeout::Error
|
65
74
|
end
|
66
75
|
|
67
|
-
|
76
|
+
it "applies a randomized exponential backoff to each attempt" do
|
77
|
+
@attempts = 0
|
78
|
+
@time_table = {}
|
79
|
+
|
80
|
+
handler = ->(exception, attempt, elapsed_time, next_interval) do
|
81
|
+
exception.class.must_equal ArgumentError
|
82
|
+
@time_table[attempt] = next_interval
|
83
|
+
end
|
84
|
+
|
85
|
+
-> do
|
86
|
+
Retriable.retry(
|
87
|
+
on: [EOFError, ArgumentError],
|
88
|
+
on_retry: handler,
|
89
|
+
rand_factor: 0.0,
|
90
|
+
max_tries: 9
|
91
|
+
) do
|
92
|
+
@attempts += 1
|
93
|
+
raise ArgumentError.new
|
94
|
+
end
|
95
|
+
end.must_raise ArgumentError
|
96
|
+
|
97
|
+
@time_table[1].between?(0.25, 0.75).must_equal true
|
98
|
+
@time_table[2].between?(0.375, 1.125).must_equal true
|
99
|
+
@time_table[3].between?(0.562, 1.687).must_equal true
|
100
|
+
@time_table[4].between?(0.8435, 2.53).must_equal true
|
101
|
+
@time_table[5].between?(1.265, 3.795).must_equal true
|
102
|
+
@time_table[6].between?(1.897, 5.692).must_equal true
|
103
|
+
@time_table[7].between?(2.846, 8.538).must_equal true
|
104
|
+
@time_table[8].between?(4.269, 12.807).must_equal true
|
105
|
+
@time_table[9].between?(6.403, 19.210).must_equal true
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "retries with an on_retry handler, 6 max retries, and a 0.0 rand_factor" do
|
68
109
|
before do
|
69
110
|
max_tries = 6
|
70
111
|
@attempts = 0
|
71
112
|
@time_table = {}
|
72
113
|
|
73
|
-
handler =
|
114
|
+
handler = ->(exception, attempt, elapsed_time, next_interval) do
|
74
115
|
exception.class.must_equal ArgumentError
|
75
116
|
@time_table[attempt] = next_interval
|
76
117
|
end
|
@@ -86,11 +127,11 @@ describe Retriable do
|
|
86
127
|
end
|
87
128
|
end
|
88
129
|
|
89
|
-
it
|
130
|
+
it "makes 6 attempts" do
|
90
131
|
@attempts.must_equal 6
|
91
132
|
end
|
92
133
|
|
93
|
-
it
|
134
|
+
it "applies a non-randomized exponential backoff to each attempt" do
|
94
135
|
@time_table.must_equal({
|
95
136
|
1 => 0.5,
|
96
137
|
2 => 0.75,
|
@@ -101,12 +142,12 @@ describe Retriable do
|
|
101
142
|
end
|
102
143
|
end
|
103
144
|
|
104
|
-
it
|
145
|
+
it "retry has a max interval of 1.5 seconds" do
|
105
146
|
max_tries = 6
|
106
147
|
attempts = 0
|
107
148
|
time_table = {}
|
108
149
|
|
109
|
-
handler =
|
150
|
+
handler = ->(exception, attempt, elapsed_time, next_interval) do
|
110
151
|
time_table[attempt] = next_interval
|
111
152
|
end
|
112
153
|
|
@@ -130,14 +171,47 @@ describe Retriable do
|
|
130
171
|
})
|
131
172
|
end
|
132
173
|
|
133
|
-
it
|
134
|
-
|
174
|
+
it "retries with defined intervals" do
|
175
|
+
intervals = [
|
176
|
+
0.5,
|
177
|
+
0.75,
|
178
|
+
1.125,
|
179
|
+
1.5,
|
180
|
+
1.5
|
181
|
+
]
|
182
|
+
time_table = {}
|
183
|
+
|
184
|
+
handler = ->(exception, attempt, elapsed_time, next_interval) do
|
185
|
+
time_table[attempt] = next_interval
|
186
|
+
end
|
187
|
+
|
188
|
+
-> do
|
189
|
+
subject.retry(
|
190
|
+
on: EOFError,
|
191
|
+
on_retry: handler,
|
192
|
+
intervals: intervals
|
193
|
+
) do
|
194
|
+
raise EOFError.new
|
195
|
+
end
|
196
|
+
end.must_raise EOFError
|
197
|
+
|
198
|
+
time_table.must_equal({
|
199
|
+
1 => 0.5,
|
200
|
+
2 => 0.75,
|
201
|
+
3 => 1.125,
|
202
|
+
4 => 1.5,
|
203
|
+
5 => 1.5
|
204
|
+
})
|
205
|
+
end
|
206
|
+
|
207
|
+
it "can call #retriable in the global" do
|
208
|
+
-> do
|
135
209
|
retriable do
|
136
|
-
puts
|
210
|
+
puts "should raise NoMethodError"
|
137
211
|
end
|
138
212
|
end.must_raise NoMethodError
|
139
213
|
|
140
|
-
require_relative
|
214
|
+
require_relative "../lib/retriable/core_ext/kernel"
|
141
215
|
|
142
216
|
i = 0
|
143
217
|
retriable do
|
@@ -148,7 +222,7 @@ describe Retriable do
|
|
148
222
|
end
|
149
223
|
end
|
150
224
|
|
151
|
-
it
|
225
|
+
it "retry runs for a max elapsed time of 2 seconds" do
|
152
226
|
subject.configure do |c|
|
153
227
|
c.sleep_disabled = false
|
154
228
|
end
|
@@ -158,11 +232,11 @@ describe Retriable do
|
|
158
232
|
attempts = 0
|
159
233
|
time_table = {}
|
160
234
|
|
161
|
-
handler =
|
235
|
+
handler = ->(exception, attempt, elapsed_time, next_interval) do
|
162
236
|
time_table[attempt] = elapsed_time
|
163
237
|
end
|
164
238
|
|
165
|
-
|
239
|
+
-> do
|
166
240
|
subject.retry(
|
167
241
|
base_interval: 1.0,
|
168
242
|
multiplier: 1.0,
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "minitest/focus"
|
3
|
+
require "pry"
|
4
4
|
|
5
|
-
require_relative
|
5
|
+
require_relative "../lib/retriable"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: retriable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.beta3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Chu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -113,8 +113,11 @@ files:
|
|
113
113
|
- lib/retriable.rb
|
114
114
|
- lib/retriable/config.rb
|
115
115
|
- lib/retriable/core_ext/kernel.rb
|
116
|
+
- lib/retriable/exponential_backoff.rb
|
116
117
|
- lib/retriable/version.rb
|
117
118
|
- retriable.gemspec
|
119
|
+
- spec/config_spec.rb
|
120
|
+
- spec/exponential_backoff_spec.rb
|
118
121
|
- spec/retriable_spec.rb
|
119
122
|
- spec/spec_helper.rb
|
120
123
|
- wercker.yml
|
@@ -144,5 +147,7 @@ specification_version: 4
|
|
144
147
|
summary: Retriable is an simple DSL to retry failed code blocks with randomized exponential
|
145
148
|
backoff
|
146
149
|
test_files:
|
150
|
+
- spec/config_spec.rb
|
151
|
+
- spec/exponential_backoff_spec.rb
|
147
152
|
- spec/retriable_spec.rb
|
148
153
|
- spec/spec_helper.rb
|