Class: ChronoMachines::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/chrono_machines/async_support.rb,
lib/chrono_machines/executor.rb

Overview

Patch the Executor's robust_sleep method to use Async's non-blocking sleep if an Async task is currently running.

Instance Method Summary collapse

Constructor Details

#initialize(policy_or_options = {}) ⇒ Executor

Returns a new instance of Executor.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chrono_machines/executor.rb', line 5

def initialize(policy_or_options = {})
  policy_options = if policy_or_options.is_a?(Symbol)
                     ChronoMachines.config.get_policy(policy_or_options)
                   else
                     ChronoMachines.config.get_policy(Configuration::DEFAULT_POLICY_NAME).merge(policy_or_options)
                   end

  @backoff_strategy = policy_options[:backoff_strategy] || :exponential
  @max_attempts = policy_options[:max_attempts]
  @base_delay = policy_options[:base_delay]
  @multiplier = policy_options[:multiplier] || 2
  @max_delay = policy_options[:max_delay]
  @jitter_factor = policy_options[:jitter_factor]
  @retryable_exceptions = policy_options[:retryable_exceptions]
  @on_failure = policy_options[:on_failure]
  @on_retry = policy_options[:on_retry]
  @on_success = policy_options[:on_success]
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chrono_machines/executor.rb', line 24

def call
  attempts = 0

  begin
    attempts += 1
    result = yield

    # Call success callback if defined
    @on_success&.call(result: result, attempts: attempts)

    result
  rescue StandardError => e
    # Check if exception is retryable
    unless @retryable_exceptions.any? { |ex| e.is_a?(ex) }
      # Non-retryable exception - call failure callback and re-raise
      handle_final_failure(e, attempts)
      raise e
    end

    # Check if we've exhausted all attempts
    if attempts >= @max_attempts
      handle_final_failure(e, attempts)
      raise MaxRetriesExceededError.new(e, attempts)
    end

    # Calculate delay
    delay = calculate_delay(attempts)

    # Call retry callback if defined
    @on_retry&.call(exception: e, attempt: attempts, next_delay: delay)

    # Execute delay with robust sleep
    robust_sleep(delay)
    retry
  end
end

#original_robust_sleepObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chrono_machines/async_support.rb', line 18

def robust_sleep(delay)
  # Handle potential interruptions to sleep
  # In Ruby 3.2+, Kernel.sleep is fiber-aware
  return if delay <= 0

  begin
    sleep(delay)
  rescue Interrupt
    # Re-raise interrupt signals
    raise
  rescue StandardError
    # Log or handle other sleep interruptions, but continue
    # In most cases, we want to proceed with the retry
  end
end

#robust_sleep(delay) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chrono_machines/executor.rb', line 113

def robust_sleep(delay)
  # Handle potential interruptions to sleep
  # In Ruby 3.2+, Kernel.sleep is fiber-aware
  return if delay <= 0

  begin
    sleep(delay)
  rescue Interrupt
    # Re-raise interrupt signals
    raise
  rescue StandardError
    # Log or handle other sleep interruptions, but continue
    # In most cases, we want to proceed with the retry
  end
end