Sha256: acc533d6ecfb45ea6c09cbf2e59e335978cc5de5e2d9fd8c4fc55f541f796136

Contents?: true

Size: 1.89 KB

Versions: 107

Compression:

Stored size: 1.89 KB

Contents

require 'pact_broker/client/error'

module PactBroker
  module Client
    class Retry
      class RescuableError
        UNRESCUEABLE = [PactBroker::Client::Error]

        def self.===(e)
          case e
          when *UNRESCUEABLE then false
          else true
          end
        end
      end

      def self.while_error options = {}
        max_tries = options.fetch(:times, 3)
        tries = 0
        while true
          begin
            return yield
          rescue RescuableError => e
            tries += 1
            $stderr.puts "Error making request - #{e.class} #{e.message} #{e.backtrace.find{|l| l.include?('pact_broker-client')}}, attempt #{tries} of #{max_tries}"
            raise e if max_tries == tries
            sleep options.fetch(:sleep, 5)
          end
        end
      end

      def self.until_truthy_or_max_times options = {}
        max_tries = options.fetch(:times, 3)
        tries = 0
        verbose = options[:verbose]
        sleep_interval = options.fetch(:sleep, 5)
        sleep(sleep_interval) if options[:sleep_first]
        while true
          begin
            result = yield
            return result if max_tries < 2
            if options[:condition]
              condition_result = options[:condition].call(result)
              return result if condition_result
            else
              return result if result
            end
            tries += 1
            return result if max_tries == tries
            sleep sleep_interval
          rescue RescuableError => e
            tries += 1
            $stderr.puts "ERROR: Error making request - #{e.class} #{e.message} #{e.backtrace.find{|l| l.include?('pact_broker-client')}}, attempt #{tries} of #{max_tries}"
            raise e if max_tries == tries
            sleep sleep_interval
          end
        end
      end

      def self.sleep seconds
        Kernel.sleep seconds
      end
    end
  end
end

Version data entries

107 entries across 107 versions & 1 rubygems

Version Path
pact_broker-client-1.27.14 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.13 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.12 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.11 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.10 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.9 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.8 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.4 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.2 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.1 lib/pact_broker/client/retry.rb
pact_broker-client-1.27.0 lib/pact_broker/client/retry.rb
pact_broker-client-1.26.0 lib/pact_broker/client/retry.rb
pact_broker-client-1.25.1 lib/pact_broker/client/retry.rb
pact_broker-client-1.25.0 lib/pact_broker/client/retry.rb
pact_broker-client-1.24.0 lib/pact_broker/client/retry.rb
pact_broker-client-1.23.0 lib/pact_broker/client/retry.rb
pact_broker-client-1.22.3 lib/pact_broker/client/retry.rb
pact_broker-client-1.22.2 lib/pact_broker/client/retry.rb
pact_broker-client-1.22.1 lib/pact_broker/client/retry.rb
pact_broker-client-1.21.0 lib/pact_broker/client/retry.rb