grace

package
v0.85.0-pre.12 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(l Lifecycle)

Run manages the core lifecycle of an application, including startup, shutdown, and os signal handling. Lifecycle.Startup is expected to block (e.g., http.Server.ListenAndServe). If it returns immediately, Run will wait for a shutdown signal before exiting.

func Terminate

func Terminate(process *os.Process, waitFor time.Duration, logger *slog.Logger) error

Terminate attempts to gracefully terminate a process, falling back to force kill after timeout. If logger is nil, defaults to stdout.

Types

type Lifecycle

type Lifecycle struct {
	ShutdownTimeout time.Duration // Default: 30 seconds
	Signals         []os.Signal   // Default: SIGHUP, SIGINT, SIGTERM, SIGQUIT
	Logger          *slog.Logger  // Default: os.Stdout

	// Startup runs your main application logic (e.g., server.ListenAndServe).
	// This callback should block until the application is ready to shut down.
	// Do not call os.Exit or log.Fatal here; return an error instead.
	Startup func() error

	// Shutdown runs cleanup logic (e.g., server.Shutdown, closing DB connections).
	// The context has a timeout based on ShutdownTimeout.
	// Do not call os.Exit or log.Fatal here; return an error instead.
	Shutdown func(context.Context) error
}

func (Lifecycle) Run

func (l Lifecycle) Run()

Run manages the core lifecycle of an application, including startup, shutdown, and os signal handling. Lifecycle.Startup is expected to block (e.g., http.Server.ListenAndServe). If it returns immediately, Run will wait for a shutdown signal before exiting.

Jump to

Keyboard shortcuts

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