Returned NotifyContext approach.

This commit is contained in:
Revertron 2024-07-25 14:50:33 +02:00 committed by GitHub
parent 31f809034f
commit 7fd315168a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"crypto/ed25519" "crypto/ed25519"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@ -55,8 +56,9 @@ func main() {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) // Catch interrupts from the operating system to exit gracefully.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
// Create a new logger that logs output to stdout. // Create a new logger that logs output to stdout.
var logger *log.Logger var logger *log.Logger
@ -273,13 +275,13 @@ func main() {
//Windows service shutdown //Windows service shutdown
minwinsvc.SetOnExit(func() { minwinsvc.SetOnExit(func() {
logger.Infof("Shutting down service ...") logger.Infof("Shutting down service ...")
sigCh <- os.Interrupt cancel()
// Wait for all parts to shutdown properly // Wait for all parts to shutdown properly
<-done <-done
}) })
// Block until we are told to shut down. // Block until we are told to shut down.
<-sigCh <-ctx.Done()
// Shut down the node. // Shut down the node.
_ = n.admin.Stop() _ = n.admin.Stop()