mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
29 lines
433 B
Go
29 lines
433 B
Go
|
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
|
||
|
}
|