You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|