lifecycle

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 6 Imported by: 0

README

lifecycle - manage service and goroutine lifecycles

Go Reference

This package provides structured management of service and goroutine lifecycles. It coordinates ordered startup and graceful shutdown of components, ensuring proper initialization sequence and reverse-order cleanup.

Documentation

Overview

Package lifecycle provides structured management of service and goroutine lifecycles. It coordinates ordered startup and graceful shutdown of components, ensuring proper initialization sequence and reverse-order cleanup.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger(ctx context.Context) *slog.Logger

Logger returns slog.Logger associated with ctx.

Types

type Component

type Component interface {
	// Start is called during startup.
	// Returning an error or calling cancel will initiate shutdown.
	Start(ctx context.Context, cancel context.CancelCauseFunc) error
	// Stop is called during shutdown.
	Stop(ctx context.Context) error
}

Component represents a component that can be managed by Lifecycle.

func Signal

func Signal(signals ...os.Signal) Component

Signal returns a component that will initiate shutdown when one of the specified signals is received.

type Lifecycle

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

Lifecycle manages component initialization and shutdown. Starts components in append order, stops them in reverse order.

func New

func New(opts ...Option) *Lifecycle

New creates a new Lifecycle.

func (*Lifecycle) Append

func (l *Lifecycle) Append(c Component)

Append adds a component to the lifecycle.

func (*Lifecycle) Go

func (l *Lifecycle) Go(fn func(ctx context.Context) error)

Go adds a managed goroutine component to the lifecycle. Lifecycle will wait for the goroutine to exit during shutdown.

func (*Lifecycle) Run

func (l *Lifecycle) Run(ctx context.Context) error

Run starts all components and waits for shutdown signal. Returns the error that initiated the shutdown and all errors encountered during shutdown. Waits for all components to stop before returning.

type Option

type Option func(cfg *config)

Option represents Lifecycle configuration option.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for lifecycle events. Default is none.

func WithStopTimeout

func WithStopTimeout(stopTimeout time.Duration) Option

WithStopTimeout sets shutdown timeout duration. Default is one minute.

type Procedure

type Procedure struct {
	// The name of the procedure. Used in logging.
	Name string
	// OnStart is called during startup.
	OnStart func(ctx context.Context) error
	// OnStop is called during shutdown.
	OnStop func(ctx context.Context) error
}

Procedure runs OnStart and OnStop functions during startup/shutdown. If OnStart function returns an error, shutdown will be initiated.

func (Procedure) Start

func (p Procedure) Start(ctx context.Context, cancel context.CancelCauseFunc) error

func (Procedure) Stop

func (p Procedure) Stop(ctx context.Context) error

type Service

type Service struct {
	// The name of the service. Used in logging.
	Name string
	// Run is called inside a goroutine during startup.
	Run func(ctx context.Context) error
	// Shutdown is called during shutdown.
	Shutdown func(ctx context.Context) error
}

Service runs Run function in a goroutine during startup and Shutdown function during shutdown. If Run function returns an error, shutdown will be initiated.

func (Service) Start

func (a Service) Start(ctx context.Context, cancel context.CancelCauseFunc) error

func (Service) Stop

func (a Service) Stop(ctx context.Context) error

type SignalError

type SignalError struct {
	Signal os.Signal
}

func (*SignalError) Error

func (e *SignalError) Error() string

Jump to

Keyboard shortcuts

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