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
# 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

  @max_attempts = policy_options[:max_attempts]
  @base_delay = policy_options[:base_delay]
  @multiplier = policy_options[:multiplier]
  @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



23
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
# File 'lib/chrono_machines/executor.rb', line 23

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

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

    # Calculate and execute delay with robust sleep
    delay = calculate_delay(attempts)
    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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chrono_machines/executor.rb', line 69

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