galched-bot/modules/grace/graceful.go
alexvanin 0507cdb3d1 Initial commit
This commit contains skeleton of the application with
uber/fx DI library.
2019-05-10 11:21:59 +03:00

26 lines
401 B
Go

package grace
import (
"context"
"log"
"os"
"os/signal"
"syscall"
)
func NewGracefulContext() context.Context {
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
}