Class: EnhanceSwarm::RetryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/enhance_swarm/retry_handler.rb

Defined Under Namespace

Classes: RetryError

Class Method Summary collapse

Class Method Details

.retryable_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/enhance_swarm/retry_handler.rb', line 26

def self.retryable_error?(error)
  case error
  when CommandExecutor::CommandError
    # Retry on timeout or command not found, but not on validation errors
    error.message.include?('timed out') || error.message.include?('not found')
  when Errno::ENOENT, Errno::ECONNREFUSED, Errno::ETIMEDOUT
    true
  when IOError, SystemCallError
    true
  else
    false
  end
end

.with_retry(max_retries: 3, base_delay: 1, max_delay: 30, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enhance_swarm/retry_handler.rb', line 7

def self.with_retry(max_retries: 3, base_delay: 1, max_delay: 30, &block)
  retries = 0
  
  begin
    yield
  rescue StandardError => e
    retries += 1
    
    if retries <= max_retries && retryable_error?(e)
      delay = [base_delay * (2 ** (retries - 1)), max_delay].min
      puts "Attempt #{retries} failed: #{e.message}. Retrying in #{delay}s...".colorize(:yellow)
      sleep(delay)
      retry
    end
    
    raise RetryError.new("Operation failed after #{max_retries} retries: #{e.message}")
  end
end