Windows: start the service in a goroutine

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2023-05-23 12:59:27 +02:00
parent 2b77709a04
commit 255985b7b0
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
3 changed files with 27 additions and 11 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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)