module Puppet::Util::RetryAction

Public Class Methods

retry_action(options = {}) { || ... } click to toggle source

Execute the supplied block retrying with exponential backoff.

@param [Hash] options the retry options @option options [Integer] :retries Maximum number of times to retry. @option options [Array<Exception>] :retry_exceptions ([StandardError]) Optional array of exceptions that are allowed to be retried. @yield The block to be executed.

   # File lib/puppet/util/retry_action.rb
14 def self.retry_action(options = {})
15   # Retry actions for a specified amount of time. This method will allow the final
16   # retry to complete even if that extends beyond the timeout period.
17   if !block_given?
18     raise RetryException::NoBlockGiven
19   end
20 
21   retries = options[:retries]
22   if retries.nil?
23     raise RetryException::NoRetriesGiven
24   end
25 
26   retry_exceptions = options[:retry_exceptions] || [StandardError]
27   failures = 0
28   begin
29     yield
30   rescue *retry_exceptions => e
31     if failures >= retries
32       raise RetryException::RetriesExceeded, _("%{retries} exceeded") % { retries: retries }, e.backtrace
33     end
34 
35     Puppet.info(_("Caught exception %{klass}:%{error} retrying") % { klass: e.class, error: e })
36 
37     failures += 1
38 
39     # Increase the amount of time that we sleep after every
40     # failed retry attempt.
41     sleep (((2 ** failures) -1) * 0.1)
42 
43     retry
44 
45   end
46 end