瀏覽代碼

Windows: start the service in a goroutine

Signed-off-by: Nicola Murino <[email protected]>
Nicola Murino 2 年之前
父節點
當前提交
255985b7b0
共有 3 個文件被更改,包括 27 次插入11 次删除
  1. 5 1
      internal/dataprovider/mysql.go
  2. 5 1
      internal/dataprovider/pgsql.go
  3. 17 9
      internal/service/service_windows.go

+ 5 - 1
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

+ 5 - 1
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) {

+ 17 - 9
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)