Class: ChronoMachines::Executor
- Inherits:
-
Object
- Object
- ChronoMachines::Executor
- 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
- #call ⇒ Object
-
#initialize(policy_or_options = {}) ⇒ Executor
constructor
A new instance of Executor.
- #original_robust_sleep ⇒ Object
- #robust_sleep(delay) ⇒ Object
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( = {}) = if .is_a?(Symbol) ChronoMachines.config.get_policy() else ChronoMachines.config.get_policy(Configuration::DEFAULT_POLICY_NAME).merge() end @max_attempts = [:max_attempts] @base_delay = [:base_delay] @multiplier = [:multiplier] @max_delay = [:max_delay] @jitter_factor = [:jitter_factor] @retryable_exceptions = [:retryable_exceptions] @on_failure = [:on_failure] @on_retry = [:on_retry] @on_success = [:on_success] end |
Instance Method Details
#call ⇒ Object
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_sleep ⇒ Object
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 |