Class: ActiveMatrix::AgentManager
- Inherits:
-
Object
- Object
- ActiveMatrix::AgentManager
- Includes:
- Logging, Singleton
- Defined in:
- lib/active_matrix/agent_manager.rb
Overview
Manages the lifecycle of Matrix bot agents using async fibers
Constant Summary collapse
- OTEL_ATTRS =
OpenTelemetry semantic conventions for messaging
{ service: 'activematrix.agent_manager', messaging_system: 'matrix' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#initialize ⇒ AgentManager
constructor
A new instance of AgentManager.
-
#install_signal_handlers! ⇒ Object
Install signal handlers for graceful shutdown.
-
#pause_agent(agent) ⇒ Object
Pause an agent (keep it registered but stop processing).
-
#restart_agent(agent) ⇒ Object
Restart an agent.
-
#resume_agent(agent) ⇒ Object
Resume a paused agent.
-
#running? ⇒ Boolean
Check if currently running.
-
#start_agent(agent) ⇒ Object
Start a specific agent as an async task.
-
#start_agents(agents) ⇒ Object
Start specific agents (used by daemon workers).
-
#start_all ⇒ Object
Start all agents marked as active in the database This is the main entry point - runs the async reactor.
-
#status ⇒ Object
Get status of all agents.
-
#stop_agent(agent) ⇒ Object
Stop a specific agent.
-
#stop_all ⇒ Object
Stop all running agents.
Methods included from Logging
Constructor Details
#initialize ⇒ AgentManager
Returns a new instance of AgentManager.
22 23 24 25 26 27 28 |
# File 'lib/active_matrix/agent_manager.rb', line 22 def initialize @registry = AgentRegistry.instance @config = ActiveMatrix.config @barrier = nil @monitor_task = nil @running = false end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
20 21 22 |
# File 'lib/active_matrix/agent_manager.rb', line 20 def config @config end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
20 21 22 |
# File 'lib/active_matrix/agent_manager.rb', line 20 def registry @registry end |
Instance Method Details
#install_signal_handlers! ⇒ Object
Install signal handlers for graceful shutdown. Call this explicitly if you want the gem to handle SIGINT/SIGTERM. By default, signal handling is left to the host application.
33 34 35 36 37 38 39 40 |
# File 'lib/active_matrix/agent_manager.rb', line 33 def install_signal_handlers! %w[INT TERM].each do |signal| Signal.trap(signal) do stop_all exit # rubocop:disable Rails/Exit end end end |
#pause_agent(agent) ⇒ Object
Pause an agent (keep it registered but stop processing)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/active_matrix/agent_manager.rb', line 166 def pause_agent(agent) return false unless agent.may_pause? entry = @registry.get(agent.id) return false unless entry logger.info "Pausing agent: #{agent.name}" client = entry[:instance]&.client client&.stop_listener if client&.listening? agent.pause! true end |
#restart_agent(agent) ⇒ Object
Restart an agent
159 160 161 162 163 |
# File 'lib/active_matrix/agent_manager.rb', line 159 def restart_agent(agent) stop_agent(agent) sleep(1) # Brief pause start_agent(agent) end |
#resume_agent(agent) ⇒ Object
Resume a paused agent
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/active_matrix/agent_manager.rb', line 182 def resume_agent(agent) return false unless agent.paused? entry = @registry.get(agent.id) return false unless entry logger.info "Resuming agent: #{agent.name}" agent.resume! client = entry[:instance]&.client client&.start_listener agent.connection_established! true end |
#running? ⇒ Boolean
Check if currently running
209 210 211 |
# File 'lib/active_matrix/agent_manager.rb', line 209 def running? @running end |
#start_agent(agent) ⇒ Object
Start a specific agent as an async task
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_matrix/agent_manager.rb', line 84 def start_agent(agent) return false unless @running if @registry.running?(agent) logger.warn "Agent #{agent.name} is already running" return false end logger.info "Starting agent: #{agent.name}" begin agent.connect! task = @barrier.async do |subtask| subtask.annotate "agent-#{agent.name}" run_agent(agent) end # Store task reference for later control @registry.register_task(agent, task) true rescue StandardError => e logger.error "Failed to start agent #{agent.name}: #{e.}" agent.encounter_error! false end end |
#start_agents(agents) ⇒ Object
Start specific agents (used by daemon workers)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_matrix/agent_manager.rb', line 51 def start_agents(agents) return if @running @running = true agents_array = agents.respond_to?(:to_a) ? agents.to_a : agents logger.info "Starting #{agents_array.size} agents..." Sync do @barrier = Async::Barrier.new # Start the event router for routing Matrix events EventRouter.instance.start startup_delay = config.agent_startup_delay || 2 agents_array.each_with_index do |agent, index| sleep(startup_delay) if index.positive? start_agent(agent) end start_monitor_task logger.info "Started #{@registry.count} agents" # Wait for all agent tasks to complete (blocks until shutdown) @barrier.wait ensure @barrier&.stop @running = false end end |
#start_all ⇒ Object
Start all agents marked as active in the database This is the main entry point - runs the async reactor
44 45 46 47 |
# File 'lib/active_matrix/agent_manager.rb', line 44 def start_all agents = ActiveMatrix::Agent.where.not(state: :offline) start_agents(agents) end |
#status ⇒ Object
Get status of all agents
199 200 201 202 203 204 205 206 |
# File 'lib/active_matrix/agent_manager.rb', line 199 def status { running: @registry.count, agents: @registry.health_status, monitor_active: @monitor_task&.alive? || false, shutdown: !@running } end |
#stop_agent(agent) ⇒ Object
Stop a specific agent
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/active_matrix/agent_manager.rb', line 113 def stop_agent(agent) entry = @registry.get(agent.id) return false unless entry logger.info "Stopping agent: #{agent.name}" begin # Stop the client sync client = entry[:instance]&.client client&.stop_listener if client&.listening? # Save sync token agent.update(last_sync_token: client.sync_token) if client&.sync_token.present? # Stop the async task gracefully task = entry[:task] task&.stop # Update state agent.disconnect! if agent.may_disconnect? true rescue StandardError => e logger.error "Error stopping agent #{agent.name}: #{e.}" false end end |
#stop_all ⇒ Object
Stop all running agents
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/active_matrix/agent_manager.rb', line 142 def stop_all logger.info 'Stopping all agents...' # Stop monitor task @monitor_task&.stop # Stop event router EventRouter.instance.stop # Stop all agent tasks via barrier @barrier&.stop @running = false logger.info 'All agents stopped' end |