github.com/hernad/nomad@v1.6.112/helper/winsvc/service_windows.go (about)

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