Class: Nvoi::Utils::StepExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/nvoi/utils/retry.rb

Overview

StepExecutor executes deployment steps with automatic retry on transient failures

Instance Method Summary collapse

Constructor Details

#initialize(max_retries, log) ⇒ StepExecutor

Returns a new instance of StepExecutor.



7
8
9
10
# File 'lib/nvoi/utils/retry.rb', line 7

def initialize(max_retries, log)
  @max_retries = max_retries
  @log = log
end

Instance Method Details

#execute(step_name) ⇒ Object

Execute runs a step with automatic retry on retryable errors



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/nvoi/utils/retry.rb', line 13

def execute(step_name)
  last_error = nil

  @max_retries.times do |attempt|
    @log.info "Retry attempt %d/%d for: %s", attempt, @max_retries - 1, step_name if attempt > 0

    begin
      yield
      return # Success
    rescue StandardError => e
      last_error = e

      # Check if error is retryable
      unless retryable?(e)
        @log.error "Non-retryable error in %s: %s", step_name, e.message
        raise e
      end

      # Log retry with backoff
      if attempt < @max_retries - 1
        backoff_duration = exponential_backoff(attempt)
        @log.warning "Retryable error in %s: %s (backing off %ds)", step_name, e.message, backoff_duration
        sleep(backoff_duration)
      end
    end
  end

  raise Errors::DeploymentError.new(step_name, "max retries (#{@max_retries}) exceeded: #{last_error&.message}")
end