Class: CMDx::Retry
- Inherits:
-
Object
- Object
- CMDx::Retry
- Defined in:
- lib/cmdx/retry.rb
Overview
Manages retry logic and state for task execution.
The Retry class tracks retry availability, attempt counts, and remaining retries for a given task. It also resolves exception matching and computes wait times using configurable jitter strategies.
Instance Attribute Summary collapse
-
#task ⇒ Task
readonly
Returns the task instance associated with this retry.
Instance Method Summary collapse
-
#attempts ⇒ Integer
Returns the number of retry attempts already made.
-
#available ⇒ Integer
Returns the total number of retries configured for the task.
-
#available? ⇒ Boolean
Checks if the task has any retries configured.
-
#exception?(exception) ⇒ Boolean
Checks if the given exception matches any configured retry exception.
-
#exceptions ⇒ Array<Class>
Returns the list of exception classes eligible for retry.
-
#initialize(task) ⇒ Retry
constructor
Creates a new Retry instance for the given task.
-
#remaining ⇒ Integer
Returns the number of retries still available.
-
#remaining? ⇒ Boolean
Checks if there are retries still available.
-
#retried? ⇒ Boolean
Checks if the task has been retried at least once.
-
#wait ⇒ Float
Computes the wait time before the next retry attempt.
Constructor Details
#initialize(task) ⇒ Retry
Creates a new Retry instance for the given task.
31 32 33 |
# File 'lib/cmdx/retry.rb', line 31 def initialize(task) @task = task end |
Instance Attribute Details
#task ⇒ Task (readonly)
Returns the task instance associated with this retry.
19 20 21 |
# File 'lib/cmdx/retry.rb', line 19 def task @task end |
Instance Method Details
#attempts ⇒ Integer
Returns the number of retry attempts already made.
67 68 69 |
# File 'lib/cmdx/retry.rb', line 67 def attempts Integer(task.result.retries || 0) end |
#available ⇒ Integer
Returns the total number of retries configured for the task.
43 44 45 |
# File 'lib/cmdx/retry.rb', line 43 def available Integer(task.class.settings.retries || 0) end |
#available? ⇒ Boolean
Checks if the task has any retries configured.
55 56 57 |
# File 'lib/cmdx/retry.rb', line 55 def available? available.positive? end |
#exception?(exception) ⇒ Boolean
Checks if the given exception matches any configured retry exception.
132 133 134 |
# File 'lib/cmdx/retry.rb', line 132 def exception?(exception) exceptions.any? { |e| exception.class <= e } end |
#exceptions ⇒ Array<Class>
Returns the list of exception classes eligible for retry.
115 116 117 118 119 120 |
# File 'lib/cmdx/retry.rb', line 115 def exceptions @exceptions ||= Utils::Wrap.array( task.class.settings.retry_on || [StandardError, CMDx::TimeoutError] ) end |
#remaining ⇒ Integer
Returns the number of retries still available.
91 92 93 |
# File 'lib/cmdx/retry.rb', line 91 def remaining available - attempts end |
#remaining? ⇒ Boolean
Checks if there are retries still available.
103 104 105 |
# File 'lib/cmdx/retry.rb', line 103 def remaining? remaining.positive? end |
#retried? ⇒ Boolean
Checks if the task has been retried at least once.
79 80 81 |
# File 'lib/cmdx/retry.rb', line 79 def retried? attempts.positive? end |
#wait ⇒ Float
Computes the wait time before the next retry attempt.
Supports multiple jitter strategies: a Symbol calls a task method, a Proc is evaluated in the task instance context, a callable object receives the task and attempts, and a Numeric is multiplied by the attempt count.
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cmdx/retry.rb', line 151 def wait jitter = task.class.settings.retry_jitter if jitter.is_a?(Symbol) task.send(jitter, attempts) elsif jitter.is_a?(Proc) task.instance_exec(attempts, &jitter) elsif jitter.respond_to?(:call) jitter.call(task, attempts) else jitter.to_f * attempts end.to_f end |