Pull request 1988: 6158-fix-service-restart

Updates #6158.

Squashed commit of the following:

commit d474a7cb2cdf0a28cf2df320f9095a739241dcc3
Merge: 9c371bde4 53625d891
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 31 17:33:14 2023 +0300

    Merge branch 'master' into 6158-fix-service-restart

commit 9c371bde4b32eee7b5b36766095af3d579e14f2c
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 31 16:53:11 2023 +0300

    aghos: imp code

commit 2722d38ad51ea6b42caf81bbfc41206c95dac358
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 31 16:29:22 2023 +0300

    home: imp code more

commit 50de4c3379cf6e46e5a7cf287381d2b2c3b97127
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 31 16:20:18 2023 +0300

    home: imp code

commit dcba579fbe5cb82bea391bd08caee9df82c2618b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Aug 31 14:57:24 2023 +0300

    home: fix service restart for windows
This commit is contained in:
Stanislav Chzhen 2023-08-31 17:50:46 +03:00
parent 53625d8913
commit 0182b9ec18
5 changed files with 25 additions and 2 deletions

View File

@ -182,3 +182,8 @@ func IsReconfigureSignal(sig os.Signal) (ok bool) {
func IsShutdownSignal(sig os.Signal) (ok bool) { func IsShutdownSignal(sig os.Signal) (ok bool) {
return isShutdownSignal(sig) return isShutdownSignal(sig)
} }
// SendShutdownSignal sends the shutdown signal to the channel.
func SendShutdownSignal(c chan<- os.Signal) {
sendShutdownSignal(c)
}

View File

@ -37,3 +37,7 @@ func isShutdownSignal(sig os.Signal) (ok bool) {
return false return false
} }
} }
func sendShutdownSignal(_ chan<- os.Signal) {
// On Unix we are already notified by the system.
}

View File

@ -77,3 +77,7 @@ func isShutdownSignal(sig os.Signal) (ok bool) {
return false return false
} }
} }
func sendShutdownSignal(c chan<- os.Signal) {
c <- os.Interrupt
}

View File

@ -121,7 +121,7 @@ func Main(clientBuildFS fs.FS) {
}() }()
if opts.serviceControlAction != "" { if opts.serviceControlAction != "" {
handleServiceControlAction(opts, clientBuildFS, done) handleServiceControlAction(opts, clientBuildFS, signals, done)
return return
} }

View File

@ -33,6 +33,7 @@ const (
// daemon. // daemon.
type program struct { type program struct {
clientBuildFS fs.FS clientBuildFS fs.FS
signals chan os.Signal
done chan struct{} done chan struct{}
opts options opts options
} }
@ -55,6 +56,9 @@ func (p *program) Start(_ service.Service) (err error) {
func (p *program) Stop(_ service.Service) (err error) { func (p *program) Stop(_ service.Service) (err error) {
log.Info("service: stopping: waiting for cleanup") log.Info("service: stopping: waiting for cleanup")
aghos.SendShutdownSignal(p.signals)
// Wait for other goroutines to complete their job.
<-p.done <-p.done
return nil return nil
@ -195,7 +199,12 @@ func restartService() (err error) {
// - run: This is a special command that is not supposed to be used directly // - run: This is a special command that is not supposed to be used directly
// it is specified when we register a service, and it indicates to the app // it is specified when we register a service, and it indicates to the app
// that it is being run as a service/daemon. // that it is being run as a service/daemon.
func handleServiceControlAction(opts options, clientBuildFS fs.FS, done chan struct{}) { func handleServiceControlAction(
opts options,
clientBuildFS fs.FS,
signals chan os.Signal,
done chan struct{},
) {
// Call chooseSystem explicitly to introduce OpenBSD support for service // Call chooseSystem explicitly to introduce OpenBSD support for service
// package. It's a noop for other GOOS values. // package. It's a noop for other GOOS values.
chooseSystem() chooseSystem()
@ -229,6 +238,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS, done chan str
s, err := service.New(&program{ s, err := service.New(&program{
clientBuildFS: clientBuildFS, clientBuildFS: clientBuildFS,
signals: signals,
done: done, done: done,
opts: runOpts, opts: runOpts,
}, svcConfig) }, svcConfig)