Class: Fractor::Workflow::RetryState

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/workflow/retry_config.rb

Overview

Tracks retry state for a job execution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_name) ⇒ RetryState

Returns a new instance of RetryState.



100
101
102
103
104
105
# File 'lib/fractor/workflow/retry_config.rb', line 100

def initialize(job_name)
  @job_name = job_name
  @attempt = 1
  @errors = []
  @started_at = Time.now
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



98
99
100
# File 'lib/fractor/workflow/retry_config.rb', line 98

def attempt
  @attempt
end

#errorsObject (readonly)

Returns the value of attribute errors.



98
99
100
# File 'lib/fractor/workflow/retry_config.rb', line 98

def errors
  @errors
end

#job_nameObject (readonly)

Returns the value of attribute job_name.



98
99
100
# File 'lib/fractor/workflow/retry_config.rb', line 98

def job_name
  @job_name
end

#started_atObject (readonly)

Returns the value of attribute started_at.



98
99
100
# File 'lib/fractor/workflow/retry_config.rb', line 98

def started_at
  @started_at
end

Instance Method Details

#exhausted?(max_attempts) ⇒ Boolean

Check if retry attempts have been exhausted

Parameters:

  • max_attempts (Integer)

    Maximum allowed attempts

Returns:

  • (Boolean)

    true if attempts exhausted



121
122
123
# File 'lib/fractor/workflow/retry_config.rb', line 121

def exhausted?(max_attempts)
  @attempt > max_attempts
end

#last_errorException?

Get the last error that occurred

Returns:

  • (Exception, nil)

    The last error or nil



127
128
129
# File 'lib/fractor/workflow/retry_config.rb', line 127

def last_error
  @errors.last&.dig(:error)
end

#record_failure(error) ⇒ Object

Record a failed attempt

Parameters:

  • error (Exception)

    The error that occurred



109
110
111
112
113
114
115
116
# File 'lib/fractor/workflow/retry_config.rb', line 109

def record_failure(error)
  @errors << {
    attempt: @attempt,
    error: error,
    timestamp: Time.now,
  }
  @attempt += 1
end

#summaryHash

Get a summary of all retry attempts

Returns:

  • (Hash)

    Summary data



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fractor/workflow/retry_config.rb', line 139

def summary
  {
    job_name: @job_name,
    total_attempts: @attempt - 1,
    total_time: total_time,
    errors: @errors.map do |err|
      {
        attempt: err[:attempt],
        error_class: err[:error].class.name,
        error_message: err[:error].message,
        timestamp: err[:timestamp],
      }
    end,
  }
end

#total_timeNumeric

Get total execution time across all attempts

Returns:

  • (Numeric)

    Total time in seconds



133
134
135
# File 'lib/fractor/workflow/retry_config.rb', line 133

def total_time
  Time.now - @started_at
end