lifecycle

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	StartupTimeout:  5 * time.Second,
	ShutdownTimeout: 5 * time.Second,
	StartStrategy:   StartStrategyFailFast | StartStrategyRollbackOnError,
}

DefaultConfig is a default lifecycle configuration.

View Source
var DefaultShutdownConfig = ShutdownConfig{}

DefaultShutdownConfig is the default configuration for the graceful shutdown.

View Source
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 New

func New(config Config) *Lifecycle

New creates new lifecycle manager.

func (*Lifecycle) Close

func (l *Lifecycle) Close() error

Close closes lifecycle manager.

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) Start

func (l *Lifecycle) Start() error

Start starts all registered startup hooks.

func (*Lifecycle) Statuses

func (l *Lifecycle) Statuses() []ServiceState

Statuses returns current statuses of all registered services and hooks.

func (*Lifecycle) Stop

func (l *Lifecycle) Stop() error

Stop stops all registered shutdown 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

func NewStdLogger(out io.Writer) *StdLogger

NewStdLogger creates a new logger that writes to the specified writer.

func (*StdLogger) Printf

func (l *StdLogger) Printf(format string, v ...interface{})

type Subscription

type Subscription interface {
	// Cancel this subscription.
	Cancel()
}

Subscription for publisher.

Jump to

Keyboard shortcuts

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