def_retry 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 922034268714241a2daa2aa5911a822df72dba30
4
- data.tar.gz: be54c004348a1638ea9cca9888b8d4e1a87ee8d7
3
+ metadata.gz: cc55787b57b63d7d02f818fcc08844d0ff435568
4
+ data.tar.gz: 81dac9e37930cb37d7de3cfe35da89f97b7e99d1
5
5
  SHA512:
6
- metadata.gz: 47e8447fb8f53962f7d10000907ea204638871401f3ab461aef45576ea5a312a038d788f1c62295a8707c62bef1fb02e06e041b0a3d344bef81aa4d1b08559d4
7
- data.tar.gz: c2ed226eea5e857e3898c372801288c6b4571435b524c63aa0ebd2d25c70e3823a9efe6d70fd17ac5b63bcbc9d0c19cd3e998e5fc5bdd10081184be83421323f
6
+ metadata.gz: 487af13048562ddb668f83b5e57e7465368efd4135c05a10d89c487cb82ecdce4d8626011d04606af7fd533127c35fa26019ecbdcba3e53833a1852e1e200ce2
7
+ data.tar.gz: 4199234a1a2d4ef18302619a48844fadaebb098d9f22b9cf908caa12f83a14f21952fcb99e535b0a3c22b40c0c286b0183ba75e97d612f308ad7fa9571578b8b
data/README.md CHANGED
@@ -82,14 +82,30 @@ DefRetry.retry on: ApiError do
82
82
  end
83
83
  ```
84
84
 
85
+ ### Create a Retrier with default options
86
+
87
+ ```ruby
88
+ require 'def_retry'
89
+
90
+ Retrier = DefRetry::Retrier.new({
91
+ on: [ApiError, Timeout],
92
+ tries: 7,
93
+ on_retry: ->(exception, try_count) { log exception },
94
+ on_ensure: ->(value, try_count) { log value, try_count },
95
+ sleep: :exponential,
96
+
97
+ })
98
+ ```
99
+
85
100
  ### Options
86
101
 
87
- These apply to both `.def_retry` and `#retryable`:
88
- - `:on`: A single class or an array of exception classes to be rescued
89
- - `:tries`: Integer number of maximum retries to run. DefRetry will stop retrying if the retry count reaches this number
90
- - `:sleep`: Either 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`)
102
+ These apply to `.def_retry`, `#retryable`, and `DefRetry.retry`:
103
+ - `:on`: A single class or an array of exception classes to be rescued.
104
+ - `:tries`: Integer number of maximum retries to run. DefRetry will stop retrying if the retry count reaches this number.
105
+ - `: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`).
91
106
  - `:on_retry`: A callback to run every time a retry happens i.e. the specified exception(s) are rescued. It will receive the exception that was rescued and the current try count as arguments, respectively.
92
107
  - `:on_ensure`: A callback to run at the end before returning the block's value. It will receive the block's return value and the current try count as arguments, respectively.
108
+ - `:re_raise`: (default true) re raise the exception after done retrying.
93
109
 
94
110
  ## Contributing
95
111
 
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
24
  end
@@ -35,18 +35,20 @@ module DefRetry
35
35
  @return = block.call
36
36
  rescue *@exceptions => e
37
37
  @try_count += 1
38
- sleep @sleep.call(@try_count) if @sleep
38
+ run_sleep_strategy if @sleep
39
39
  @on_retry.call e, @try_count
40
40
 
41
- if @try_count < @tries
42
- retry
43
- elsif @re_raise
44
- raise e
45
- end
41
+ @try_count < @tries ? retry : (@re_raise and raise)
46
42
  ensure
47
43
  @on_ensure.call @return, @try_count
48
44
  @return
49
45
  end
50
46
  end
47
+
48
+ private
49
+
50
+ def run_sleep_strategy
51
+ sleep @sleep.is_a?(Fixnum) ? @sleep : @sleep.call(@try_count)
52
+ end
51
53
  end
52
54
  end
@@ -1,3 +1,3 @@
1
1
  module DefRetry
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -22,7 +22,7 @@ describe DefRetry::Retrier do
22
22
  DefRetry::Retrier.new({
23
23
  on: Exception,
24
24
  sleep: :invalid
25
- }, -> {})
25
+ })
26
26
  }.to raise_error ArgumentError
27
27
  end
28
28
  end
@@ -32,36 +32,46 @@ describe DefRetry::Retrier do
32
32
  the_block = -> { :ran }
33
33
  retrier = DefRetry::Retrier.new({
34
34
  on: Exception
35
- }, the_block)
35
+ })
36
36
 
37
- expect(retrier.run).to be :ran
37
+ expect(retrier.run(&the_block)).to be :ran
38
38
  end
39
39
 
