Module: ASIR::RetryBehavior

Included in:
Channel, Transport::Retry
Defined in:
lib/asir/retry_behavior.rb

Overview

!SLIDE Generic retry behavior

Defined Under Namespace

Classes: RetryError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#try_maxObject

Maximum trys.



6
7
8
# File 'lib/asir/retry_behavior.rb', line 6

def try_max
  @try_max
end

#try_sleepObject

Initial amount of seconds to sleep between each try.



8
9
10
# File 'lib/asir/retry_behavior.rb', line 8

def try_sleep
  @try_sleep
end

#try_sleep_incrementObject

Amount of seconds to increment sleep between each try.



10
11
12
# File 'lib/asir/retry_behavior.rb', line 10

def try_sleep_increment
  @try_sleep_increment
end

#try_sleep_maxObject

Maxinum amount of seconds to sleep between each try.



12
13
14
# File 'lib/asir/retry_behavior.rb', line 12

def try_sleep_max
  @try_sleep_max
end

Instance Method Details

#with_retryObject

Yields:

:try, n_try
:rescue, exc
:retry, exc
:failed, nil


19
20
21
22
23
24
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
# File 'lib/asir/retry_behavior.rb', line 19

def with_retry
  n_try = 0
  sleep_secs = try_sleep
  result = done = last_exception = nil
  begin
    n_try += 1
    result = yield :try, n_try
    done = true
  rescue *Error::Unforwardable.unforwardable => exc
    raise
  rescue ::Exception => exc
    last_exception = exc
    yield :rescue, exc
    if ! try_max || try_max > n_try
      yield :retry, exc
      if sleep_secs
        sleep sleep_secs if sleep_secs > 0
        sleep_secs += try_sleep_increment if try_sleep_increment
        sleep_secs = try_sleep_max if try_sleep_max && sleep_secs > try_sleep_max
      end
      retry
    end
  end
  unless done
    unless yield :failed, last_exception
      exc = last_exception
      raise RetryError, "Retry failed: #{exc.inspect}", exc.backtrace
    end
  end
  result
end