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

Added service wrapper code #6220

Merged
merged 9 commits into from
Nov 11, 2019
Merged

Added service wrapper code #6220

merged 9 commits into from
Nov 11, 2019

Conversation

angrycub
Copy link
Contributor

This is the basic code to add the Windows Service Manager hooks to Nomad. There is still a need to add logging to file in order to fully support this feature.

@angrycub angrycub changed the base branch from master to 0.8.7 September 24, 2019 17:41
@angrycub angrycub changed the base branch from 0.8.7 to master September 24, 2019 17:41
@endocrimes
Copy link
Contributor

@angrycub What's left to get this ready for review? Would be nice to ship service support in 10.1

@angrycub
Copy link
Contributor Author

This looks good to go based on prelim. testing. I was able to start Nomad as a service and manage it from the native Windows tooling.

@angrycub angrycub marked this pull request as ready for review October 29, 2019 19:52
Copy link
Contributor

@endocrimes endocrimes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@@ -179,6 +183,22 @@ testing.

- `log_json` `(bool: false)` - Output logs in a JSON format.

- `log_file` `(string: "")` - Specifies the path for logging. If the path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for handling this 👍

service_os/service_windows.go Outdated Show resolved Hide resolved

func (serviceWindows) Execute(args []string, r <-chan wsvc.ChangeRequest, s chan<- wsvc.Status) (svcSpecificEC bool, exitCode uint32) {
const accCommands = wsvc.AcceptStop | wsvc.AcceptShutdown
s <- wsvc.Status{State: wsvc.StartPending}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary since the next line sends Running? If Windows requires us sending it first we should comment to that effect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the win32 API docs for this concept, it looks like the expectation is that services set SERVICE_START_PENDING at the very beginning, and then wait to set SERVICE_STARTED only once we're ready to receive any shutdown signals. But the way this Execute gets invoked, the change request channel already exists by the time we're called, so we're good-to-go at that point. Compare to https://docs.microsoft.com/en-us/windows/win32/services/writing-a-servicemain-function where they have to set up the stop signal handler first.

So I don't think there's any particular reason to have this line here. Removed in aa0b3f7

(That being said, all signal handling is racy during startup, including on Linux. If we get a signal before the signal handler has been created, we die without being able to gracefully shut down. The API for Windows implies they don't send you a shutdown notice until you've indicated you're ready to receive one via SERVICE_START_PENDING, but that's just from the service manager. Some other process could come along and SIGINT you and you'd be just as dead as under Unix.)

c := <-r
switch c.Cmd {
case wsvc.Interrogate:
s <- c.CurrentStatus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any clue why Go's example service does a wacky send/sleep/send instead of just sending back CurrentStatus?! https://github.com/golang/sys/blob/master/windows/svc/example/service.go#L38-L42

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the background in https://code.google.com/archive/p/winsvc/issues/4 it looks like the comment they have here "Testing deadlock" literally means "we've left testing code in here in order to see if we can trigger this deadlock." I don't think we want to keep that.

s <- c.CurrentStatus
case wsvc.Stop, wsvc.Shutdown:
chanGraceExit <- 1
s <- wsvc.Status{State: wsvc.StopPending}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should swap the order of these 2 lines to notify Windows we're intending to stop before signally to Nomad to stop. Either that or make the chan buffered so L35 doesn't block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It looks like this was introduced in hashicorp/consul@319a0ae#diff-b58928334218b555d4fbe9d2b431a706 but there was a giant sleep in that commit, so I suspect the order wasn't well-defined.

Copy link
Member

@tgross tgross Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f107b52

service_os/service.go Outdated Show resolved Hide resolved
website/source/docs/configuration/index.html.md Outdated Show resolved Hide resolved
log should be written to before it needs to be rotated. Must be a duration
value such as 30s.

- `log_rotate_max_files` `(int: 0)` - Specifies the maximum number of older log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I totally didn't think about the defaults of these from #6429 -- I filed #6603 as a followup

website/source/guides/install/windows-service.html.md Outdated Show resolved Hide resolved
website/source/layouts/guides.erb Show resolved Hide resolved
website/source/layouts/guides.erb Outdated Show resolved Hide resolved
@tgross
Copy link
Member

tgross commented Nov 6, 2019

I've rebased this on master and I think I've addressed the open code review comments. Next I'm going to take it for a spin on the Windows e2e infra to verify it still works.

@tgross tgross added this to the near-term milestone Nov 7, 2019
@tgross
Copy link
Member

tgross commented Nov 8, 2019

Success!

PS C:\Users\Administrator> sc.exe query Nomad

SERVICE_NAME: Nomad
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

@tgross
Copy link
Member

tgross commented Nov 8, 2019

@schmichael this was previously approved by @endocrimes but you had some open questions on it, so I want to make sure you're happy with my answers before we merge this.

Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple unimportant nitpicks. Feel free to merge whenever. Thanks @angrycub and @tgross!

helper/winsvc/service_windows.go Outdated Show resolved Hide resolved
helper/winsvc/service.go Outdated Show resolved Hide resolved
@tgross
Copy link
Member

tgross commented Nov 11, 2019

Just a couple unimportant nitpicks. Feel free to merge whenever.

👍 I'll pick those up as well as the CHANGELOG conflict.

angrycub and others added 8 commits November 11, 2019 14:05
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
* fix layout on guide for sidebar
* link downloads
* correct md formatting for links inside of monospace
* shorten title
@tgross
Copy link
Member

tgross commented Nov 11, 2019

I've made those updates and rebased on master. Once the tests pass I'll squash-and-merge this.

@tgross tgross merged commit 1ec6388 into master Nov 11, 2019
@tgross tgross deleted the f-win-service branch November 11, 2019 20:16
@tgross tgross removed this from the near-term milestone Jan 9, 2020
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants