Class: CMDx::Retry

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Retry

Creates a new Retry instance for the given task.

Examples:

retry_instance = Retry.new(task)

Parameters:

  • task (Task)

    the task to manage retries for

Rbs:

  • (Task task) -> void



31
32
33
# File 'lib/cmdx/retry.rb', line 31

def initialize(task)
  @task = task
end

Instance Attribute Details

#taskTask (readonly)

Returns the task instance associated with this retry.

Examples:

retry_instance.task # => #<CreateUser ...>

Returns:

  • (Task)

    the task being retried

Rbs:

  • @task: Task



19
20
21
# File 'lib/cmdx/retry.rb', line 19

def task
  @task
end

Instance Method Details

#attemptsInteger

Returns the number of retry attempts already made.

Examples:

retry_instance.attempts # => 1

Returns:

  • (Integer)

    the current retry attempt count

Rbs:

  • () -> Integer



67
68
69
# File 'lib/cmdx/retry.rb', line 67

def attempts
  Integer(task.result.retries || 0)
end

#availableInteger

Returns the total number of retries configured for the task.

Examples:

retry_instance.available # => 3

Returns:

  • (Integer)

    the configured retry count

Rbs:

  • () -> Integer



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.

Examples:

retry_instance.available? # => true

Returns:

  • (Boolean)

    true if retries are configured

Rbs:

  • () -> bool



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.

Examples:

retry_instance.exception?(RuntimeError.new("fail")) # => true

Parameters:

  • exception (Exception)

    the exception to check

Returns:

  • (Boolean)

    true if the exception qualifies for retry

Rbs:

  • (Exception exception) -> bool



132
133
134
# File 'lib/cmdx/retry.rb', line 132

def exception?(exception)
  exceptions.any? { |e| exception.class <= e }
end

#exceptionsArray<Class>

Returns the list of exception classes eligible for retry.

Examples:

retry_instance.exceptions # => [StandardError, CMDx::TimeoutError]

Returns:

  • (Array<Class>)

    exception classes that trigger a retry

Rbs:



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

#remainingInteger

Returns the number of retries still available.

Examples:

retry_instance.remaining # => 2

Returns:

  • (Integer)

    the remaining retry count

Rbs:

  • () -> Integer



91
92
93
# File 'lib/cmdx/retry.rb', line 91

def remaining
  available - attempts
end

#remaining?Boolean

Checks if there are retries still available.

Examples:

retry_instance.remaining? # => true

Returns:

  • (Boolean)

    true if remaining retries exist

Rbs:

  • () -> bool



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.

Examples:

retry_instance.retried? # => true

Returns:

  • (Boolean)

    true if at least one retry has occurred

Rbs:

  • () -> bool



79
80
81
# File 'lib/cmdx/retry.rb', line 79

def retried?
  attempts.positive?
end

#waitFloat

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.

Examples:

With numeric jitter (0.5 * attempts)

retry_instance.wait # => 1.0

With symbol jitter referencing a task method

retry_instance.wait # => 2.5

Returns:

  • (Float)

    the wait duration in seconds

Rbs:

  • () -> Float



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