From 46509be8a0826dd84f6a91df9b4132925b4a9abf Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 2 Aug 2023 10:52:55 +0700 Subject: [PATCH] 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. --- cmd/ctrld/cli.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/ctrld/cli.go b/cmd/ctrld/cli.go index f9543aa..95ccb49 100644 --- a/cmd/ctrld/cli.go +++ b/cmd/ctrld/cli.go @@ -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") } },