1
0
mirror of https://github.com/rancher/norman.git synced 2025-07-09 05:14:02 +00:00
norman/signal/sigterm.go

29 lines
433 B
Go
Raw Normal View History

2017-11-29 00:33:13 +00:00
package signal
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
)
func SigTermCancelContext(ctx context.Context) context.Context {
term := make(chan os.Signal)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-term:
logrus.Infof("Received SIGTERM, cancelling")
cancel()
case <-ctx.Done():
}
}()
return ctx
}