retry_block 1.1.0 → 1.2.0
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/README.rdoc +21 -0
- data/lib/retry_block.rb +4 -2
- data/retry_block.gemspec +1 -1
- data/test/test_retry_block.rb +22 -0
- metadata +4 -4
data/README.rdoc
CHANGED
|
@@ -181,3 +181,24 @@ The output of the above code is:
|
|
|
181
181
|
Failed again! Sleeping 16 seconds ...
|
|
182
182
|
Failed again! Sleeping 16 seconds ...
|
|
183
183
|
Finished
|
|
184
|
+
|
|
185
|
+
=== Do NOT Catch This!
|
|
186
|
+
|
|
187
|
+
If you would like to write a catch-all, but would like to exclude certain
|
|
188
|
+
exceptions, you can specify the <tt>:do_not_catch</tt> option. The
|
|
189
|
+
following block of code will keep executing until it either completes
|
|
190
|
+
successfully or the <tt>Interrupt</tt> exception is raised, thus allowing a
|
|
191
|
+
user to easily quit the process with Ctrl-C.
|
|
192
|
+
|
|
193
|
+
retry_block :do_not_catch => Interrupt do
|
|
194
|
+
// do something useful
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
You may pass an exception or a list of exceptions to
|
|
198
|
+
<tt>:do_not_catch</tt>. Please note that, unlike the <tt>:catch</tt>
|
|
199
|
+
option, the <tt>:do_not_catch</tt> option must pass the exact classes that
|
|
200
|
+
it wishes to not catch. Subclasses will <i>not</i> match. For example,
|
|
201
|
+
passing <tt>Exception</tt> to <tt>:catch</tt> will catch all subclasses of
|
|
202
|
+
<tt>Exception</tt>. However, passing <tt>Exception</tt> to
|
|
203
|
+
<tt>:do_not_catch</tt> will ONLY match if <tt>Exception</tt> is raised, but
|
|
204
|
+
not any of its subclasses.
|
data/lib/retry_block.rb
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
require "retry_block/version"
|
|
2
|
-
|
|
3
1
|
module Kernel
|
|
4
2
|
def retry_block (opts={}, &block)
|
|
5
3
|
opts = {
|
|
6
4
|
:attempts => nil, # Number of times to try block. Nil means to retry forever until success
|
|
7
5
|
:sleep => 0, # Seconds to sleep between attempts
|
|
8
6
|
:catch => Exception, # An exception or array of exceptions to listen for
|
|
7
|
+
:do_not_catch => nil, # Do not catch the specified exception or list of exceptions.
|
|
9
8
|
:fail_callback => nil # Proc/lambda that gets executed between attempts
|
|
10
9
|
}.merge(opts)
|
|
11
10
|
|
|
12
11
|
opts[:catch] = [ opts[:catch] ].flatten
|
|
12
|
+
opts[:do_not_catch] = [ opts[:do_not_catch] ].flatten
|
|
13
13
|
attempts = 1
|
|
14
14
|
|
|
15
15
|
if (not opts[:attempts].nil?) and (not opts[:attempts].is_a? Integer or opts[:attempts] <= 0)
|
|
@@ -20,6 +20,8 @@ module Kernel
|
|
|
20
20
|
return yield attempts
|
|
21
21
|
rescue *opts[:catch] => exception
|
|
22
22
|
|
|
23
|
+
raise if opts[:do_not_catch].include? exception.class
|
|
24
|
+
|
|
23
25
|
# If a callable object was given for :fail_callback then call it
|
|
24
26
|
if opts[:fail_callback].respond_to? :call
|
|
25
27
|
callback_opts = [attempts, exception].slice(0, opts[:fail_callback].arity)
|
data/retry_block.gemspec
CHANGED
data/test/test_retry_block.rb
CHANGED
|
@@ -199,6 +199,28 @@ class TestRetryBlock < Test::Unit::TestCase
|
|
|
199
199
|
end
|
|
200
200
|
assert_equal 10, @run_count
|
|
201
201
|
end
|
|
202
|
+
|
|
203
|
+
def test_do_not_catch1
|
|
204
|
+
assert_raises(Interrupt) do
|
|
205
|
+
retry_block(:do_not_catch => Interrupt, :attempts => 2, :fail_callback => @fail_callback) do |attempt|
|
|
206
|
+
@run_count += 1
|
|
207
|
+
raise Interrupt if attempt == 1
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
assert_equal 1, @run_count
|
|
211
|
+
assert_equal 0, @fail_count
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def test_do_not_catch2
|
|
215
|
+
assert_raises(Interrupt) do
|
|
216
|
+
retry_block(:do_not_catch => [ArgumentError, Interrupt], :attempts => 2, :fail_callback => @fail_callback) do |attempt|
|
|
217
|
+
@run_count += 1
|
|
218
|
+
raise Interrupt if attempt == 1
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
assert_equal 1, @run_count
|
|
222
|
+
assert_equal 0, @fail_count
|
|
223
|
+
end
|
|
202
224
|
end
|
|
203
225
|
|
|
204
226
|
class TestException < Exception
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: retry_block
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 31
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
|
-
-
|
|
8
|
+
- 2
|
|
9
9
|
- 0
|
|
10
|
-
version: 1.
|
|
10
|
+
version: 1.2.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Alfred J. Fazio
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-11-
|
|
18
|
+
date: 2011-11-07 00:00:00 Z
|
|
19
19
|
dependencies: []
|
|
20
20
|
|
|
21
21
|
description: Take control of unstable or indeterminate code with retry_block
|