2018-03-14 12:14:03 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ContextWithSignal creates a context canceled when SIGINT or SIGTERM are notified.
|
2018-03-14 12:14:03 +00:00
|
|
|
func ContextWithSignal(ctx context.Context) context.Context {
|
|
|
|
newCtx, cancel := context.WithCancel(ctx)
|
|
|
|
signals := make(chan os.Signal)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-signals:
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return newCtx
|
|
|
|
}
|