4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/with_retries.rb', line 4
def with_retries(errors, params = {}, &block)
attempts = params[:attempts] or
raise ArgumentError.new("Attempts parameter not provided")
logger = params[:logger]
timeout = params[:timeout]
begin
yield
rescue *errors => e
attempts -= 1
Kernel.sleep(timeout) if timeout
if logger
times = (attempts == 1 ? "time" : "times")
logger.warn(
"Attempt failed. Retrying #{attempts} more #{times}...\n" +
["#{e.class}: #{e.message}:", *e.backtrace].join("\n ") +
"\n"
)
end
attempts > 0 ? retry : raise
end
end
|