-
Notifications
You must be signed in to change notification settings - Fork 90
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since in this case mackerel-agent itself has been stopped successfully, status reported here should not be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If mackerel-agent did not accept the shutdown command unintentionally, how should behave? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, This part report status of current condition. If this return Running, service manager will send svc.Shutdown again in later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's better to retry retiring, but should not block the shutdown process of Windows too long.
I didn't know that. Thank you to inform. Overall it looks good to set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows service manager has a fixed timeout value (20 seconds) to wait for shutdown. When it goes over 20 seconds, the OS forcibly terminates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Understand. Thank you for explanation! From my comment #505 (comment):
What do you think about this? I imagine following process may happen:
In this case, will mackerel-agent service be "running" but agent is stopped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there seems to be more falls possibles.
I would like to make these be same as UNIX's one as possible. When failed to spawn The case that shutdown is canceled (3), the session can only be used during the time-out period. So not a really cancelation. So I'm thinking terminating mackerel-agent is right thinkg. 4 possibly be occured on some environment. But we don't have idea for recover. Either way, mackerel-agent will be detected errors by server caused by regularly posts stopped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, understand. |
||
} | ||
} | ||
} | ||
} | ||
case <-exit: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
svc.AcceptShutdown | svc.AcceptShutdown
can be replaced with singlesvc.AcceptShutdown
? (Or typo forsvc.AcceptStop | svc.AcceptShutdown
?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks your reviewing this. The duplicates must be removed. Will do