runner

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package runner provides an application lifecycle manager for coordinated startup and graceful shutdown.

Services are registered in two tiers: infrastructure (database, cache, queue) and core (API handlers). Infrastructure services start first; if any fail, the application exits immediately. Core services start afterward. On shutdown, core services stop first, then infrastructure. The entire shutdown is bounded by a configurable timeout (default 30 seconds). Service panics are caught and logged.

Basic usage:

r := runner.New(
    runner.WithInfrastructureService(dbService),
    runner.WithInfrastructureService(cacheService),
    runner.WithCoreService(apiServer),
    runner.WithShutdownTimeout(30*time.Second),
)
r.Run() // Blocks until SIGINT/SIGTERM; calls os.Exit(1) on error

Use RunContext instead of Run to supply your own context and receive an error instead of exiting the process (e.g. for custom signal handling).

Each service must implement the Service interface (Start, Stop, Name). Startup failures in any tier abort the application immediately.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServicePanic   = errors.New("runner: service panicked")
	ErrServiceFailed  = errors.New("runner: service failed to start")
	ErrShutdownTimout = errors.New("runner: shutdown timeout exceeded")
)

Functions

This section is empty.

Types

type Option

type Option func(*Runner)

func WithCoreService

func WithCoreService(svc Service) Option

func WithInfrastructureService

func WithInfrastructureService(svc Service) Option

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

func WithStartupGracePeriod added in v1.4.0

func WithStartupGracePeriod(d time.Duration) Option

type Runner

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

func New

func New(opts ...Option) *Runner

func (*Runner) Run

func (r *Runner) Run()

func (*Runner) RunContext added in v1.4.0

func (r *Runner) RunContext(ctx context.Context) error

RunContext starts all registered services and blocks until ctx is cancelled or any service fails. Either event triggers a graceful shutdown (core services first, then infrastructure). It returns nil on a clean cancellation-triggered shutdown and the service failure otherwise.

Most applications should use Run, which wires SIGINT/SIGTERM handling and exits the process on failure. RunContext is for callers that manage their own signals or exit codes, and for tests.

type Service

type Service interface {
	Start(ctx context.Context) error
	Stop() error
	Name() string
}

Jump to

Keyboard shortcuts

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