2019-05-10 08:21:59 +00:00
|
|
|
package grace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2019-05-10 08:53:58 +00:00
|
|
|
func New() context.Context {
|
2019-05-10 08:21:59 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
log.Print("ctx: caught interrupt signal")
|
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|