Simplified Windows service shutdown.

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

View file

@ -11,7 +11,6 @@ import (
"os/signal" "os/signal"
"regexp" "regexp"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/gologme/log" "github.com/gologme/log"
@ -37,11 +36,6 @@ type node struct {
admin *admin.AdminSocket admin *admin.AdminSocket
} }
var (
sigCh = make(chan os.Signal, 1)
doneCh = make(chan struct{})
)
// The main function is responsible for configuring and starting Yggdrasil. // The main function is responsible for configuring and starting Yggdrasil.
func main() { func main() {
genconf := flag.Bool("genconf", false, "print a new config to stdout") genconf := flag.Bool("genconf", false, "print a new config to stdout")
@ -59,6 +53,9 @@ func main() {
loglevel := flag.String("loglevel", "info", "loglevel to enable") loglevel := flag.String("loglevel", "info", "loglevel to enable")
flag.Parse() flag.Parse()
done := make(chan struct{})
defer close(done)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Create a new logger that logs output to stdout. // Create a new logger that logs output to stdout.
@ -273,52 +270,22 @@ func main() {
} }
} }
//Windows service shutdown service //Windows service shutdown
minwinsvc.SetOnExit(func() { minwinsvc.SetOnExit(func() {
logger.Infof("Shutting down service ...") logger.Infof("Shutting down service ...")
sigCh <- os.Interrupt sigCh <- os.Interrupt
// Wait for all parts to shutdown properly // Wait for all parts to shutdown properly
<-doneCh <-done
}) })
// Block until we are told to shut down. // Block until we are told to shut down.
<-sigCh <-sigCh
// Shut down the node using a wait group to synchronize // Shut down the node.
var wg sync.WaitGroup _ = n.admin.Stop()
_ = n.multicast.Stop()
wg.Add(3) _ = n.tun.Stop()
go func() {
defer wg.Done()
if err := n.admin.Stop(); err != nil {
logger.Errorf("Error stopping admin: %v", err)
}
}()
go func() {
defer wg.Done()
if err := n.multicast.Stop(); err != nil {
logger.Errorf("Error stopping multicast: %v", err)
}
}()
go func() {
defer wg.Done()
if err := n.tun.Stop(); err != nil {
logger.Errorf("Error stopping tun: %v", err)
}
}()
// Stop the core synchronously since it's not in a goroutine
n.core.Stop() n.core.Stop()
// Wait for all goroutines to finish
wg.Wait()
// Notify that shutdown is complete
close(doneCh)
close(sigCh)
} }
func setLogLevel(loglevel string, logger *log.Logger) { func setLogLevel(loglevel string, logger *log.Logger) {