Class: RubyReactor::RetryContext
- Inherits:
-
Object
- Object
- RubyReactor::RetryContext
- Defined in:
- lib/ruby_reactor/retry_context.rb
Overview
Tracks retry attempts and state for steps in async execution
Instance Attribute Summary collapse
-
#current_step ⇒ Object
Returns the value of attribute current_step.
-
#failure_reason ⇒ Object
Returns the value of attribute failure_reason.
-
#next_retry_at ⇒ Object
Returns the value of attribute next_retry_at.
-
#step_attempts ⇒ Object
Returns the value of attribute step_attempts.
Class Method Summary collapse
- .calculate_backoff_delay(attempt_number, backoff_strategy, base_delay) ⇒ Object
- .deserialize_from_retry(data) ⇒ Object
Instance Method Summary collapse
- #attempts_for_step(step_name) ⇒ Object
- #can_retry_step?(step_name, max_attempts) ⇒ Boolean
- #increment_attempt_for_step(step_name) ⇒ Object
-
#initialize ⇒ RetryContext
constructor
A new instance of RetryContext.
- #reset ⇒ Object
- #serialize_for_retry ⇒ Object
Constructor Details
#initialize ⇒ RetryContext
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_step ⇒ Object
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_reason ⇒ Object
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_at ⇒ Object
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_attempts ⇒ Object
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
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 |
#reset ⇒ Object
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_retry ⇒ Object
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 |