Documentation
¶
Overview ¶
Package lifecycle owns main: Run sets up logging, tracing and metrics from the identity, writes process.start, runs the app's function and takes every exit through one stop sequence (contract.md section 9.1). The causes are the function returning, SIGINT, SIGTERM or SIGHUP, Stop, a panic in the function or in a goroutine started with Go, and a Go goroutine returning an error. The sequence cancels ctx, waits for the Go goroutines, runs the OnStop steps in reverse order, waits for the function, writes process.stop or process.crash, flushes spans and metrics and exits with the code of the failure table.
func main() {
lifecycle.Run(identity.New("api"), serve, lifecycle.Grace(20*time.Second))
}
func serve(ctx context.Context, log *slog.Logger) error {
db, err := sql.Open("postgres", os.Getenv("DB_URL"))
if err != nil {
return err
}
lifecycle.OnStop(ctx, "db", db.Close)
srv := httpserver.New(log, routes(db), httpserver.Addr(":8080"))
lifecycle.OnStop(ctx, "http", func() error { return srv.Shutdown(context.Background()) })
lifecycle.Go(ctx, "consumer", func(ctx context.Context) error { return consumer.Run(ctx, db) })
return srv.ListenAndServe(ctx)
}
Teardown is OnStop steps, not defers. os.Exit and log.Fatal inside the function skip the sequence; so does a panic in a goroutine started with plain go (section 6).
Run makes its logger the default of log/slog, so a line written through slog.Info or a library that logs through slog.Default is the contract's line. A service test runs fn through the same sequence in-process with lifecycletest.Run.
Example ¶
The server of contract.md section 9.1: the database closes after the consumer, because Run waits for the Go goroutines before the steps; the http step drains the requests and makes serve return.
package main
import (
"context"
"database/sql"
"log/slog"
"net/http"
"os"
"time"
"github.com/AndreiD108/d108-go/httpserver"
"github.com/AndreiD108/d108-go/identity"
"github.com/AndreiD108/d108-go/lifecycle"
)
// The server of contract.md section 9.1: the database closes after the consumer, because Run
// waits for the Go goroutines before the steps; the http step drains the requests and makes
// serve return.
func main() {
lifecycle.Run(identity.New("api"), serve, lifecycle.Grace(20*time.Second))
}
func serve(ctx context.Context, log *slog.Logger) error {
db, err := sql.Open("postgres", os.Getenv("DB_URL"))
if err != nil {
return err
}
lifecycle.OnStop(ctx, "db", db.Close)
srv := httpserver.New(log, routes(db), httpserver.Addr(":8080"))
lifecycle.OnStop(ctx, "http", func() error { return srv.Shutdown(context.Background()) })
lifecycle.Go(ctx, "consumer", func(ctx context.Context) error { return consume(ctx, db) })
return srv.ListenAndServe(ctx)
}
func routes(*sql.DB) http.Handler { return http.NewServeMux() }
func consume(ctx context.Context, _ *sql.DB) error {
<-ctx.Done()
return ctx.Err()
}
Output:
Index ¶
Examples ¶
Constants ¶
const ( ReasonDone = run.ReasonDone ReasonError = run.ReasonError ReasonSignal = run.ReasonSignal ReasonApp = run.ReasonApp ReasonForced = run.ReasonForced )
The reason values of process.stop.
const DefaultGrace = run.DefaultGrace
DefaultGrace is the time the stop sequence gets before Run forces the exit.
const HeartbeatInterval = 60 * time.Second
HeartbeatInterval is the time between two process.heartbeat lines.
Variables ¶
This section is empty.
Functions ¶
func Go ¶
Go starts fn in a goroutine the stop sequence waits for. A panic in fn is recovered and written as process.crash; an error ends the process with reason error and the error's exit code; nil ends the goroutine quietly. A cancelled or terminated error after the stop started is quiet too. Once the steps began, fn does not start and a WARN line says so.
func OnStop ¶
OnStop registers a named stop step. Steps run in reverse order of registration when the process stops, every one of them, each failure an ERROR line with the step's name. A step registered once the steps began (the Go goroutines had ended) does not run and is reported with a WARN line. ctx must come from Run (or lifecycletest.Run); another ctx panics.
func Run ¶
Run owns main. It builds the logger, refuses to start on a bad LOG_LEVEL or LOG_OUTPUT (the error envelope on stderr, exit 2), turns tracing and metrics on when the environment asks, writes process.start, runs fn and ends the process through the stop sequence. It never returns. A second call in the same process panics.
Types ¶
type Option ¶
Option configures Run.
func Grace ¶
Grace sets how long the stop sequence may take before Run forces the exit (default DefaultGrace). 0 or less never forces; a second signal still does.
func Heartbeat ¶
Heartbeat turns the process.heartbeat line on or off (default on). Commands turn it off.
func RedactFlags ¶ added in v0.2.0
RedactFlags names value-taking long flags, without the dashes, whose values process.start logs as "redacted": a flag that carries a pepper or a similar value. The app still reads the real value; such a flag has no short form.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
run
Package run is the state of one lifecycle run and its stop sequence, shared by lifecycle (the process owner) and lifecycletest (the in-process harness).
|
Package run is the state of one lifecycle run and its stop sequence, shared by lifecycle (the process owner) and lifecycletest (the in-process harness). |
|
Package lifecycletest runs a lifecycle.Func through the real stop sequence inside a test, so a service test sees its OnStop steps, Go goroutines and Stop calls behave as they do under lifecycle.Run: ctx cancelled, the Go goroutines drained, the steps in reverse order, fn waited for, process.stop or process.crash as the last line.
|
Package lifecycletest runs a lifecycle.Func through the real stop sequence inside a test, so a service test sees its OnStop steps, Go goroutines and Stop calls behave as they do under lifecycle.Run: ctx cancelled, the Go goroutines drained, the steps in reverse order, fn waited for, process.stop or process.crash as the last line. |