Class: Bosh::Retryable

Inherits:
Object
  • Object
show all
Defined in:
lib/common/retryable.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Retryable

Returns a new instance of Retryable.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/common/retryable.rb', line 5

def initialize(options = {})
  opts = validate_options(options)

  @ensure_callback = opts[:ensure]
  @matching        = opts[:matching]
  @on_exception    = [opts[:on]].flatten
  @try_count       = 0
  @retry_exception = nil
  @retry_limit     = opts[:tries]
  @sleeper         = opts[:sleep]
end

Instance Method Details

#retryer(&block) ⇒ Object

this method will loop until the block returns a true value



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/common/retryable.rb', line 18

def retryer(&block)
  loop do
    @try_count += 1
    y = yield @try_count, @retry_exception
    @retry_exception = nil  # no exception was raised in the block
    return y if y
    raise Bosh::Common::RetryCountExceeded if @try_count >= @retry_limit
    wait
  end
rescue *@on_exception => exception
  raise unless exception.message =~ @matching
  raise if @try_count >= @retry_limit

  @retry_exception = exception
  wait
  retry
ensure
  @ensure_callback.call(@try_count)
end