Module: Puppet::Util::RetryAction

Defined in:
lib/puppet/util/retry_action.rb

Defined Under Namespace

Classes: RetryException

Class Method Summary collapse

Class Method Details

.retry_action(options = {}) { ... } ⇒ Object

Execute the supplied block retrying with exponential backoff.

Parameters:

  • options (Hash) (defaults to: {})

    the retry options

Options Hash (options):

  • :retries (FixNum)

    Maximum number of times to retry.

  • :retry_exceptions (Array<Exception>) — default: [StandardError]

    Optional array of exceptions that are allowed to be retried.

Yields:

  • The block to be executed.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet/util/retry_action.rb', line 13

def self.retry_action(options = {})
  # Retry actions for a specified amount of time. This method will allow the final
  # retry to complete even if that extends beyond the timeout period.
  if !block_given?
    raise RetryException::NoBlockGiven
  end

  retries = options[:retries]
  if retries.nil?
    raise RetryException::NoRetriesGiven
  end

  retry_exceptions = options[:retry_exceptions] || [StandardError]
  failures = 0
  begin
    yield
  rescue *retry_exceptions => e
    if failures >= retries
      raise RetryException::RetriesExceeded, _("%{retries} exceeded") % { retries: retries }, e.backtrace
    end

    Puppet.info(_("Caught exception %{klass}:%{error} retrying") % { klass: e.class, error: e })

    failures += 1

    # Increase the amount of time that we sleep after every
    # failed retry attempt.
    sleep (((2 ** failures) -1) * 0.1)

    retry

  end
end