Class: Nanobot::Channels::Manager
- Inherits:
-
Object
- Object
- Nanobot::Channels::Manager
- Defined in:
- lib/nanobot/channels/manager.rb
Overview
ChannelManager orchestrates all channels
Instance Attribute Summary collapse
-
#bus ⇒ Object
readonly
Returns the value of attribute bus.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#add_channel(channel) ⇒ Object
Add a channel.
-
#channel?(name) ⇒ Boolean
Check if a channel exists.
-
#get_channel(name) ⇒ BaseChannel?
Get a channel by name.
-
#initialize(config:, bus:, logger: nil, restart_delay: 5) ⇒ Manager
constructor
A new instance of Manager.
-
#size ⇒ Integer
Get count of channels.
-
#start_all ⇒ Object
Start all enabled channels.
-
#stop_all ⇒ Object
Stop all channels.
Constructor Details
#initialize(config:, bus:, logger: nil, restart_delay: 5) ⇒ Manager
Returns a new instance of Manager.
16 17 18 19 20 21 22 23 |
# File 'lib/nanobot/channels/manager.rb', line 16 def initialize(config:, bus:, logger: nil, restart_delay: 5) @config = config @bus = bus @logger = logger || Logger.new(IO::NULL) @channels = {} @threads = [] @restart_delay = restart_delay end |
Instance Attribute Details
#bus ⇒ Object (readonly)
Returns the value of attribute bus.
10 11 12 |
# File 'lib/nanobot/channels/manager.rb', line 10 def bus @bus end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
10 11 12 |
# File 'lib/nanobot/channels/manager.rb', line 10 def channels @channels end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/nanobot/channels/manager.rb', line 10 def logger @logger end |
Instance Method Details
#add_channel(channel) ⇒ Object
Add a channel
27 28 29 30 |
# File 'lib/nanobot/channels/manager.rb', line 27 def add_channel(channel) @channels[channel.name] = channel @logger.info "Added channel: #{channel.name}" end |
#channel?(name) ⇒ Boolean
Check if a channel exists
85 86 87 |
# File 'lib/nanobot/channels/manager.rb', line 85 def channel?(name) @channels.key?(name) end |
#get_channel(name) ⇒ BaseChannel?
Get a channel by name
78 79 80 |
# File 'lib/nanobot/channels/manager.rb', line 78 def get_channel(name) @channels[name] end |
#size ⇒ Integer
Get count of channels
91 92 93 |
# File 'lib/nanobot/channels/manager.rb', line 91 def size @channels.size end |
#start_all ⇒ Object
Start all enabled channels
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nanobot/channels/manager.rb', line 33 def start_all @channels.each do |name, channel| # Subscribe to outbound messages for this channel @bus.subscribe_outbound(name) do || channel.send() rescue StandardError => e @logger.error "Error sending message on #{name}: #{e.}" end # Start channel in separate thread with supervision thread = start_channel_with_supervision(name, channel, restart_delay: @restart_delay) @threads << thread end # Start bus dispatcher @bus.start_dispatch @logger.info 'All channels started' end |
#stop_all ⇒ Object
Stop all channels
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nanobot/channels/manager.rb', line 55 def stop_all @logger.info 'Stopping all channels' @channels.each do |name, channel| channel.stop rescue StandardError => e @logger.error "Error stopping #{name}: #{e.}" end # Stop bus @bus.stop # Wait for threads to finish @threads.each do |thread| thread.join(5) # Wait up to 5 seconds end @logger.info 'All channels stopped' end |