lifecycle

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lifecycle coordinates application components and resource cleanup.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRun = errors.New("lifecycle: application has already run")

Functions

This section is empty.

Types

type App

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

App owns the lifecycle of its components and is run once.

func New

func New(options Options, components ...Component) *App

func (*App) Run

func (a *App) Run(ctx context.Context) (err error)

Run starts components in order. Cancellation or completion of any component stops the application. Shutdown uses a fresh context with a shared timeout, stops components in reverse order, waits for their exit, then runs Cleanup. Ordinary caller cancellation is a successful shutdown. Component, shutdown and cleanup errors are preserved with errors.Join.

func (*App) RunSignals

func (a *App) RunSignals() error

RunSignals runs until a component exits, SIGINT or SIGTERM is received.

type Component

type Component interface {
	Start(context.Context) error
	Wait() error
	Stop(context.Context) error
}

Component represents a server or background worker. Start returns when ready; Wait blocks until execution ends and returns nil for normal shutdown. Stop must respect its context and release partially started resources even if Start failed. Stop must also allow Wait to finish.

type Options

type Options struct {
	// ShutdownTimeout bounds the shared shutdown phase; zero defaults to 10s.
	ShutdownTimeout time.Duration
	// Cleanup is typically the callback returned by a generated initializer.
	// It runs after shutdown, including on startup failure or cancellation.
	Cleanup func() error
}

Jump to

Keyboard shortcuts

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