Module: Invoca::Utils

Defined in:
lib/invoca/utils.rb,
lib/invoca/utils/diff.rb,
lib/invoca/utils/diff.rb,
lib/invoca/utils/version.rb,
lib/invoca/utils/exceptions.rb,
lib/invoca/utils/multi_sender.rb,
lib/invoca/utils/guaranteed_utf8_string.rb

Defined Under Namespace

Modules: Diffable Classes: Diff, GuaranteedUTF8String, MultiSender

Constant Summary collapse

VERSION =
"0.6.0"

Class Method Summary collapse

Class Method Details

.retry_on_exception(exception_classes, retries: 1, before_retry: nil) {|retries| ... } ⇒ Object

Yields and rescues any exceptions given in ‘exception_classes`, retrying the given number of times. The final retry does not rescue any exceptions. The try number (0..retries) is yielded as a block param.

Parameters:

  • exception_classes (Class or Array(Class))
    • exception()s) to rescue

  • retries: (Integer) (defaults to: 1)
    • 1+ count of retries (1 retry = up to 2 tries total)

  • before_retry (Proc) (defaults to: nil)
    • optional proc which is called before each retry, with the exception passed as a block param

Yields:

  • (retries)

Returns:

  • the value from yield



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/invoca/utils/exceptions.rb', line 14

def retry_on_exception(exception_classes, retries: 1, before_retry: nil)
  retries.times do |attempt_number|
    begin
      return yield(attempt_number)
    rescue *Array(exception_classes) => ex
      before_retry&.call(ex)
    end
  end

  yield(retries)   # no rescue for this last try, so any exceptions will raise out
end