lib/retriable.rb in retriable-2.0.0.beta3 vs lib/retriable.rb in retriable-2.0.0.beta4

- old
+ new

@@ -14,48 +14,62 @@ def config @config ||= Config.new end - def retry( + def retriable( max_tries: config.max_tries, base_interval: config.base_interval, max_interval: config.max_interval, rand_factor: config.rand_factor, multiplier: config.multiplier, max_elapsed_time: config.max_elapsed_time, intervals: config.intervals, timeout: config.timeout, on: config.on, - on_retry: config.on_retry, - &block + on_retry: config.on_retry ) - raise LocalJumpError unless block_given? - start_time = Time.now elapsed_time = -> { Time.now - start_time } - max_tries = intervals.size if intervals - intervals ||= ExponentialBackoff.new( - max_tries: max_tries, - base_interval: base_interval, - multiplier: multiplier, - max_interval: max_interval, - rand_factor: rand_factor - ).intervals + if intervals + max_tries = intervals.size + else + intervals = ExponentialBackoff.new( + max_tries: max_tries, + base_interval: base_interval, + multiplier: multiplier, + max_interval: max_interval, + rand_factor: rand_factor + ).intervals + end + exception_list = on.kind_of?(Hash) ? on.keys : on + intervals.each.with_index(1) do |interval, attempt| begin if timeout - Timeout::timeout(timeout) { return block.call(attempt) } + Timeout::timeout(timeout) { return yield(attempt) } else - return block.call(attempt) + return yield(attempt) end - rescue *[*on] => exception - on_retry.call(exception, attempt, Time.now - start_time, interval) if on_retry + rescue *[*exception_list] => exception + if on.kind_of?(Hash) + patterns = [*on[exception.class]] + message_match = patterns.empty? ? true : false + patterns.each do |pattern| + if !!(exception.message =~ pattern) + message_match = true + break + end + end + raise unless message_match + end + + on_retry.call(exception, attempt, elapsed_time.call, interval) if on_retry raise if attempt >= max_tries || (elapsed_time.call + interval) > max_elapsed_time - sleep interval if interval > 0 && config.sleep_disabled != true + sleep interval if config.sleep_disabled != true end end end end