Class: EnhanceSwarm::SignalHandler

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

Class Method Summary collapse

Class Method Details

.handle_shutdown(signal) ⇒ Object



22
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
# File 'lib/enhance_swarm/signal_handler.rb', line 22

def self.handle_shutdown(signal)
  return if @shutdown_requested
  
  @shutdown_requested = true
  Logger.info("Received #{signal}, initiating graceful shutdown...")
  
  begin
    # Stop accepting new operations
    puts "\nšŸ›‘ Graceful shutdown initiated...".colorize(:yellow)
    
    # Clean up any active operations
    cleanup_active_operations
    
    # Perform final cleanup
    CleanupManager.cleanup_all_swarm_resources
    
    Logger.info("Graceful shutdown completed")
    puts "āœ… Shutdown complete".colorize(:green)
    
    exit(0)
  rescue StandardError => e
    Logger.error("Error during shutdown: #{e.message}")
    puts "āŒ Error during shutdown: #{e.message}".colorize(:red)
    exit(1)
  end
end

.handle_status_requestObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/enhance_swarm/signal_handler.rb', line 49

def self.handle_status_request
  Logger.info("Status request received via USR1")
  
  status = {
    active_operations: @active_operations.size,
    shutdown_requested: @shutdown_requested,
    timestamp: Time.now.iso8601
  }
  
  puts JSON.pretty_generate(status)
end

.register_operation(operation_id, details = {}) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/enhance_swarm/signal_handler.rb', line 61

def self.register_operation(operation_id, details = {})
  @active_operations ||= {}
  @active_operations[operation_id] = {
    started_at: Time.now,
    details: details
  }
end

.setupObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/enhance_swarm/signal_handler.rb', line 8

def self.setup
  @shutdown_requested = false
  @active_operations = {}
  
  # Handle graceful shutdown signals
  Signal.trap('INT') { handle_shutdown('SIGINT') }
  Signal.trap('TERM') { handle_shutdown('SIGTERM') }
  
  # Handle info signal for status
  if Signal.list.key?('USR1')
    Signal.trap('USR1') { handle_status_request }
  end
end

.shutdown_requested?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/enhance_swarm/signal_handler.rb', line 73

def self.shutdown_requested?
  @shutdown_requested
end

.unregister_operation(operation_id) ⇒ Object



69
70
71
# File 'lib/enhance_swarm/signal_handler.rb', line 69

def self.unregister_operation(operation_id)
  @active_operations&.delete(operation_id)
end