fix notification dispatcher send on closed channel after disabling from config

This commit is contained in:
yusing 2024-12-21 04:13:33 +08:00
parent 751594860a
commit 1042d12df6

View file

@ -37,7 +37,7 @@ const dispatchErr = "notification dispatch error"
func StartNotifDispatcher(parent *task.Task) *Dispatcher { func StartNotifDispatcher(parent *task.Task) *Dispatcher {
dispatcher = &Dispatcher{ dispatcher = &Dispatcher{
task: parent.Subtask("notification dispatcher"), task: parent.Subtask("notification"),
logCh: make(chan *LogMessage), logCh: make(chan *LogMessage),
providers: F.NewSet[Provider](), providers: F.NewSet[Provider](),
} }
@ -79,23 +79,27 @@ func (disp *Dispatcher) RegisterProvider(cfg types.NotificationConfig) (Provider
func (disp *Dispatcher) start() { func (disp *Dispatcher) start() {
defer func() { defer func() {
dispatcher = nil
disp.providers.Clear() disp.providers.Clear()
close(disp.logCh) close(disp.logCh)
disp.task.Finish("dispatcher stopped") disp.task.Finish(nil)
}() }()
for { for {
select { select {
case <-disp.task.Context().Done(): case <-disp.task.Context().Done():
return return
case msg := <-disp.logCh: case msg, ok := <-disp.logCh:
if !ok {
return
}
go disp.dispatch(msg) go disp.dispatch(msg)
} }
} }
} }
func (disp *Dispatcher) dispatch(msg *LogMessage) { func (disp *Dispatcher) dispatch(msg *LogMessage) {
task := disp.task.Subtask("dispatch notif") task := disp.task.Subtask("dispatcher")
defer task.Finish("notif dispatched") defer task.Finish("notif dispatched")
errs := E.NewBuilder(dispatchErr) errs := E.NewBuilder(dispatchErr)