From 255985b7b0e73b6f44005ea0b394b2e5d0ae2cf4 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 23 May 2023 12:59:27 +0200 Subject: [PATCH] Windows: start the service in a goroutine Signed-off-by: Nicola Murino --- internal/dataprovider/mysql.go | 6 +++++- internal/dataprovider/pgsql.go | 6 +++++- internal/service/service_windows.go | 26 +++++++++++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/dataprovider/mysql.go b/internal/dataprovider/mysql.go index 155feb4f..64b87140 100644 --- a/internal/dataprovider/mysql.go +++ b/internal/dataprovider/mysql.go @@ -243,7 +243,11 @@ func initializeMySQLProvider() error { dbHandle.SetConnMaxLifetime(240 * time.Second) dbHandle.SetConnMaxIdleTime(120 * time.Second) provider = &MySQLProvider{dbHandle: dbHandle} - return nil + + ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout) + defer cancel() + + return dbHandle.PingContext(ctx) } func getMySQLConnectionString(redactedPwd bool) (string, error) { var connectionString string diff --git a/internal/dataprovider/pgsql.go b/internal/dataprovider/pgsql.go index e24c714b..931ff4b7 100644 --- a/internal/dataprovider/pgsql.go +++ b/internal/dataprovider/pgsql.go @@ -264,7 +264,11 @@ func initializePGSQLProvider() error { dbHandle.SetConnMaxLifetime(240 * time.Second) dbHandle.SetConnMaxIdleTime(120 * time.Second) provider = &PGSQLProvider{dbHandle: dbHandle} - return nil + + ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout) + defer cancel() + + return dbHandle.PingContext(ctx) } func getPGSQLHostsAndPorts(configHost string, configPort int) (string, string) { diff --git a/internal/service/service_windows.go b/internal/service/service_windows.go index f4958483..066379f2 100644 --- a/internal/service/service_windows.go +++ b/internal/service/service_windows.go @@ -90,12 +90,12 @@ func (s *WindowsService) handleExit(wasStopped chan bool) { select { case <-wasStopped: // the service was stopped nothing to do - logger.Debug(logSender, "", "Windows Service was stopped") + logger.Info(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) + logger.Info(logSender, "", "Service wait ended, error: %v", s.Service.Error) if s.Service.Error == nil { os.Exit(0) } else { @@ -104,18 +104,26 @@ func (s *WindowsService) handleExit(wasStopped chan bool) { } } -func (s *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { - const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptParamChange | acceptRotateLog +func (s *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) { changes <- svc.Status{State: svc.StartPending} - if err := s.Service.Start(false); err != nil { - return true, 1 - } + + go func() { + if err := s.Service.Start(false); err != nil { + logger.Error(logSender, "", "Windows service failed to start, error: %v", err) + s.Service.Error = err + s.Service.Shutdown <- true + return + } + logger.Info(logSender, "", "Windows service started") + cmdsAccepted := svc.AcceptStop | svc.AcceptShutdown | svc.AcceptParamChange | acceptRotateLog + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + }() wasStopped := make(chan bool, 1) go s.handleExit(wasStopped) - changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown} loop: for { c := <-r @@ -296,7 +304,7 @@ func (s *WindowsService) Install(args ...string) error { Delay: 90 * time.Second, }, } - err = service.SetRecoveryActions(recoveryActions, uint32(300)) + err = service.SetRecoveryActions(recoveryActions, 300) if err != nil { service.Delete() return fmt.Errorf("unable to set recovery actions: %v", err)