Class: SenroUsecaser::RetryContext

Inherits:
Object
  • Object
show all
Defined in:
lib/senro_usecaser/retry_context.rb

Overview

Represents the context of a retry operation

This class tracks the state of retry attempts including:

  • Current attempt number
  • Maximum attempts allowed
  • Elapsed time since first attempt
  • Whether a retry should occur

Examples:

Basic usage in on_failure hook

on_failure do |input, result, context|
  if context.attempt < 3
    context.retry!(wait: 1.0)
  end
end

With modified input

on_failure do |input, result, context|
  if result.errors.first&.code == :rate_limited
    context.retry!(input: input.with_reduced_batch_size, wait: 5.0)
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_attempts: nil) ⇒ RetryContext

Initializes a new retry context

: (?max_attempts: Integer?) -> void



55
56
57
58
59
60
61
62
63
# File 'lib/senro_usecaser/retry_context.rb', line 55

def initialize(max_attempts: nil)
  @attempt = 1
  @max_attempts = max_attempts
  @started_at = Time.now
  @last_error = nil
  @should_retry = false
  @retry_input = nil
  @retry_wait = nil
end

Instance Attribute Details

#attempt ⇒ Object (readonly)

Returns the current attempt number (1-indexed) : () -> Integer



30
31
32
# File 'lib/senro_usecaser/retry_context.rb', line 30

def attempt
  @attempt
end

#last_error ⇒ Object (readonly)

Returns the error from the last failed attempt : () -> Error?



42
43
44
# File 'lib/senro_usecaser/retry_context.rb', line 42

def last_error
  @last_error
end

#max_attempts ⇒ Object (readonly)

Returns the maximum number of attempts allowed : () -> Integer?



34
35
36
# File 'lib/senro_usecaser/retry_context.rb', line 34

def max_attempts
  @max_attempts
end

#retry_input ⇒ Object (readonly)

Returns the input to use for the retry (nil means use original) : () -> untyped



46
47
48
# File 'lib/senro_usecaser/retry_context.rb', line 46

def retry_input
  @retry_input
end

#retry_wait ⇒ Object (readonly)

Returns the wait time before retrying : () -> (Float | Integer)?



50
51
52
# File 'lib/senro_usecaser/retry_context.rb', line 50

def retry_wait
  @retry_wait
end

#started_at ⇒ Object (readonly)

Returns the time when the first attempt started : () -> Time



38
39
40
# File 'lib/senro_usecaser/retry_context.rb', line 38

def started_at
  @started_at
end

Instance Method Details

#elapsed_time ⇒ Object

Returns the elapsed time since the first attempt

: () -> Float



75
76
77
# File 'lib/senro_usecaser/retry_context.rb', line 75

def elapsed_time
  Time.now - @started_at
end

#exhausted? ⇒ Boolean

Returns true if max_attempts has been reached

: () -> bool

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/senro_usecaser/retry_context.rb', line 82

def exhausted?
  return false unless @max_attempts

  @attempt >= @max_attempts
end

#increment!(last_error: nil) ⇒ Object

Increments the attempt counter and resets retry state Called internally between retry attempts

: (?last_error: Error?) -> void



117
118
119
120
121
# File 'lib/senro_usecaser/retry_context.rb', line 117

def increment!(last_error: nil)
  @attempt += 1
  @last_error = last_error
  reset_retry_state!
end

#reset_retry_state! ⇒ Object

Resets the retry request state Called internally after processing retry decision

: () -> void



127
128
129
130
131
# File 'lib/senro_usecaser/retry_context.rb', line 127

def reset_retry_state!
  @should_retry = false
  @retry_input = nil
  @retry_wait = nil
end

#retried? ⇒ Boolean

Returns true if this is a retry (attempt > 1)

: () -> bool

Returns:

  • (Boolean)


68
69
70
# File 'lib/senro_usecaser/retry_context.rb', line 68

def retried?
  @attempt > 1
end

#retry!(input: nil, wait: nil) ⇒ Object

Requests a retry with optional modified input and wait time

: (?input: untyped, ?wait: (Float | Integer)?) -> void

Examples:

Retry with default settings

context.retry!

Retry with wait time

context.retry!(wait: 2.0)

Retry with modified input

context.retry!(input: modified_input, wait: 1.0)


107
108
109
110
111
# File 'lib/senro_usecaser/retry_context.rb', line 107

def retry!(input: nil, wait: nil)
  @should_retry = true
  @retry_input = input
  @retry_wait = wait
end

#should_retry? ⇒ Boolean

Returns true if a retry has been requested

: () -> bool

Returns:

  • (Boolean)


91
92
93
# File 'lib/senro_usecaser/retry_context.rb', line 91

def should_retry?
  @should_retry
end