Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyStarted = errors.New("service-manager already started")
ErrAlreadyStarted is returned from Manager.Start when the service-manager was already started before.
var ErrAlreadyStopped = errors.New("service-manager is already stopped")
ErrAlreadyStopped is returned from Manager.Stop when it was called before.
var ErrAlreadyStopping = errors.New("service-manager is already stopping")
ErrAlreadyStopping is returned from Manager.Stop when the service-manager is already in stopping state.
var ErrNoService = errors.New("at least one service must be registered to start the service-manager")
ErrNoService is returned from Manager.Start when no service is added
var ErrNotStarted = errors.New("service-manager is not started yet")
ErrNotStarted is returned from Manager.Stop when it is called before Manager.Start is called.
Functions ¶
func OSStopSignal ¶
func OSStopSignal() <-chan struct{}
OSStopSignal returns a channel which will be closed when SIGTERM or SIGINT is send to the program.
func SignalFromTime ¶
SignalFromTime wraps channels returned from time async functions so that they can be used with StopOnSignal.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager represents a service-manager which controls the lifecycle for provided services.
func (*Manager) Start ¶
Start executes the Start method for each previously added service in a seperated goroutine. A ServiceErrors is returned when at least one Service.Start method failed. If a signal-channel is given, the channel waits for signal, if received Manager.Stop is executed. Start can only be called once otherwise ErrAlreadyStarted error is returned. If a start-method fails during start process (service return error before Manager.Stop was called) Manager.Stop will be exectued to stop all Service which have been started already. If no service is registred ErrNoService is returned. Before any service is started, it is ensured that the Service.Start and Service.Stop is not nil. The Service.Name must not be empty and must be unique, otherwise a error is returned
func (*Manager) Stop ¶
Stop executes the Stop method for each previously added service. Each Service.Stop is executed in its own goroutine, the goroutine can only be finished when the Service.Stop returns. This means that a stop-goroutine can continue to run in the background even if stop-timeout has been triggered and Manager.Stop already starts to execute Service.Kill methods. If the Service.Stop is not terminated after the stop-timeout duration, the Kill method will be executed for that service, if available. It will return a ServiceErrors error when at least on Service.Start method failed Manager is considered stopped when either all Service.Start methods are returned or all kill-timeouts for all services are elapsed. If service-manager is already stopped ErrAlreadyStopped error is returned. If service-manager in stopping state ErrAlreadyStopping error is returned.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option a service-manager option
func StopOnOSSignal ¶
func StopOnOSSignal() Option
StopOnOSSignal uses OSStopSignal for StopOnSignal channel
func StopOnSignal ¶
func StopOnSignal(sig <-chan struct{}) Option
StopOnSignal set channel to trigger the service-manager to stop.
type OptionFunc ¶
type OptionFunc func(*Manager)
OptionFunc is an adapter to allow the use of ordinary functions as Option.
type Service ¶
type Service struct {
// Name is a unique identifiyer for the service
Name ServiceName
// Start start a service and is required
Start func() error
// Stop stops a service and is required
Stop func(context.Context)
// When stop was not successfull after a given timeout kill is executed.
// todo: Add context.Context argument
Kill func()
// StopTimeout max duration before Kill gets executed
StopTimeout time.Duration
// KillTimeout max duration before shutdown failes
KillTimeout time.Duration
StopAfter []ServiceName
StopBefore []ServiceName
}
Service represents a service which is controlled by the Manager
type ServiceErrors ¶
type ServiceErrors struct {
Map map[ServiceName]error
}
ServiceErrors contains all service errors
func (ServiceErrors) Error ¶
func (serr ServiceErrors) Error() string