github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/winsvc/service_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package winsvc
     5  
     6  import (
     7  	wsvc "golang.org/x/sys/windows/svc"
     8  )
     9  
    10  type serviceWindows struct{}
    11  
    12  func init() {
    13  	interactive, err := wsvc.IsAnInteractiveSession()
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	// Cannot run as a service when running interactively
    18  	if interactive {
    19  		return
    20  	}
    21  	go wsvc.Run("", serviceWindows{})
    22  }
    23  
    24  // Execute implements the Windows service Handler type. It will be
    25  // called at the start of the service, and the service will exit
    26  // once Execute completes.
    27  func (serviceWindows) Execute(args []string, r <-chan wsvc.ChangeRequest, s chan<- wsvc.Status) (svcSpecificEC bool, exitCode uint32) {
    28  	const accCommands = wsvc.AcceptStop | wsvc.AcceptShutdown
    29  	s <- wsvc.Status{State: wsvc.Running, Accepts: accCommands}
    30  	for {
    31  		c := <-r
    32  		switch c.Cmd {
    33  		case wsvc.Interrogate:
    34  			s <- c.CurrentStatus
    35  		case wsvc.Stop, wsvc.Shutdown:
    36  			s <- wsvc.Status{State: wsvc.StopPending}
    37  			chanGraceExit <- 1
    38  			return false, 0
    39  		}
    40  	}
    41  
    42  	return false, 0
    43  }