Class: Nanobot::Channels::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/nanobot/channels/manager.rb

Overview

ChannelManager orchestrates all channels

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, bus:, logger: nil, restart_delay: 5) ⇒ Manager

Returns a new instance of Manager.

Parameters:

  • config (Config) —

    channel configuration

  • bus (Bus::MessageBus) —

    message bus for pub/sub

  • logger (Logger, nil) (defaults to: nil) —

    optional logger

  • restart_delay (Integer) (defaults to: 5) —

    base delay in seconds between restart attempts



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

Parameters:



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

Parameters:

  • name (String) —

    channel name

Returns:

  • (Boolean)


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

Parameters:

  • name (String) —

    channel name

Returns:



78
79
80
# File 'lib/nanobot/channels/manager.rb', line 78

def get_channel(name)
  @channels[name]
end

#size ⇒ Integer

Get count of channels

Returns:

  • (Integer)


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 |message|
      channel.send(message)
    rescue StandardError => e
      @logger.error "Error sending message on #{name}: #{e.message}"
    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.message}"
  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