From 4f964bfe9df541fb8af561747e748cb38b8e9557 Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 26 Oct 2022 14:44:58 +0200 Subject: [PATCH 1/5] improve shutdown logs --- service/service.go | 12 +++--------- service/service_windows.go | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/service/service.go b/service/service.go index 5f807ad9..f3b80cce 100644 --- a/service/service.go +++ b/service/service.go @@ -38,7 +38,7 @@ import ( // HandleSignals manages OS signals that ask the service/daemon to stop. // The stopFunction should break the loop in the Beat so that -// the service shut downs gracefully. +// the service shutdowns gracefully. func HandleSignals(stopFunction func(), cancel context.CancelFunc) { var callback sync.Once logger := logp.NewLogger("service") @@ -49,20 +49,14 @@ func HandleSignals(stopFunction func(), cancel context.CancelFunc) { go func() { sig := <-sigc - switch sig { - case syscall.SIGINT, syscall.SIGTERM: - logger.Debug("Received sigterm/sigint, stopping") - case syscall.SIGHUP: - logger.Debug("Received sighup, stopping") - } - + logger.Infof("Received signal %q, stopping", sig) cancel() callback.Do(stopFunction) }() // Handle the Windows service events go ProcessWindowsControlEvents(func() { - logger.Debug("Received svc stop/shutdown request") + logger.Info("Received Windows SVC stop/shutdown request") callback.Do(stopFunction) }) } diff --git a/service/service_windows.go b/service/service_windows.go index be83c52e..e3228d5b 100644 --- a/service/service_windows.go +++ b/service/service_windows.go @@ -47,6 +47,7 @@ func (m *beatService) Execute(args []string, r <-chan svc.ChangeRequest, changes changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + log := logp.NewLogger("service_windows") loop: for c := range r { switch c.Cmd { @@ -55,18 +56,28 @@ loop: // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4 time.Sleep(100 * time.Millisecond) changes <- c.CurrentStatus - case svc.Stop, svc.Shutdown: + + // The svc.Cmd tye does not implement the Stringer interface and its + // underlying type is an integer, therefore it's needed to manually log them. + case svc.Stop: + log.Info("received state change 'svc.Stop' from windows service manager") + break + case svc.Shutdown: + log.Info("received state change 'svc.Shutdown' from windows service manager") break loop default: - logp.Err("Unexpected control request: $%d. Ignored.", c) + log.Errorf("Unexpected control request: $%d. Ignored.", c) } } changes <- svc.Status{State: svc.StopPending} + log.Info("changed windows service state to svc.StopPending, invoking stopCallback") m.stopCallback() + // Block until notifyWindowsServiceStopped below is called. This is required // as the windows/svc package will transition the service to STOPPED state // once this function returns. <-m.done + log.Debug("windows service state changed to svc.Stopped") return ssec, errno } @@ -104,7 +115,6 @@ func ProcessWindowsControlEvents(stopCallback func()) { serviceInstance.stopCallback = stopCallback err = run(os.Args[0], serviceInstance) - if err == nil { return } From 375f9319cd03ef0e9e5c9892530b91d691b29330 Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 26 Oct 2022 14:58:09 +0200 Subject: [PATCH 2/5] add entry to changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 335595ad..2ed719cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- service: Improve shutdown logs #87 + ### Deprecated ### Removed From d51e4d5b96f74d2f7986fc4cc6dc6ffeffa7cfaf Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 26 Oct 2022 15:13:57 +0200 Subject: [PATCH 3/5] make linter happy --- service/service_windows.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/service/service_windows.go b/service/service_windows.go index e3228d5b..2bdfef3c 100644 --- a/service/service_windows.go +++ b/service/service_windows.go @@ -61,10 +61,11 @@ loop: // underlying type is an integer, therefore it's needed to manually log them. case svc.Stop: log.Info("received state change 'svc.Stop' from windows service manager") - break + break loop case svc.Shutdown: log.Info("received state change 'svc.Shutdown' from windows service manager") break loop + default: log.Errorf("Unexpected control request: $%d. Ignored.", c) } @@ -100,7 +101,7 @@ const couldNotConnect syscall.Errno = 1063 func ProcessWindowsControlEvents(stopCallback func()) { defer close(serviceInstance.executeFinished) - // nolint: staticcheck // keep using the deprecated method in order to maintain the existing behavior + //nolint:staticcheck // keep using the deprecated method in order to maintain the existing behavior isInteractive, err := svc.IsAnInteractiveSession() if err != nil { logp.Err("IsAnInteractiveSession: %v", err) From 830806a2f23fd3d28e2885151845e476d356c2e1 Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 26 Oct 2022 17:26:13 +0200 Subject: [PATCH 4/5] another linting fix --- service/service_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/service_windows.go b/service/service_windows.go index 2bdfef3c..75876bd9 100644 --- a/service/service_windows.go +++ b/service/service_windows.go @@ -120,7 +120,7 @@ func ProcessWindowsControlEvents(stopCallback func()) { return } - // nolint: errorlint // this system error is a special case + //nolint:errorlint // this system error is a special case if errnoErr, ok := err.(syscall.Errno); ok && errnoErr == couldNotConnect { /* If, as in the case of Jenkins, the process is started as an interactive process, but the invoking process From 6fb74268b7dfc19cd059309aa5cd36a76ac442e6 Mon Sep 17 00:00:00 2001 From: Anderson Queiroz Date: Wed, 26 Oct 2022 22:54:50 +0200 Subject: [PATCH 5/5] Update service.go --- service/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/service.go b/service/service.go index f3b80cce..538f3fde 100644 --- a/service/service.go +++ b/service/service.go @@ -38,7 +38,7 @@ import ( // HandleSignals manages OS signals that ask the service/daemon to stop. // The stopFunction should break the loop in the Beat so that -// the service shutdowns gracefully. +// the service shuts down gracefully. func HandleSignals(stopFunction func(), cancel context.CancelFunc) { var callback sync.Once logger := logp.NewLogger("service")