Class: Tenax::Runner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tenax/runner.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Executes a block under a profile's resilience policy.

The Runner owns the retry loop, dispatches to the configured backoff strategy between attempts, classifies errors as retryable or terminal, and emits instrumentation events at each phase.

Users typically don't construct a Runner directly; Tenax.run does it.

Instance Method Summary collapse

Constructor Details

#initialize(profile, instrumentation: nil, sleeper: Kernel.method(:sleep), clock: Process.method(:clock_gettime)) ⇒ Runner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Runner.



17
18
19
20
21
22
# File 'lib/tenax/runner.rb', line 17

def initialize(profile, instrumentation: nil, sleeper: Kernel.method(:sleep), clock: Process.method(:clock_gettime))
  @profile = profile
  @instrumentation = instrumentation || Tenax.configuration.instrumentation
  @sleeper = sleeper
  @clock = clock
end

Instance Method Details

#call { ... } ⇒ Tenax::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute the block.

Yields:

  • the operation to execute

Returns:

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tenax/runner.rb', line 27

def call(&block)
  raise ArgumentError, "Tenax::Runner#call requires a block" unless block

  start_time = monotonic_now
  attempts = 0
  previous_delay = nil
  last_error = nil

  emit(:run_started, profile_name: @profile.name)

  while attempts < @profile.max_attempts
    attempts += 1
    emit(:attempt_started, profile_name: @profile.name, attempt: attempts)
    result, previous_delay, last_error =
      execute_attempt(block, attempts, start_time, previous_delay)
    return result if result
    break if last_error && attempts >= @profile.max_attempts
  end

  exhausted_result(last_error, attempts, start_time)
end