open-nomad/helper/winsvc/service_windows.go
Charlie Voiselle 835831a3d8 Added service wrapper code (#6220)
This is the basic code to add the Windows Service Manager hooks to Nomad.

Includes vendoring golang.org/x/sys/windows/svc and added Docs:
* guide for installing as a windows service.
* configuration for logging to file from PR #6429
2019-11-11 15:16:07 -05:00

43 lines
979 B
Go

//+build windows
package winsvc
import (
wsvc "golang.org/x/sys/windows/svc"
)
type serviceWindows struct{}
func init() {
interactive, err := wsvc.IsAnInteractiveSession()
if err != nil {
panic(err)
}
// Cannot run as a service when running interactively
if interactive {
return
}
go wsvc.Run("", serviceWindows{})
}
// Execute implements the Windows service Handler type. It will be
// called at the start of the service, and the service will exit
// once Execute completes.
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.Running, Accepts: accCommands}
for {
c := <-r
switch c.Cmd {
case wsvc.Interrogate:
s <- c.CurrentStatus
case wsvc.Stop, wsvc.Shutdown:
s <- wsvc.Status{State: wsvc.StopPending}
chanGraceExit <- 1
return false, 0
}
}
return false, 0
}