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
-
#try_max ⇒ Object
Maximum trys.
-
#try_sleep ⇒ Object
Initial amount of seconds to sleep between each try.
-
#try_sleep_increment ⇒ Object
Amount of seconds to increment sleep between each try.
-
#try_sleep_max ⇒ Object
Maxinum amount of seconds to sleep between each try.
Instance Method Summary collapse
-
#with_retry ⇒ Object
Yields: :try, n_try - for each attempt.
Instance Attribute Details
#try_max ⇒ Object
Maximum trys.
9 10 11 |
# File 'lib/asir/retry_behavior.rb', line 9 def try_max @try_max end |
#try_sleep ⇒ Object
Initial amount of seconds to sleep between each try.
11 12 13 |
# File 'lib/asir/retry_behavior.rb', line 11 def try_sleep @try_sleep end |
#try_sleep_increment ⇒ Object
Amount of seconds to increment sleep between each try.
13 14 15 |
# File 'lib/asir/retry_behavior.rb', line 13 def try_sleep_increment @try_sleep_increment end |
#try_sleep_max ⇒ Object
Maxinum amount of seconds to sleep between each try.
15 16 17 |
# File 'lib/asir/retry_behavior.rb', line 15 def try_sleep_max @try_sleep_max end |
Instance Method Details
#with_retry ⇒ Object
Yields:
:try, n_try - for each attempt.
:rescue, exc - for any rescued exception.
:retry, exc - before each retry.
:failed, last_exc - when too many retrys occurred.
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 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/asir/retry_behavior.rb', line 22 def with_retry n_try = 0 @try_sleep_value ||= ::ASIR::AdaptiveValue.new @try_sleep_value.init = try_sleep @try_sleep_value.reset! result = done = last_exception = nil begin n_try += 1 result = yield :try, n_try done = true rescue *Error::Unrecoverable.modules raise rescue *Error::Unforwardable.modules raise rescue ::Exception => exc last_exc = exc yield :rescue, exc if ! try_max || try_max > n_try yield :retry, exc if try_sleep sleep_secs = @try_sleep_value.value sleep sleep_secs if sleep_secs > 0 @try_sleep_value.init = try_sleep @try_sleep_value.add = try_sleep_increment @try_sleep_value.max = try_sleep_max @try_sleep_value.adapt! end retry end end unless done unless yield :failed, last_exc exc = last_exc raise RetryError, "Retry failed: #{exc.inspect}", exc.backtrace end end result end |