Class: ActiveMatrix::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/active_matrix/daemon.rb,
lib/active_matrix/daemon/worker.rb,
lib/active_matrix/daemon/probe_server.rb,
lib/active_matrix/daemon/signal_handler.rb

Overview

Main daemon coordinator for managing Matrix bot agents

Responsibilities:

  • Fork and manage worker processes

  • Distribute agents across workers

  • Handle signals (TERM, INT, HUP, USR1, USR2)

  • Run HTTP health probe server

  • Monitor worker health and restart on crash

Defined Under Namespace

Classes: ProbeServer, SignalHandler, Worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workers: 1, probe_port: 3042, probe_host: '127.0.0.1', agent_names: nil) ⇒ Daemon

Returns a new instance of Daemon.



21
22
23
24
25
26
27
28
29
# File 'lib/active_matrix/daemon.rb', line 21

def initialize(workers: 1, probe_port: 3042, probe_host: '127.0.0.1', agent_names: nil)
  @workers_count = workers
  @probe_port = probe_port
  @probe_host = probe_host
  @agent_names = agent_names
  @workers = {} # { pid => { index:, agent_ids: } }
  @running = false
  @start_time = nil
end

Instance Attribute Details

#agent_namesObject (readonly)

Returns the value of attribute agent_names.



19
20
21
# File 'lib/active_matrix/daemon.rb', line 19

def agent_names
  @agent_names
end

#probe_hostObject (readonly)

Returns the value of attribute probe_host.



19
20
21
# File 'lib/active_matrix/daemon.rb', line 19

def probe_host
  @probe_host
end

#probe_portObject (readonly)

Returns the value of attribute probe_port.



19
20
21
# File 'lib/active_matrix/daemon.rb', line 19

def probe_port
  @probe_port
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



19
20
21
# File 'lib/active_matrix/daemon.rb', line 19

def start_time
  @start_time
end

#workers_countObject (readonly)

Returns the value of attribute workers_count.



19
20
21
# File 'lib/active_matrix/daemon.rb', line 19

def workers_count
  @workers_count
end

Instance Method Details

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_matrix/daemon.rb', line 35

def run
  @start_time = Time.zone.now
  @running = true

  logger.info "Starting ActiveMatrix daemon (workers: #{workers_count}, probe: #{probe_host}:#{probe_port})"

  # Initialize OpenTelemetry if available
  logger.info 'OpenTelemetry tracing enabled' if Telemetry.configure!

  install_signal_handlers
  start_probe_server
  start_workers

  monitor_loop
ensure
  shutdown
end

#shutdownObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_matrix/daemon.rb', line 53

def shutdown
  return unless @running

  @running = false
  logger.info 'Shutting down ActiveMatrix daemon...'

  stop_probe_server
  stop_workers

  Telemetry.shutdown

  logger.info 'ActiveMatrix daemon stopped'
end

#statusObject



67
68
69
70
71
72
73
74
# File 'lib/active_matrix/daemon.rb', line 67

def status
  {
    status: @running ? 'ok' : 'stopping',
    uptime: @start_time ? (Time.zone.now - @start_time).to_i : 0,
    workers: @workers.size,
    agents: aggregate_agent_status
  }
end

#worker_pidsObject



31
32
33
# File 'lib/active_matrix/daemon.rb', line 31

def worker_pids
  @workers.keys
end