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.
crate/exegroup/signal.go

27 lines
455 B
Go

package exegroup
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
)
func HandleSignal(signals ...os.Signal) GoFunc {
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 {
defer signal.Stop(c)
select {
case sig := <-c:
return fmt.Errorf("signal %s", sig)
case <-ctx.Done():
return ctx.Err()
}
}
}