Class: Subledger::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/subledger/exception_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ExceptionHandler

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



18
19
20
21
22
23
# 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

Instance Method Details

#with_retryObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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