40
- it 'retries on exception :limit times' do
40
+ it 'retries on exception :tries times' do
41
41
  retrier = DefRetry::Retrier.new({
42
42
  on: Exception,
43
- tries: 2
44
- }, block_exception)
43
+ tries: 2,
44
+ re_raise: false
45
+ })
45
46
 
46
- retrier.run
47
+ retrier.run &block_exception
47
48
  expect(@retries).to be 2
48
49
  end
49
50
 
51
+ it 'reraises the exception by default after done retrying' do
52
+ retrier = DefRetry::Retrier.new({
53
+ on: Exception
54
+ })
55
+
56
+ expect { retrier.run &block_exception }.to raise_error Exception
57
+ end
58
+
50
59
  it 'raises unspecified exceptions' do
51
60
  retrier = DefRetry::Retrier.new({
52
61
  on: ArgumentError
53
- }, block_exception)
62
+ })
54
63
 
55
- expect { retrier.run }.to raise_error Exception
64
+ expect { retrier.run &block_exception }.to raise_error Exception
56
65
  end
57
66
 
58
67
  it 'runs an on_retry callback' do
59
68
  retrier = DefRetry::Retrier.new({
60
69
  on: Exception,
61
- on_retry: ->(e, n) { @did_retry = :yes }
62
- }, block_exception)
70
+ on_retry: ->(e, n) { @did_retry = :yes },
71
+ re_raise: false
72
+ })
63
73
 
64
- retrier.run
74
+ retrier.run &block_exception
65
75
  expect(@did_retry).to be :yes
66
76
  end
67
77
 
@@ -69,9 +79,9 @@ describe DefRetry::Retrier do
69
79
  retrier = DefRetry::Retrier.new({
70
80
  on: Exception,
71
81
  on_ensure: ->(r, n) { @did_ensure = :yes }
72
- }, -> {})
82
+ })
73
83
 
74
- retrier.run
84
+ retrier.run {}
75
85
  expect(@did_ensure).to be :yes
76
86
  end
77
87
 
@@ -81,10 +91,11 @@ describe DefRetry::Retrier do
81
91
  on_retry: ->(e, r) {
82
92
  @exception = e
83
93
  @retry_count = r
84
- }
85
- }, block_exception)
94
+ },
95
+ re_raise: false
96
+ })
86
97
 
87
- retrier.run
98
+ retrier.run &block_exception
88
99
  expect(@exception).to be_kind_of Exception
89
100
  expect(@retry_count).to be 3 # default limit
90
101
  end
@@ -96,9 +107,9 @@ describe DefRetry::Retrier do
96
107
  @value = v
97
108
  @retry_count = r
98
109
  }
99
- }, -> { :ran })
110
+ })
100
111
 
101
- retrier.run
112
+ retrier.run { :ran }
102
113
  expect(@value).to be :ran
103
114
  expect(@retry_count).to be 0 # default limit
104
115
  end
@@ -111,11 +122,12 @@ describe DefRetry::Retrier do
111
122
  sleep: :constant,
112
123
  # the 1st retry will sleep for 1 second
113
124
  # the 2nd retry will sleep for 1 second
114
- tries: 2
115
- }, block_exception)
125
+ tries: 2,
126
+ re_raise: false
127
+ })
116
128
 
117
129
  start_time = Time.now.to_i
118
- retrier.run
130
+ retrier.run &block_exception
119
131
  end_time = Time.now.to_i
120
132
 
121
133
  expect(end_time - start_time).to be 2
@@ -129,11 +141,12 @@ describe DefRetry::Retrier do
129
141
  sleep: :linear,
130
142
  # the 1st retry will sleep for 1 second
131
143
  # the 2nd retry will sleep for 2 seconds, and so on
132
- tries: 2
133
- }, block_exception)
144
+ tries: 2,
145
+ re_raise: false
146
+ })
134
147
 
135
148
  start_time = Time.now.to_i
136
- retrier.run
149
+ retrier.run &block_exception
137
150
  end_time = Time.now.to_i
138
151
 
139
152
  expect(end_time - start_time).to be 3
@@ -147,11 +160,12 @@ describe DefRetry::Retrier do
147
160
  sleep: :exponential,
148
161
  # the 1st retry will sleep for 1**2 == 1 second
149
162
  # the 2nd retry will sleep for 2**2 == 4 seconds
150
- tries: 2
151
- }, block_exception)
163
+ tries: 2,
164
+ re_raise: false
165
+ })
152
166
 
153
167
  start_time = Time.now.to_i
154
- retrier.run
168
+ retrier.run &block_exception
155
169
  end_time = Time.now.to_i
156
170
 
157
171
  expect(end_time - start_time).to be 5
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: def_retry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Salazar
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '10.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '10.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2.14'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2.14'
55
55
  description: Allows you to define methods that will retry on exception or declare
56
56
  blocks of code with retry.
57
57
  email: