cmd/ctrld: start service before restart on Windows

On Windows, calling s.Restart will fail if service is not running,
ensure ctrld is started before calling restart.
This commit is contained in:
Cuong Manh Le
2023-08-02 10:52:55 +07:00
committed by Cuong Manh Le
parent d3d2ed539f
commit 46509be8a0

View File

@@ -453,7 +453,7 @@ func initCLI() {
}
// On Linux, Darwin, Freebsd, ctrld set DNS on startup, because the DNS setting could be
// reset after rebooting. On windows, we only need to set once here. See prog.preRun in
// prog_*.go file for dedicated code on each platform.
// prog_*.go file for dedicated code on each platform. (1)
if runtime.GOOS == "windows" {
p.setDNS()
}
@@ -525,7 +525,10 @@ func initCLI() {
initLogging()
if doTasks([]task{{s.Stop, true}}) {
p.router.Cleanup()
p.resetDNS()
// See comment (1) in startCmd.
if runtime.GOOS != "windows" {
p.resetDNS()
}
mainLog.Load().Notice().Msg("Service stopped")
}
},
@@ -547,7 +550,15 @@ func initCLI() {
return
}
initLogging()
if doTasks([]task{{s.Restart, true}}) {
tasks := []task{{s.Restart, true}}
// On Windows, s.Restart will return error unless service is running.
if runtime.GOOS == "windows" {
tasks = []task{
{s.Start, false},
{s.Restart, true},
}
}
if doTasks(tasks) {
mainLog.Load().Notice().Msg("Service restarted")
}
},