Maintenance: use signal.NotifyContext
This commit is contained in:
@@ -60,6 +60,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM, nativeos.Interrupt)
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
logger := logging.New(logging.StdLog)
|
logger := logging.New(logging.StdLog)
|
||||||
@@ -75,26 +76,20 @@ func main() {
|
|||||||
errorCh <- _main(ctx, buildInfo, args, logger, os, osUser, unix, cli)
|
errorCh <- _main(ctx, buildInfo, args, logger, os, osUser, unix, cli)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
signalsCh := make(chan nativeos.Signal, 1)
|
|
||||||
signal.Notify(signalsCh,
|
|
||||||
syscall.SIGINT,
|
|
||||||
syscall.SIGTERM,
|
|
||||||
nativeos.Interrupt,
|
|
||||||
)
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case signal := <-signalsCh:
|
case <-ctx.Done():
|
||||||
logger.Warn("Caught OS signal %s, shutting down", signal)
|
stop()
|
||||||
|
logger.Warn("Caught OS signal, shutting down")
|
||||||
case err := <-errorCh:
|
case err := <-errorCh:
|
||||||
|
stop()
|
||||||
close(errorCh)
|
close(errorCh)
|
||||||
if err == nil { // expected exit such as healthcheck
|
if err == nil { // expected exit such as healthcheck
|
||||||
nativeos.Exit(0)
|
nativeos.Exit(0)
|
||||||
}
|
}
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
const shutdownGracePeriod = 5 * time.Second
|
const shutdownGracePeriod = 5 * time.Second
|
||||||
timer := time.NewTimer(shutdownGracePeriod)
|
timer := time.NewTimer(shutdownGracePeriod)
|
||||||
select {
|
select {
|
||||||
|
|||||||
Reference in New Issue
Block a user