From 899f1a184479eb35115fa7a53def7ed65e14cab6 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 18 Jan 2021 21:46:26 +0100 Subject: [PATCH] improve windows service ensure to exit the service process in any case --- service/service_windows.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/service/service_windows.go b/service/service_windows.go index 6e2ee1ec..8c2238ea 100644 --- a/service/service_windows.go +++ b/service/service_windows.go @@ -74,6 +74,28 @@ func (s *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, chan if err := s.Service.Start(); err != nil { return true, 1 } + + wasStopped := make(chan bool, 1) + + go func() { + s.Service.Wait() + + select { + case <-wasStopped: + // the service was stopped nothing to do + logger.Debug(logSender, "", "Windows Service was stopped") + return + default: + // the server failed while running, we must be sure to exit the process. + // The defined recovery action will be executed. + logger.Debug(logSender, "", "Service wait ended, error: %v", s.Service.Error) + if s.Service.Error == nil { + os.Exit(0) + } else { + os.Exit(1) + } + } + }() changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} loop: for { @@ -85,6 +107,7 @@ loop: case svc.Stop, svc.Shutdown: logger.Debug(logSender, "", "Received service stop request") changes <- svc.Status{State: svc.StopPending} + wasStopped <- true s.Service.Stop() break loop case svc.ParamChange: @@ -237,17 +260,18 @@ func (s *WindowsService) Install(args ...string) error { recoveryActions := []mgr.RecoveryAction{ { Type: mgr.ServiceRestart, - Delay: 0, + Delay: 5 * time.Second, }, { Type: mgr.ServiceRestart, Delay: 60 * time.Second, }, { - Type: mgr.NoAction, + Type: mgr.ServiceRestart, + Delay: 90 * time.Second, }, } - err = service.SetRecoveryActions(recoveryActions, uint32(3600)) + err = service.SetRecoveryActions(recoveryActions, uint32(300)) if err != nil { service.Delete() return fmt.Errorf("unable to set recovery actions: %v", err)