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
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.
-
#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.
-
#start_agent(agent) ⇒ Object
Start a specific agent.
-
#start_all ⇒ Object
Start all agents marked as active in the database.
-
#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.
13 14 15 16 17 18 19 20 |
# File 'lib/active_matrix/agent_manager.rb', line 13 def initialize @registry = AgentRegistry.instance @config = ActiveMatrix.config @shutdown = false @monitor_thread = nil setup_signal_handlers end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/active_matrix/agent_manager.rb', line 11 def config @config end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
11 12 13 |
# File 'lib/active_matrix/agent_manager.rb', line 11 def registry @registry end |
Instance Method Details
#pause_agent(agent) ⇒ Object
Pause an agent (keep it registered but stop processing)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/active_matrix/agent_manager.rb', line 159 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_thread if client.listening? agent.pause! true end |
#restart_agent(agent) ⇒ Object
Restart an agent
152 153 154 155 156 |
# File 'lib/active_matrix/agent_manager.rb', line 152 def restart_agent(agent) stop_agent(agent) sleep(1) # Brief pause start_agent(agent) end |
#resume_agent(agent) ⇒ Object
Resume a paused agent
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/active_matrix/agent_manager.rb', line 175 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_thread agent.connection_established! true end |
#start_agent(agent) ⇒ Object
Start a specific agent
39 40 41 42 43 44 45 46 47 48 49 50 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_matrix/agent_manager.rb', line 39 def start_agent(agent) return if @shutdown if @registry.running?(agent) logger.warn "Agent #{agent.name} is already running" return false end logger.info "Starting agent: #{agent.name}" begin # Update state agent.connect! # Create bot instance in a new thread thread = Thread.new do Thread.current.name = "agent-#{agent.name}" begin # Create client and bot instance client = create_client_for_agent(agent) bot_class = agent.bot_class.constantize bot_instance = bot_class.new(client) # Register the agent @registry.register(agent, bot_instance) # Authenticate if needed if agent.access_token.present? client.access_token = agent.access_token else client.login(agent.username, agent.password) agent.update(access_token: client.access_token) end # Restore sync token if available client.sync_token = agent.last_sync_token if agent.last_sync_token.present? # Mark as online agent.connection_established! # Start the sync loop client.start_listener_thread client.instance_variable_get(:@sync_thread).join rescue StandardError => e logger.error "Error in agent #{agent.name}: #{e.}" logger.error e.backtrace.join("\n") agent.encounter_error! raise ensure @registry.unregister(agent) agent.disconnect! if agent.may_disconnect? end end thread.abort_on_exception = true true rescue StandardError => e logger.error "Failed to start agent #{agent.name}: #{e.}" agent.encounter_error! false end end |
#start_all ⇒ Object
Start all agents marked as active in the database
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/active_matrix/agent_manager.rb', line 23 def start_all return if @shutdown logger.info 'Starting all active agents...' agents = defined?(MatrixAgent) ? MatrixAgent.where.not(state: :offline) : [] agents.each_with_index do |agent, index| sleep(config.agent_startup_delay || 2) if index.positive? start_agent(agent) end start_monitor_thread logger.info "Started #{@registry.count} agents" end |
#status ⇒ Object
Get status of all agents
192 193 194 195 196 197 198 199 |
# File 'lib/active_matrix/agent_manager.rb', line 192 def status { running: @registry.count, agents: @registry.health_status, monitor_active: @monitor_thread&.alive? || false, shutdown: @shutdown } end |
#stop_agent(agent) ⇒ Object
Stop a specific agent
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_matrix/agent_manager.rb', line 104 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_thread if client.listening? # Save sync token agent.update(last_sync_token: client.sync_token) if client.sync_token.present? # Kill the thread if still alive thread = entry[:thread] if thread&.alive? thread.kill thread.join(5) # Wait up to 5 seconds end # 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/active_matrix/agent_manager.rb', line 136 def stop_all logger.info 'Stopping all agents...' @shutdown = true # Stop monitor thread @monitor_thread&.kill # Stop all agents @registry.all_records.each do |agent| stop_agent(agent) end logger.info 'All agents stopped' end |