mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-25 17:10:28 +00:00
improve windows service
ensure to exit the service process in any case
This commit is contained in:
parent
41a1af863e
commit
899f1a1844
1 changed files with 27 additions and 3 deletions
|
@ -74,6 +74,28 @@ func (s *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, chan
|
||||||
if err := s.Service.Start(); err != nil {
|
if err := s.Service.Start(); err != nil {
|
||||||
return true, 1
|
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}
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
||||||
loop:
|
loop:
|
||||||
for {
|
for {
|
||||||
|
@ -85,6 +107,7 @@ loop:
|
||||||
case svc.Stop, svc.Shutdown:
|
case svc.Stop, svc.Shutdown:
|
||||||
logger.Debug(logSender, "", "Received service stop request")
|
logger.Debug(logSender, "", "Received service stop request")
|
||||||
changes <- svc.Status{State: svc.StopPending}
|
changes <- svc.Status{State: svc.StopPending}
|
||||||
|
wasStopped <- true
|
||||||
s.Service.Stop()
|
s.Service.Stop()
|
||||||
break loop
|
break loop
|
||||||
case svc.ParamChange:
|
case svc.ParamChange:
|
||||||
|
@ -237,17 +260,18 @@ func (s *WindowsService) Install(args ...string) error {
|
||||||
recoveryActions := []mgr.RecoveryAction{
|
recoveryActions := []mgr.RecoveryAction{
|
||||||
{
|
{
|
||||||
Type: mgr.ServiceRestart,
|
Type: mgr.ServiceRestart,
|
||||||
Delay: 0,
|
Delay: 5 * time.Second,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Type: mgr.ServiceRestart,
|
Type: mgr.ServiceRestart,
|
||||||
Delay: 60 * time.Second,
|
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 {
|
if err != nil {
|
||||||
service.Delete()
|
service.Delete()
|
||||||
return fmt.Errorf("unable to set recovery actions: %v", err)
|
return fmt.Errorf("unable to set recovery actions: %v", err)
|
||||||
|
|
Loading…
Reference in a new issue