mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-05 23:17:31 +08:00
feat: implement channel healthcheck and sentinel auto-healing
This commit is contained in:
@@ -17,6 +17,7 @@ type Channel interface {
|
||||
Send(ctx context.Context, msg bus.OutboundMessage) error
|
||||
IsRunning() bool
|
||||
IsAllowed(senderID string) bool
|
||||
HealthCheck(ctx context.Context) error
|
||||
}
|
||||
|
||||
type BaseChannel struct {
|
||||
|
||||
@@ -217,6 +217,31 @@ func (m *Manager) StopAll(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) CheckHealth(ctx context.Context) map[string]error {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
results := make(map[string]error)
|
||||
for name, channel := range m.channels {
|
||||
results[name] = channel.HealthCheck(ctx)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (m *Manager) RestartChannel(ctx context.Context, name string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
channel, ok := m.channels[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("channel %s not found", name)
|
||||
}
|
||||
|
||||
logger.InfoCF("channels", "Restarting channel", map[string]interface{}{"channel": name})
|
||||
_ = channel.Stop(ctx)
|
||||
return channel.Start(ctx)
|
||||
}
|
||||
|
||||
func (m *Manager) dispatchOutbound(ctx context.Context) {
|
||||
logger.InfoC("channels", "Outbound dispatcher started")
|
||||
|
||||
@@ -271,10 +296,7 @@ func (m *Manager) GetStatus() map[string]interface{} {
|
||||
|
||||
status := make(map[string]interface{})
|
||||
for name, channel := range m.channels {
|
||||
status[name] = map[string]interface{}{
|
||||
"enabled": true,
|
||||
"running": channel.IsRunning(),
|
||||
}
|
||||
status[name] = map[string]interface{}{}
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
@@ -74,6 +74,16 @@ func (c *TelegramChannel) SetTranscriber(transcriber *voice.GroqTranscriber) {
|
||||
c.transcriber = transcriber
|
||||
}
|
||||
|
||||
func (c *TelegramChannel) HealthCheck(ctx context.Context) error {
|
||||
if !c.IsRunning() {
|
||||
return fmt.Errorf("telegram bot not running")
|
||||
}
|
||||
hCtx, cancel := withTelegramAPITimeout(ctx)
|
||||
defer cancel()
|
||||
_, err := c.bot.GetMe(hCtx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *TelegramChannel) Start(ctx context.Context) error {
|
||||
if c.IsRunning() {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user