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