Documentation
¶
Overview ¶
Package lifecycle provides a simple interface for managing the application lifecycle.
The main component of this package is Lifecycle, which is a service lifecycle manager. It provides methods to register startup and shutdown hooks, and to start and stop the service. Lifecycle also provides a method to subscribe to service state changes, which can be used to monitor the service state.
Lifecycle package also provides a SignalHandler component, which can be used to handle OS signals and stop the service on receiving a signal.
`Config` type contains configuration for Lifecycle, the default configuration can be obtained with `DefaultConfig`.
Index ¶
- Variables
- type Config
- type Lifecycle
- func (l *Lifecycle) Close() error
- func (l *Lifecycle) RegisterService(service types.ServiceConfig)
- func (l *Lifecycle) RegisterShutdownHook(name string, h types.ShutdownHook)
- func (l *Lifecycle) RegisterStartupHook(name string, h types.StartupHook)
- func (l *Lifecycle) Start() error
- func (l *Lifecycle) Statuses() []ServiceState
- func (l *Lifecycle) Stop() error
- func (l *Lifecycle) SubscribeMonitor(ch chan<- []ServiceState) Subscription
- type Logger
- type ServiceState
- type ShutdownConfig
- type SignalHandler
- type StartStrategy
- type StdLogger
- type Subscription
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ StartupTimeout: 5 * time.Second, ShutdownTimeout: 5 * time.Second, StartStrategy: StartStrategyFailFast | StartStrategyRollbackOnError, }
DefaultConfig is a default lifecycle configuration.
var DefaultShutdownConfig = ShutdownConfig{}
DefaultShutdownConfig is the default configuration for the graceful shutdown.
var NopLogger = nopLogger{}
NopLogger is a no-op logger.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// StartupTimeout is a timeout for startup.
StartupTimeout time.Duration
// ShutdownTimeout is a timeout for shutdown.
ShutdownTimeout time.Duration
// StartStrategy is a strategy for startup.
StartStrategy StartStrategy
}
Config is a lifecycle configuration.
type Lifecycle ¶
type Lifecycle struct {
// contains filtered or unexported fields
}
Lifecycle represents application lifecycle manager. It provides methods to register startup and shutdown hooks, and to start and stop the service.
func (*Lifecycle) RegisterService ¶
func (l *Lifecycle) RegisterService(service types.ServiceConfig)
RegisterService registers service to lifecycle with config.
func (*Lifecycle) RegisterShutdownHook ¶
func (l *Lifecycle) RegisterShutdownHook(name string, h types.ShutdownHook)
RegisterShutdownHook adds shutdown hook to lifecycle.
func (*Lifecycle) RegisterStartupHook ¶
func (l *Lifecycle) RegisterStartupHook(name string, h types.StartupHook)
RegisterStartupHook adds startup hook to lifecycle.
func (*Lifecycle) Statuses ¶
func (l *Lifecycle) Statuses() []ServiceState
Statuses returns current statuses of all registered services and hooks.
func (*Lifecycle) SubscribeMonitor ¶
func (l *Lifecycle) SubscribeMonitor(ch chan<- []ServiceState) Subscription
SubscribeMonitor subscribes to lifecycle service state monitor.
type Logger ¶
type Logger interface {
// Printf prints formatted message.
Printf(format string, v ...interface{})
}
Logger for lifecycle messages.
type ServiceState ¶
type ServiceState struct {
// Service ID, is assigned on register calls.
ID int
// Service name, specified by user.
Name string
// Service status.
Status types.ServiceStatus
// Service error, if any.
Error error
}
ServiceState is a named state of service with unique ID of this server and optional error.
func (ServiceState) String ¶
func (s ServiceState) String() string
type ShutdownConfig ¶
type ShutdownConfig struct {
ExitOnShutdown bool
}
ShutdownConfig is the configuration for the graceful shutdown.
type SignalHandler ¶
type SignalHandler struct {
// contains filtered or unexported fields
}
SignalHandler is an OS signal handler that can be used to trigger a lifecycle shutdown.
func NewSignalHandler ¶
func NewSignalHandler(lifecycle *Lifecycle, logger Logger, signals ...os.Signal) *SignalHandler
NewSignalHandler creates a new SignalHandler that will trigger the given lifecycle on the given signals. If no signals are given, it will trigger on SIGINT and SIGTERM.
func (*SignalHandler) Start ¶
func (h *SignalHandler) Start(cfg ShutdownConfig)
Start starts the signal handler in a new goroutine.
func (*SignalHandler) Wait ¶
func (h *SignalHandler) Wait() (err error)
Wait waits for the signal handler to stop.
type StartStrategy ¶
type StartStrategy int
StartStrategy is a strategy for startup.
const ( // StartStrategyFailFast (default) returns error on first failed service. StartStrategyFailFast StartStrategy = 1 << iota // StartStrategyStartAll starts all services and returns error if any service failed. StartStrategyStartAll // StartStrategyRollbackOnError starts all services and rollback all started services on error. StartStrategyRollbackOnError )
type StdLogger ¶
type StdLogger struct {
// contains filtered or unexported fields
}
StdLogger is a logger that writes to the specified writer.
func NewStdLogger ¶
NewStdLogger creates a new logger that writes to the specified writer.
type Subscription ¶
type Subscription interface {
// Cancel this subscription.
Cancel()
}
Subscription for publisher.