package exegroup import ( "context" "fmt" "os" "os/signal" "syscall" "git.blauwelle.com/go/crate/log" ) // HandleSignal 提供信号处理函数; func HandleSignal(signals ...os.Signal) func(ctx context.Context) error { c := make(chan os.Signal, 1) if len(signals) == 0 { signals = []os.Signal{syscall.SIGTERM, syscall.SIGINT} } signal.Notify(c, signals...) return func(ctx context.Context) error { log.Tracef(ctx, "notify signal %v", signals) defer signal.Stop(c) select { case sig := <-c: err := fmt.Errorf("signal received: %s", sig) log.Trace(ctx, err.Error()) return err case <-ctx.Done(): return ctx.Err() } } }