Class: GembirdBackend::Retry
- Inherits:
-
Object
- Object
- GembirdBackend::Retry
- Defined in:
- lib/gembird-backend/retry.rb
Instance Attribute Summary collapse
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
Instance Method Summary collapse
-
#initialize(delay = true) ⇒ Retry
constructor
A new instance of Retry.
-
#try(n = 3, &block) ⇒ Object
try to make a successful call retry n times in case of error.
Constructor Details
#initialize(delay = true) ⇒ Retry
Returns a new instance of Retry.
4 5 6 |
# File 'lib/gembird-backend/retry.rb', line 4 def initialize(delay = true) @delay = delay end |
Instance Attribute Details
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
3 4 5 |
# File 'lib/gembird-backend/retry.rb', line 3 def delay @delay end |
Instance Method Details
#try(n = 3, &block) ⇒ Object
try to make a successful call retry n times in case of error
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gembird-backend/retry.rb', line 11 def try(n = 3, &block) try = lambda do begin block.call rescue ExecutionError => e GembirdBackend.logger.warn "retrying... #{e}" nil end end sleep_time = 0 n.times do result = try.call return result if result sleep (2 ** sleep_time) if delay sleep_time += 1 end nil end |