Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto retire with shutdown on Windows #505

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions wix/wrapper/wrapper_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ type handler struct {
// normal log: 2017/01/24 14:14:27 INFO <main> 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()
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
}
}
}
}
case <-exit:
Expand Down