Class: PactBroker::Client::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/client/retry.rb

Defined Under Namespace

Classes: RescuableError

Class Method Summary collapse

Class Method Details

.sleep(seconds) ⇒ Object



60
61
62
# File 'lib/pact_broker/client/retry.rb', line 60

def self.sleep seconds
  Kernel.sleep seconds
end

.until_truthy_or_max_times(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pact_broker/client/retry.rb', line 32

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

.while_error(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pact_broker/client/retry.rb', line 17

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