Module: PatientlyTry

Defined in:
lib/patiently_try.rb,
lib/patiently_try/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#patiently_try(opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/patiently_try.rb', line 4

def patiently_try(opts = {})
  retries      = opts[:retries] || 100
  wait         = opts[:wait] || 0
  catch_errors = opts[:catch] || StandardError
  logging      = opts[:logging].nil? ? true : opts[:logging]
  try          = 0

  begin
    yield
  rescue *(Array(catch_errors)) => e
    try += 1
    puts "Failed with: #{e.inspect}" if logging

    raise e if try >= retries

    puts "Retrying (#{try}/#{retries})"
    sleep wait if wait && wait > 0

    retry
  end
end