github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/cmd/service/service_handler.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Copyright 2015 Cloudbase Solutions
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  //
     5  // +build windows
     6  
     7  package service
     8  
     9  import (
    10  	"github.com/gabriel-samfira/sys/windows/svc"
    11  )
    12  
    13  // SystemService type that is responsible for managing the life-cycle of the service
    14  type SystemService struct {
    15  	// Name the label for the service. It is not used for any useful operation
    16  	// by the service handler.
    17  	Name string
    18  	// Cmd is the function the service handler will run as a service.
    19  	Cmd func(args []string)
    20  	// Args is passed to Cmd() as function arguments.
    21  	Args []string
    22  }
    23  
    24  // Execute implements the svc.Handler interface
    25  func (s *SystemService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
    26  	const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
    27  	changes <- svc.Status{State: svc.StartPending}
    28  
    29  	go s.Cmd(s.Args)
    30  
    31  	changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
    32  
    33  	for c := range r {
    34  		switch c.Cmd {
    35  		case svc.Interrogate:
    36  			changes <- c.CurrentStatus
    37  		case svc.Stop, svc.Shutdown:
    38  			// TODO (gabriel-samfira): Add more robust handling of service termination
    39  			changes <- svc.Status{State: svc.StopPending}
    40  			return false, 0
    41  		}
    42  	}
    43  	return false, 0
    44  }
    45  
    46  // Run runs the service
    47  func (s *SystemService) Run() error {
    48  	return svc.Run(s.Name, s)
    49  }