Class: SwarmSDK::Observer::Manager
- Inherits:
-
Object
- Object
- SwarmSDK::Observer::Manager
- Defined in:
- lib/swarm_sdk/observer/manager.rb
Overview
Manages observer agent executions
Handles:
- Event subscription via LogCollector
- Spawning async tasks for observer agents
- Self-consumption protection (observers don't trigger themselves)
- Task lifecycle and cleanup
Instance Method Summary collapse
-
#add_config(config) ⇒ void
Add an observer configuration.
-
#cleanup ⇒ void
Cleanup all subscriptions.
-
#initialize(swarm) ⇒ Manager
constructor
Initialize manager with swarm reference.
-
#setup ⇒ void
Setup event subscriptions for all observer configs.
-
#stop ⇒ void
Stop all observer tasks immediately.
-
#wait_for_completion ⇒ void
Wait for all observer tasks to complete.
Constructor Details
#initialize(swarm) ⇒ Manager
Initialize manager with swarm reference
24 25 26 27 28 29 30 |
# File 'lib/swarm_sdk/observer/manager.rb', line 24 def initialize(swarm) @swarm = swarm @configs = [] @subscription_ids = [] @barrier = nil @task_ids = {} end |
Instance Method Details
#add_config(config) ⇒ void
This method returns an undefined value.
Add an observer configuration
36 37 38 |
# File 'lib/swarm_sdk/observer/manager.rb', line 36 def add_config(config) @configs << config end |
#cleanup ⇒ void
This method returns an undefined value.
Cleanup all subscriptions
Unsubscribes from LogCollector to prevent memory leaks. Called by Executor.cleanup_after_execution.
96 97 98 99 |
# File 'lib/swarm_sdk/observer/manager.rb', line 96 def cleanup @subscription_ids.each { |id| LogCollector.unsubscribe(id) } @subscription_ids.clear end |
#setup ⇒ void
This method returns an undefined value.
Setup event subscriptions for all observer configs
Creates LogCollector subscriptions for each event type, filtered by type. Must be called after setup_logging() in Swarm.execute().
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/swarm_sdk/observer/manager.rb', line 46 def setup @barrier = Async::Barrier.new @configs.each do |config| config.event_handlers.each do |event_type, handler| sub_id = LogCollector.subscribe(filter: { type: event_type.to_s }) do |event| handle_event(config, handler, event) end @subscription_ids << sub_id end end end |
#stop ⇒ void
This method returns an undefined value.
Stop all observer tasks immediately
Interrupts in-flight observer LLM calls by stopping the barrier. Called during swarm interruption instead of wait_for_completion.
84 85 86 87 88 |
# File 'lib/swarm_sdk/observer/manager.rb', line 84 def stop @barrier&.stop rescue StandardError => e RubyLLM.logger.debug("SwarmSDK: Error stopping observer barrier: #{e.}") end |
#wait_for_completion ⇒ void
This method returns an undefined value.
Wait for all observer tasks to complete
Uses Async::Barrier.wait to wait for all spawned tasks. Handles errors gracefully without stopping other observers.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/swarm_sdk/observer/manager.rb', line 65 def wait_for_completion return unless @barrier # Wait for all tasks, handling errors gracefully # Barrier.wait re-raises first exception by default, so we use block form @barrier.wait do |task| task.wait rescue StandardError => error # Log but don't stop waiting for other observers RubyLLM.logger.error("Observer task failed: #{error.}") end end |