Application lifecycle manager enforcing a three-phase init/wire/start sequence with reverse-order graceful shutdown and per-component stop timeouts. What's included: - `Component` interface (OnInit / OnStart / OnStop) and `Hook` type for BeforeStart wiring functions - `Launcher` interface with Append, BeforeStart, Run (blocks on SIGINT/SIGTERM), and idempotent Shutdown(ctx) - `New(logger, opts...)` constructor with configurable ComponentStopTimeout (default 15 s); no global state Tested-via: todo-api POC integration Reviewed-against: docs/adr/
34 lines
1.2 KiB
Go
34 lines
1.2 KiB
Go
// Package launcher manages the application lifecycle for Go services.
|
|
//
|
|
// It orchestrates a sequence of phases — init, assemble, start, wait, shutdown —
|
|
// across a set of registered [Component] implementations. Any infrastructure piece
|
|
// (database pool, HTTP server, background worker) that implements the three-method
|
|
// Component interface can be managed by the launcher.
|
|
//
|
|
// # Lifecycle
|
|
//
|
|
// OnInit (all components, in order) ← open connections, allocate resources
|
|
// BeforeStart hooks (in order) ← wire dependencies after all inits done
|
|
// OnStart (all components, in order) ← start goroutines, begin serving
|
|
// --- application is running ---
|
|
// OnStop (all components, reverse) ← graceful shutdown, release resources
|
|
//
|
|
// # Basic wiring
|
|
//
|
|
// logger := logz.New(logz.Options{})
|
|
// lc := launcher.New(logger)
|
|
//
|
|
// lc.Append(db, cache, server)
|
|
// lc.BeforeStart(func() error {
|
|
// return server.RegisterRoutes(db, cache)
|
|
// })
|
|
//
|
|
// if err := lc.Run(); err != nil {
|
|
// logger.Error("launcher failed", err)
|
|
// os.Exit(1)
|
|
// }
|
|
//
|
|
// Run blocks until SIGINT/SIGTERM is received or [Launcher.Shutdown] is called,
|
|
// then stops all components in reverse order before returning.
|
|
package launcher
|