Skip to content

Commit

Permalink
Added service wrapper code
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Oct 23, 2019
1 parent 44c576e commit 460178b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
gatedwriter "github.com/hashicorp/nomad/helper/gated-writer"
"github.com/hashicorp/nomad/helper/logging"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/service_os"
"github.com/hashicorp/nomad/version"
"github.com/mitchellh/cli"
"github.com/posener/complete"
Expand Down Expand Up @@ -777,6 +778,8 @@ WAIT:
select {
case s := <-signalCh:
sig = s
case <-service_os.Shutdown_Channel():
sig = os.Interrupt
case <-c.ShutdownCh:
sig = os.Interrupt
case <-c.retryJoinErrCh:
Expand Down
7 changes: 7 additions & 0 deletions service_os/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service_os

var chanGraceExit = make(chan int)

func Shutdown_Channel() <-chan int {
return chanGraceExit
}
42 changes: 42 additions & 0 deletions service_os/service_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//+build windows

package service_os

import (
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{})
}()
}

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:
chanGraceExit <- 1
s <- wsvc.Status{State: wsvc.StopPending}
return false, 0
}
}

return false, 0
}

0 comments on commit 460178b

Please sign in to comment.