Class: GembirdBackend::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/gembird-backend/retry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#delayObject (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

Parameters:

  • n (Fixnum) (defaults to: 3)


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