Skip to content

Commit

Permalink
Merge pull request #505 from mattn/auto-retire
Browse files Browse the repository at this point in the history
Auto retire with shutdown on Windows
  • Loading branch information
daiksy committed Jul 25, 2018
2 parents 4961820 + 9d436e1 commit c667dc2
Showing 1 changed file with 19 additions and 0 deletions.
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

0 comments on commit c667dc2

Please sign in to comment.