Class: JetstreamBridge::RetryStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/core/retry_strategy.rb

Overview

Base retry strategy interface

Defined Under Namespace

Classes: RetryExhausted

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts:, backoffs: [], transient_errors: []) ⇒ RetryStrategy



16
17
18
19
20
# File 'lib/jetstream_bridge/core/retry_strategy.rb', line 16

def initialize(max_attempts:, backoffs: [], transient_errors: [])
  @max_attempts = max_attempts
  @backoffs = backoffs
  @transient_errors = transient_errors
end

Instance Method Details

#execute(context: nil) { ... } ⇒ Object

Execute block with retry logic

Yields:

  • Block to execute with retry

Raises:

  • RetryExhausted if all attempts fail



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jetstream_bridge/core/retry_strategy.rb', line 26

def execute(context: nil)
  attempts = 0
  last_error = nil

  loop do
    attempts += 1
    begin
      return yield
    rescue *retryable_errors => e
      last_error = e
      raise e if attempts >= @max_attempts

      delay = calculate_delay(attempts, e)
      log_retry(attempts, e, delay, context)
      sleep delay
    end
  end
rescue StandardError => e
  raise unless retryable?(e)

  raise RetryExhausted, "Failed after #{attempts} attempts: #{e.message}"
end