Class: Elbas::Retryable

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/elbas/retryable.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#info, #prefix

Constructor Details

#initializeRetryable

Returns a new instance of Retryable.



5
6
7
8
# File 'lib/elbas/retryable.rb', line 5

def initialize
  @max = 0
  @delay = 0
end

Class Method Details

.delay(seconds, &block) ⇒ Object



37
38
39
# File 'lib/elbas/retryable.rb', line 37

def self.delay(seconds, &block)
  new.tap { |r| r.delay seconds }
end

.times(max, &block) ⇒ Object



33
34
35
# File 'lib/elbas/retryable.rb', line 33

def self.times(max, &block)
  new.tap { |r| r.times max }
end

Instance Method Details

#delay(seconds, &block) ⇒ Object



15
16
17
18
# File 'lib/elbas/retryable.rb', line 15

def delay(seconds, &block)
  @delay = seconds
  run block if block_given?
end

#run(proc) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/elbas/retryable.rb', line 20

def run(proc)
  attempts ||= 0
  attempts += 1
  proc.call
rescue => e
  info "Rescued error in retryable action: #{e.message}"
  if attempts < @max
    info "Retrying in #{@delay} seconds..."
    sleep @delay
    retry
  end
end

#times(max, &block) ⇒ Object



10
11
12
13
# File 'lib/elbas/retryable.rb', line 10

def times(max, &block)
  @max = max
  run block if block_given?
end