module PatientlyTry
Constants
- VERSION
Public Instance Methods
patiently_try(opts = {}) { || ... }
click to toggle source
# File lib/patiently_try.rb, line 4 def patiently_try(opts = {}) opts = _parse_opts(opts) try = 0 begin yield rescue *(opts[:catch]) => e try += 1 _rescue_or_raise(e, try, opts) _wait(opts[:wait]) retry end end
Private Instance Methods
_exceeded_retries?(try, retries)
click to toggle source
# File lib/patiently_try.rb, line 63 def _exceeded_retries?(try, retries) try >= retries end
_log_backtrace(e)
click to toggle source
# File lib/patiently_try.rb, line 79 def _log_backtrace(e) puts e.backtrace.join("\n") end
_log_error(e)
click to toggle source
# File lib/patiently_try.rb, line 75 def _log_error(e) puts "Failed with: #{e.inspect}" end
_log_retry(try, retries)
click to toggle source
# File lib/patiently_try.rb, line 67 def _log_retry(try, retries) puts "Retrying (#{try}/#{retries})" end
_parse_opts(opts)
click to toggle source
# File lib/patiently_try.rb, line 49 def _parse_opts(opts) { catch: Array(opts[:catch] || StandardError), raise_if_caught: Array(opts[:raise_if_caught]), wait: opts[:wait].to_i, logging: opts[:logging].nil? ? true : opts[:logging], retries: opts[:retries] || 100 } end
_rescue_or_raise(e, try, opts)
click to toggle source
# File lib/patiently_try.rb, line 22 def _rescue_or_raise(e, try, opts) if opts[:logging] _rescue_or_raise_with_logging(e, try, opts) else _rescue_or_raise_without_logging(e, try, opts) end end
_rescue_or_raise_with_logging(e, try, opts)
click to toggle source
# File lib/patiently_try.rb, line 30 def _rescue_or_raise_with_logging(e, try, opts) _log_error(e) if opts[:logging] if _should_raise?(e, try, opts) _log_backtrace(e) if opts[:logging] raise e end _log_retry(try, opts[:retries]) if opts[:logging] end
_rescue_or_raise_without_logging(e, try, opts)
click to toggle source
# File lib/patiently_try.rb, line 41 def _rescue_or_raise_without_logging(e, try, opts) raise e if _should_raise?(e, try, opts) end
_rescued_but_excluded?(e, excluded_errors)
click to toggle source
# File lib/patiently_try.rb, line 71 def _rescued_but_excluded?(e, excluded_errors) excluded_errors.to_a.inject(false) { |excl, error| excl || e.is_a?(error) } end
_should_raise?(e, try, opts)
click to toggle source
# File lib/patiently_try.rb, line 45 def _should_raise?(e, try, opts) _exceeded_retries?(try, opts[:retries]) || _rescued_but_excluded?(e, opts[:raise_if_caught]) end
_wait(wait_time)
click to toggle source
# File lib/patiently_try.rb, line 59 def _wait(wait_time) sleep wait_time if wait_time > 0 end