service

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSignal

func GetSignal(s string) (syscall.Signal, bool)

GetSignal takes a signal name like "TERM" or "SIGTERM" and returns the syscall.Signal for it, or a zero syscall.Signal and false if the name does not match a known/supported signal.

Types

type Callback

type Callback func(svc Service, running bool)

type Change

type Change struct {
	Service   Service
	Running   bool
	Timestamp time.Time
}

type Command

type Command struct {
	// Path is the path to the program's executable
	Path string

	// Args are the arguments passed to the program's executable.
	Args []string

	// Workdir specifies the working directory for the program.
	Workdir string

	// Env specifies the environment for the program.
	Env []string

	// StopSignal specifies the signal used to stop the program, when Stop is called on the handle returned by
	// a call to Start. If it's empty when Start is called, SIGTERM is used.
	StopSignal syscall.Signal

	// GracePeriod specifies the max time to wait for the program to quit on its own before it is
	// killed after a call to Stop on the handle returned by the call to Start.
	GracePeriod time.Duration
}

Command represents the command to start a Service. All fields can be changed at any time and will have effect on next call to Start.

func (*Command) Start

func (c *Command) Start(stdout, stderr io.Writer) (*handle, error)

Start starts the command using the given writers as its stdout and stderr. An error with a nil Handle is returned if the command fails to start. If the command starts, the Handle can be used to monitor the command lifecycle.

type Handle

type Handle interface {
	// Wait blocks until the service stopped (either because it decided to stop on its own, or because Stop was called).
	Wait() error

	// Stop stops the service.
	Stop()
}

Handle represents a running Service.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager() *Manager

func (*Manager) Running

func (mgr *Manager) Running(svc Service) bool

Running reports on the current running state of a service.

func (*Manager) Shutdown

func (mgr *Manager) Shutdown()

Shutdown initiate stopping all running services, blocking until all have stopped. Further calls return immediately. Once Shutdown returns, the Manager can't be used anymore to Start or Stop services.

func (*Manager) Start

func (mgr *Manager) Start(services ...Service)

Start starts the Services if they are not running. It does nothing if called after shutdown was initiate by a call to Shutdown.

func (*Manager) Stop

func (mgr *Manager) Stop(services ...Service)

Stop stops one or more services. Has no effect when called after Shutdown, as all running services will be in the process of stopping or already stopped.

func (*Manager) Subscribe

func (mgr *Manager) Subscribe(ctx context.Context) <-chan Change

Subscribe returns a new channel to which a Change is written every time a service starts and stops, and it's closed when ctx is canceled or the Manager shutdowns. There might still be Changes to read after subscription is canceled. If Subscribe is called after shutdown a closed channel is returned.

type Service

type Service interface {
	// Start starts the service, and returns an Handle to represent the running Service, or a nil Handle and an error
	// if the service cannot be started. Calling Start multiple times should start multiple instances of the Service.
	Start() (Handle, error)

	Name() string

	fmt.Stringer
}

func NewDaemon

func NewDaemon(name string, cmd *Command, stdout, stderr io.Writer) Service

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL