class Subledger::ExceptionHandler

Public Class Methods

new(args) click to toggle source

attempts seconds

 1      no point!
 2      0.15+
 3      0.35+
 4      0.75+
 5      1.55+
 6      3.15+
 7      6.35+
 8     12.75+
 9     25.55+ default max_attempts
10     51.15

attempts 11 and up are 2 seconds apart

# File lib/subledger/exception_handler.rb, line 18
def initialize args
  @name          = args[:name]
  @exception     = args[:exception]     || Exception
  @max_attempts  = args[:max_attempts]  || 10
  @initial_delay = args[:initial_delay] || 0
end

Public Instance Methods

with_retry() { || ... } click to toggle source
# File lib/subledger/exception_handler.rb, line 25
def with_retry

  attempts = 1
  total    = 0

  sleep @initial_delay

  begin
    yield
  rescue @exception => e
    if exception_fails_immediately?(e) or max_attempts?(attempts)
      raise e, "FAILURE: #{@name}, attempts: #{attempts}, total: #{total}, #{e}"
    elsif attempts < @max_attempts

      if attempts < 11
        delay = 2 ** attempts * 0.05
      else
        delay += 2
      end

      if attempts > 8
        LOG.warn "RETRY: #{@name}, attempts: #{attempts}, delay: #{delay}, #{e}"
      end

      sleep delay

      total    += delay
      attempts += 1

      retry
    end
  end
end

Private Instance Methods

exception_fails_immediately?(e) click to toggle source
# File lib/subledger/exception_handler.rb, line 65
def exception_fails_immediately? e
  name = e.class.name.to_s

  name =~ /^Subledger::/ or name =~ /ConditionalCheckFailedException/
end
max_attempts?(attempts) click to toggle source
# File lib/subledger/exception_handler.rb, line 61
def max_attempts? attempts
  attempts >= @max_attempts
end