def_retry 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -9
- data/lib/def_retry.rb +2 -2
- data/lib/def_retry/retrier.rb +4 -3
- data/lib/def_retry/version.rb +1 -1
- data/spec/def_retry_spec.rb +2 -4
- data/spec/retrier_spec.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 506e2b27bc48952be087188682123b8c47f4254f
|
4
|
+
data.tar.gz: c9e8cd16b09b01c64f3a306bddc25a0de38c6c9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19f1ee109469b9b9032c1b5ce44ea8ef9e934fc6013d90e211ce9738c21b5fbde082308a77188efc05178fd73453d4f955aafc42b27bf60b63100ed1bc6ff8fa
|
7
|
+
data.tar.gz: e74bd32c7a9cd1cb2082730759196cff27cf7d9863b8c98108ed31f1b3d79f3dfdfda972339153c1500d6f4c7bb4dcf5edc7fac89167e0ba5cdb5fa52b36cdf9
|
data/README.md
CHANGED
@@ -32,22 +32,24 @@ require 'def_retry'
|
|
32
32
|
class ApiWrapper
|
33
33
|
include DefRetry
|
34
34
|
|
35
|
-
def_retry :get_data, on: ApiError do
|
35
|
+
def_retry :get_data, on: ApiError do |*args|
|
36
36
|
do_api_call
|
37
37
|
end
|
38
38
|
end
|
39
39
|
```
|
40
40
|
|
41
|
-
This will define an instance method named
|
41
|
+
This will define an instance method named `get_data` and rescue the exception
|
42
42
|
`ApiError` and retry the block:
|
43
43
|
|
44
44
|
```ruby
|
45
|
-
do
|
45
|
+
do |*args|
|
46
46
|
do_api_call
|
47
47
|
end
|
48
48
|
```
|
49
49
|
3 times (the default).
|
50
50
|
|
51
|
+
Any arguments passed to the `get_data(*args)` method will get passed to the block.
|
52
|
+
|
51
53
|
### Retrying a block of code
|
52
54
|
|
53
55
|
```ruby
|
@@ -85,21 +87,23 @@ end
|
|
85
87
|
### Create a Retrier with default options
|
86
88
|
|
87
89
|
```ruby
|
88
|
-
|
89
|
-
|
90
|
+
# config/intializers/retrier.rb
|
90
91
|
Retrier = DefRetry::Retrier.new({
|
91
92
|
on: [ApiError, Timeout],
|
92
93
|
tries: 7,
|
93
|
-
on_retry: ->(exception, try_count) {
|
94
|
-
on_ensure: ->(value, try_count) {
|
94
|
+
on_retry: ->(exception, try_count) { Logger.debug exception },
|
95
|
+
on_ensure: ->(value, try_count) { Logger.debug value, try_count },
|
95
96
|
sleep: :exponential,
|
96
|
-
|
97
|
+
re_raise: false
|
97
98
|
})
|
99
|
+
|
100
|
+
# later...
|
101
|
+
Retrier.run { do_api_call }
|
98
102
|
```
|
99
103
|
|
100
104
|
### Options
|
101
105
|
|
102
|
-
These apply to `.def_retry`, `#retryable`, and `DefRetry.
|
106
|
+
These apply to `.def_retry`, `#retryable`, `DefRetry.retry`, and `DefRetry::Retrier.new`:
|
103
107
|
- `:on`: A single class or an array of exception classes to be rescued.
|
104
108
|
- `:tries`: Integer number of maximum retries to run. DefRetry will stop retrying if the retry count reaches this number.
|
105
109
|
- `:sleep`: Either an Integer to pass to `sleep`, a Proc that receives the current try count as its only argument or a Symbol naming one of these sleep strategies: constant, linear, exponential (see: `DefRetry::Retrier::SLEEP_STRATEGIES`).
|
data/lib/def_retry.rb
CHANGED
data/lib/def_retry/retrier.rb
CHANGED
@@ -8,6 +8,7 @@ module DefRetry
|
|
8
8
|
}
|
9
9
|
|
10
10
|
def initialize(options)
|
11
|
+
@args = options.fetch :args, []
|
11
12
|
@tries = options.fetch :tries, DEFAULT_TRIES
|
12
13
|
@on_retry = options.fetch :on_retry, ->(e, n) {}
|
13
14
|
@on_ensure = options.fetch :on_ensure, ->(r, n) {}
|
@@ -17,13 +18,13 @@ module DefRetry
|
|
17
18
|
begin
|
18
19
|
@sleep = SLEEP_STRATEGIES.fetch @sleep if @sleep.is_a? Symbol
|
19
20
|
rescue KeyError
|
20
|
-
raise ArgumentError, "The :sleep option must be a Proc or
|
21
|
+
raise ArgumentError, "The :sleep option must be an Integer, a Proc, or a Symbol: #{SLEEP_STRATEGIES.keys.join(', ')}"
|
21
22
|
end
|
22
23
|
|
23
24
|
begin
|
24
25
|
@exceptions = Array options.fetch(:on)
|
25
26
|
rescue KeyError
|
26
|
-
raise ArgumentError, 'You must specify which
|
27
|
+
raise ArgumentError, 'You must specify which exceptions to retry :on'
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
@@ -32,7 +33,7 @@ module DefRetry
|
|
32
33
|
@return = nil
|
33
34
|
|
34
35
|
begin
|
35
|
-
@return = block.call
|
36
|
+
@return = block.call *@args
|
36
37
|
rescue *@exceptions => e
|
37
38
|
@try_count += 1
|
38
39
|
run_sleep_strategy if @sleep
|
data/lib/def_retry/version.rb
CHANGED
data/spec/def_retry_spec.rb
CHANGED
@@ -10,17 +10,15 @@ class MockRetryable
|
|
10
10
|
end
|
11
11
|
|
12
12
|
describe DefRetry do
|
13
|
-
let(:mock_retryable) { MockRetryable }
|
14
|
-
|
15
13
|
context '.def_retry' do
|
16
14
|
it 'defines an instance method' do
|
17
|
-
expect(
|
15
|
+
expect(MockRetryable.new).to respond_to :api_call
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
19
|
context '#retryable' do
|
22
20
|
it "returns the block's value" do
|
23
|
-
mocked =
|
21
|
+
mocked = MockRetryable.new
|
24
22
|
expect(mocked.retryable(on: Exception) { 2 + 2 }).to be 4
|
25
23
|
end
|
26
24
|
end
|
data/spec/retrier_spec.rb
CHANGED
@@ -13,7 +13,7 @@ describe DefRetry::Retrier do
|
|
13
13
|
context '#new' do
|
14
14
|
it 'requires exceptions to be specified' do
|
15
15
|
expect {
|
16
|
-
DefRetry::Retrier.new({}
|
16
|
+
DefRetry::Retrier.new({})
|
17
17
|
}.to raise_error ArgumentError
|
18
18
|
end
|
19
19
|
|
@@ -97,7 +97,7 @@ describe DefRetry::Retrier do
|
|
97
97
|
|
98
98
|
retrier.run &block_exception
|
99
99
|
expect(@exception).to be_kind_of Exception
|
100
|
-
expect(@retry_count).to be 3 # default
|
100
|
+
expect(@retry_count).to be 3 # default :tries
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'passes the return value and retry count to on_ensure' do
|
@@ -111,7 +111,7 @@ describe DefRetry::Retrier do
|
|
111
111
|
|
112
112
|
retrier.run { :ran }
|
113
113
|
expect(@value).to be :ran
|
114
|
-
expect(@retry_count).to be 0
|
114
|
+
expect(@retry_count).to be 0
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: def_retry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Salazar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
version: '0'
|
93
93
|
requirements: []
|
94
94
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.6.7
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: The Retry Pattern in a gem.
|