github.com/secure-build/gitlab-runner@v12.5.0+incompatible/helpers/service/simple.go (about)

     1  package service_helpers
     2  
     3  import (
     4  	"errors"
     5  	service "github.com/ayufan/golang-kardianos-service"
     6  	"os"
     7  	"os/signal"
     8  	"syscall"
     9  )
    10  
    11  var (
    12  	// ErrNotSupported is returned when specific feature is not supported.
    13  	ErrNotSupported = errors.New("Not supported")
    14  )
    15  
    16  type SimpleService struct {
    17  	i service.Interface
    18  	c *service.Config
    19  }
    20  
    21  // Run should be called shortly after the program entry point.
    22  // After Interface.Stop has finished running, Run will stop blocking.
    23  // After Run stops blocking, the program must exit shortly after.
    24  func (s *SimpleService) Run() (err error) {
    25  	err = s.i.Start(s)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	sigChan := make(chan os.Signal, 3)
    31  	signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt)
    32  
    33  	<-sigChan
    34  
    35  	return s.i.Stop(s)
    36  }
    37  
    38  // Start signals to the OS service manager the given service should start.
    39  func (s *SimpleService) Start() error {
    40  	return service.ErrNoServiceSystemDetected
    41  }
    42  
    43  // Stop signals to the OS service manager the given service should stop.
    44  func (s *SimpleService) Stop() error {
    45  	return ErrNotSupported
    46  }
    47  
    48  // Restart signals to the OS service manager the given service should stop then start.
    49  func (s *SimpleService) Restart() error {
    50  	return ErrNotSupported
    51  }
    52  
    53  // Install setups up the given service in the OS service manager. This may require
    54  // greater rights. Will return an error if it is already installed.
    55  func (s *SimpleService) Install() error {
    56  	return ErrNotSupported
    57  }
    58  
    59  // Uninstall removes the given service from the OS service manager. This may require
    60  // greater rights. Will return an error if the service is not present.
    61  func (s *SimpleService) Uninstall() error {
    62  	return ErrNotSupported
    63  }
    64  
    65  // Status returns nil if the given service is running.
    66  // Will return an error if the service is not running or is not present.
    67  func (s *SimpleService) Status() error {
    68  	return ErrNotSupported
    69  }
    70  
    71  // Logger opens and returns a system logger. If the user program is running
    72  // interactively rather then as a service, the returned logger will write to
    73  // os.Stderr. If errs is non-nil errors will be sent on errs as well as
    74  // returned from Logger's functions.
    75  func (s *SimpleService) Logger(errs chan<- error) (service.Logger, error) {
    76  	return service.ConsoleLogger, nil
    77  }
    78  
    79  // SystemLogger opens and returns a system logger. If errs is non-nil errors
    80  // will be sent on errs as well as returned from Logger's functions.
    81  func (s *SimpleService) SystemLogger(errs chan<- error) (service.Logger, error) {
    82  	return nil, ErrNotSupported
    83  }
    84  
    85  // String displays the name of the service. The display name if present,
    86  // otherwise the name.
    87  func (s *SimpleService) String() string {
    88  	return "SimpleService"
    89  }