2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/reretryable.rb', line 2
def retryable(options = {}, &block)
opts = { :tries => 1,
:on => StandardError,
:sleep => 0,
:matching => /.*/ }.merge(options)
return nil if opts[:tries] == 0
retry_exception = [opts[:on]].flatten
tries = opts[:tries]
message_pattern = opts[:matching]
sleep_time = opts[:sleep]
begin
return yield
rescue *retry_exception => exception
raise unless exception.message =~ message_pattern
if (tries -= 1) > 0
sleep sleep_time
retry
end
end
yield
end
|