github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/helper/winsvc/service_windows.go (about)

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