Module: Gitlab::Triage::Retryable

Included in:
RestAPINetwork
Defined in:
lib/gitlab/triage/retryable.rb

Constant Summary collapse

MAX_RETRIES =
3
RETRY_WAIT_SECONDS =
10
BACK_OFF_SECONDS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#triesObject

Returns the value of attribute tries.



10
11
12
# File 'lib/gitlab/triage/retryable.rb', line 10

def tries
  @tries
end

Instance Method Details

#execute_with_retry(exception_types: [StandardError], backoff_exceptions: [], debug: false) ⇒ Object



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
# File 'lib/gitlab/triage/retryable.rb', line 12

def execute_with_retry(exception_types: [StandardError], backoff_exceptions: [], debug: false)
  @tries = 0

  until maximum_retries_reached?
    begin
      @tries += 1
      return yield
    rescue *exception_types => e
      base_message = "exception - %s, waiting #{RETRY_WAIT_SECONDS} secs)"

      if maximum_retries_reached?
        puts_execute_with_retry_message(e, format(base_message, "gave up, tried #{MAX_RETRIES} times")) if debug
        raise
      else
        puts_execute_with_retry_message(e, format(base_message, "retrying #{@tries}/#{MAX_RETRIES} times")) if debug
        sleep(RETRY_WAIT_SECONDS)
      end
    rescue *backoff_exceptions => e
      base_message = "backoff - %s, waiting #{BACK_OFF_SECONDS} secs)"

      if maximum_retries_reached?
        puts_execute_with_retry_message(e, format(base_message, "gave up, tried #{MAX_RETRIES} times")) if debug
        raise
      else
        puts_execute_with_retry_message(e, format(base_message, "retrying #{@tries}/#{MAX_RETRIES} times")) if debug
        sleep(BACK_OFF_SECONDS)
      end
    end
  end
end

#maximum_retries_reached?Boolean (private)

Returns:

  • (Boolean)


45
46
47
# File 'lib/gitlab/triage/retryable.rb', line 45

def maximum_retries_reached?
  tries == MAX_RETRIES
end

#puts_execute_with_retry_message(exception, message) ⇒ Object (private)



49
50
51
# File 'lib/gitlab/triage/retryable.rb', line 49

def puts_execute_with_retry_message(exception, message)
  puts Gitlab::Triage::UI.debug "execute_with_retry: #{exception} (#{message}"
end