From 3e8b01d25a6d4107b924c51d40e3fbf549de5b27 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 13 Jul 2018 22:50:05 +0900 Subject: [PATCH 1/2] Auto retire with shutdown on Windows Do retire with shutdown if $MACKEREL_AUTORETIREMENT is set. --- wix/wrapper/wrapper_windows.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/wix/wrapper/wrapper_windows.go b/wix/wrapper/wrapper_windows.go index 110eaff05..8a04ed8bf 100644 --- a/wix/wrapper/wrapper_windows.go +++ b/wix/wrapper/wrapper_windows.go @@ -78,6 +78,13 @@ type handler struct { // normal log: 2017/01/24 14:14:27 INFO
Starting mackerel-agent version:0.36.0 var logRe = regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} (?:\S+\.go:\d+: )?([A-Z]+) `) +func (h *handler) retire() error { + dir := execdir() + cmd := exec.Command(filepath.Join(dir, "mackerel-agent.exe"), "retire", "--force") + cmd.Dir = dir + return cmd.Run() +} + func (h *handler) start() error { procAllocConsole.Call() dir := execdir() @@ -214,6 +221,11 @@ func (h *handler) stop() error { return nil } +func autoRetire() bool { + env := os.Getenv("MACKEREL_AUTO_RETIREMENT") + return env != "" && env != "0" +} + // implement https://godoc.org/golang.org/x/sys/windows/svc#Handler func (h *handler) Execute(args []string, r <-chan svc.ChangeRequest, s chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) { s <- svc.Status{State: svc.StartPending} @@ -251,6 +263,13 @@ L: if err := h.stop(); err != nil { h.elog.Error(stopEid, err.Error()) s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown} + } else { + if req.Cmd == svc.Shutdown && autoRetire() { + if err := h.retire(); err != nil { + h.elog.Error(stopEid, err.Error()) + s <- svc.Status{State: svc.Running, Accepts: svc.AcceptShutdown | svc.AcceptShutdown} + } + } } } case <-exit: From 9d436e17c3ef0f5c8feb20b4e5e640dcae6e76ce Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 23 Jul 2018 12:53:21 +0900 Subject: [PATCH 2/2] Remove duplicate --- wix/wrapper/wrapper_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wix/wrapper/wrapper_windows.go b/wix/wrapper/wrapper_windows.go index 8a04ed8bf..b45d21f23 100644 --- a/wix/wrapper/wrapper_windows.go +++ b/wix/wrapper/wrapper_windows.go @@ -267,7 +267,7 @@ L: if req.Cmd == svc.Shutdown && autoRetire() { if err := h.retire(); err != nil { h.elog.Error(stopEid, err.Error()) - s <- svc.Status{State: svc.Running, Accepts: svc.AcceptShutdown | svc.AcceptShutdown} + s <- svc.Status{State: svc.Running, Accepts: svc.AcceptShutdown} } } }