Class: RubyReactor::RetryContext

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

Overview

Tracks retry attempts and state for steps in async execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRetryContext

Returns a new instance of RetryContext.



8
9
10
11
12
13
# File 'lib/ruby_reactor/retry_context.rb', line 8

def initialize
  @step_attempts = {}
  @current_step = nil
  @failure_reason = nil
  @next_retry_at = nil
end

Instance Attribute Details

#current_stepObject

Returns the value of attribute current_step.



6
7
8
# File 'lib/ruby_reactor/retry_context.rb', line 6

def current_step
  @current_step
end

#failure_reasonObject

Returns the value of attribute failure_reason.



6
7
8
# File 'lib/ruby_reactor/retry_context.rb', line 6

def failure_reason
  @failure_reason
end

#next_retry_atObject

Returns the value of attribute next_retry_at.



6
7
8
# File 'lib/ruby_reactor/retry_context.rb', line 6

def next_retry_at
  @next_retry_at
end

#step_attemptsObject

Returns the value of attribute step_attempts.



6
7
8
# File 'lib/ruby_reactor/retry_context.rb', line 6

def step_attempts
  @step_attempts
end

Class Method Details

.calculate_backoff_delay(attempt_number, backoff_strategy, base_delay) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_reactor/retry_context.rb', line 56

def self.calculate_backoff_delay(attempt_number, backoff_strategy, base_delay)
  case backoff_strategy
  when :exponential
    base_delay * (2**(attempt_number - 1))
  when :linear
    base_delay * attempt_number
  when :fixed
    base_delay
  else
    raise ArgumentError, "Unknown backoff strategy: #{backoff_strategy}"
  end
end

.deserialize_from_retry(data) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/ruby_reactor/retry_context.rb', line 47

def self.deserialize_from_retry(data)
  context = new
  context.step_attempts = data["step_attempts"] || {}
  context.current_step = data["current_step"]
  context.failure_reason = deserialize_error(data["failure_reason"])
  context.next_retry_at = data["next_retry_at"] ? Time.iso8601(data["next_retry_at"]) : nil
  context
end

Instance Method Details

#attempts_for_step(step_name) ⇒ Object



21
22
23
24
# File 'lib/ruby_reactor/retry_context.rb', line 21

def attempts_for_step(step_name)
  step_name = step_name.to_s
  @step_attempts[step_name] || 0
end

#can_retry_step?(step_name, max_attempts) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/ruby_reactor/retry_context.rb', line 26

def can_retry_step?(step_name, max_attempts)
  attempts = attempts_for_step(step_name)
  attempts < max_attempts
end

#increment_attempt_for_step(step_name) ⇒ Object



15
16
17
18
19
# File 'lib/ruby_reactor/retry_context.rb', line 15

def increment_attempt_for_step(step_name)
  step_name = step_name.to_s
  @step_attempts[step_name] ||= 0
  @step_attempts[step_name] += 1
end

#resetObject



31
32
33
34
35
36
# File 'lib/ruby_reactor/retry_context.rb', line 31

def reset
  @step_attempts = {}
  @current_step = nil
  @failure_reason = nil
  @next_retry_at = nil
end

#serialize_for_retryObject



38
39
40
41
42
43
44
45
# File 'lib/ruby_reactor/retry_context.rb', line 38

def serialize_for_retry
  {
    step_attempts: @step_attempts,
    current_step: @current_step,
    failure_reason: serialize_error(@failure_reason),
    next_retry_at: @next_retry_at&.iso8601
  }
end