From 411b24d95df3e4ef2ad5dee072354f4685aa8e7c Mon Sep 17 00:00:00 2001 From: Siva Date: Mon, 18 Jun 2018 14:49:59 -0400 Subject: [PATCH] Added Hook for Windows Service --- service_os/service.go | 1 + service_os/service_windows.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 service_os/service.go create mode 100644 service_os/service_windows.go diff --git a/service_os/service.go b/service_os/service.go new file mode 100644 index 0000000000..6db6263b6f --- /dev/null +++ b/service_os/service.go @@ -0,0 +1 @@ +package service_os diff --git a/service_os/service_windows.go b/service_os/service_windows.go new file mode 100644 index 0000000000..fa3e8b30c8 --- /dev/null +++ b/service_os/service_windows.go @@ -0,0 +1,43 @@ +//+build windows + +package service_os + +import ( + "os" + wsvc "golang.org/x/sys/windows/svc" +) + +type serviceWindows struct {} + +func init() { + interactive, err := wsvc.IsAnInteractiveSession() + if err != nil { + panic(err) + } + if interactive { + return + } + go func() { + _ = wsvc.Run("", serviceWindows{}) + os.Exit(0) + }() +} + +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} + + 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} + return false, 0 + } + } + + return false, 0 +}