diff --git a/pkg/event_handler/main.go b/pkg/event_handler/main.go index e819677..51a4e4b 100644 --- a/pkg/event_handler/main.go +++ b/pkg/event_handler/main.go @@ -27,14 +27,25 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig, Notifier: notifier, } go func() { - for event := range watcher.ResultChan() { - err2 := handler.Handle(ctx, &event) - if err2 != nil { - log.Printf("[event handler] failed to Handle workflow event %s", err2) // ERROR + defer func() { + log.Print("[event handler] shutting down, stopping watcher") + watcher.Stop() + }() + + for { + select { + case <-ctx.Done(): + log.Print("[event handler] context canceled, exiting") + return + case event, ok := <-watcher.ResultChan(): + if !ok { + log.Print("[event handler] result channel closed") + return + } + if err2 := handler.Handle(ctx, &event); err2 != nil { + log.Printf("[event handler] failed to handle workflow event: %v", err2) + } } } - log.Print("[event handler] stopped work, closing watcher") - watcher.Stop() - stop() }() }