Module: Puppet::CloudPack::Utils
- Defined in:
- lib/puppet/cloudpack/utils.rb
Defined Under Namespace
Classes: RetryException
Class Method Summary collapse
- .retry_action(parameters = { :retry_exceptions => nil, :timeout => nil }) ⇒ Object
- .timedout?(start, timeout) ⇒ Boolean
Class Method Details
.retry_action(parameters = { :retry_exceptions => nil, :timeout => nil }) ⇒ Object
7 8 9 10 11 12 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 46 47 48 49 |
# File 'lib/puppet/cloudpack/utils.rb', line 7 def self.retry_action( parameters = { :retry_exceptions => nil, :timeout => nil } ) # 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. unless block_given? raise RetryException::NoBlockGiven end raise RetryException::NoTimeoutGiven if parameters[:timeout].nil? parameters[:retry_exceptions] ||= Hash.new start = Time.now failures = 0 begin yield rescue Exception => e # If we were giving exceptions to catch, # catch the excptions we care about and retry. # All others fail hard raise RetryException::Timeout if timedout?(start, parameters[:timeout]) retry_exceptions = parameters[:retry_exceptions].keys if (not retry_exceptions.empty?) and (retry_exceptions.include?(e.class) \ or retry_exceptions.include?(e.class.to_s) \ or retry_exceptions.include?(e.class.to_s.to_sym)) Puppet.info("Caught exception #{e.class}:#{e}") Puppet.info(parameters[:retry_exceptions][e.class]) elsif (not retry_exceptions.empty?) # If the exceptions is not in the list of retry_exceptions re-raise. raise e end failures += 1 # Increase the amount of time that we sleep after every # failed retry attempt. sleep (((2 ** failures) -1) * 0.1) retry end end |
.timedout?(start, timeout) ⇒ Boolean
51 52 53 54 |
# File 'lib/puppet/cloudpack/utils.rb', line 51 def self.timedout?(start, timeout) return true if timeout.nil? (Time.now - start) >= timeout end |