Sha256: a6fe7246709050b14c511c2867a80013ddb6b9c64534038df713e6b715b34f31

Contents?: true

Size: 997 Bytes

Versions: 3

Compression:

Stored size: 997 Bytes

Contents

require 'timeout'

module NexusSW
  module LXD
    class ::Timeout::Retry < ::Timeout::Error
      def initialize(msg = nil)
        super msg if msg
      end
    end
    # Must specify :retry_interval in order to receive retries
    # And if so, then either :timeout or :retry_count must be specified
    #   :timeout == 0 without :retry_count is valid in this case, saying to retry forever
    # If nothing is specified, then this function is ineffectual and runs indefinitely
    def self.with_timeout_and_retries(options = {})
      Timeout.timeout(options[:timeout] || 0) do
        tries = 0
        loop do
          begin
            Timeout.timeout(options[:retry_interval] || 0, Timeout::Retry) do
              tries += 1
              return yield
            end
          rescue Timeout::Retry
            next if options[:retry_count] && (tries <= options[:retry_count])
            next if options[:timeout]
            raise
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lxd-common-0.6.0 lib/nexussw/lxd.rb
lxd-common-0.5.0 lib/nexussw/lxd.rb
lxd-common-0.4.1 lib/nexussw/lxd.rb