grace

package
v0.85.0-pre.2 Latest Latest
Warning

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

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

README

kit/grace

github.com/vormadev/vorma/kit/grace

grace provides lifecycle orchestration for long-running Go processes:

  • startup execution
  • OS signal handling
  • shutdown callbacks with timeout context
  • optional helper to terminate child processes cleanly

Import

import "github.com/vormadev/vorma/kit/grace"

Quick Start

srv := &http.Server{Addr: ":8080", Handler: router}

grace.Orchestrate(grace.OrchestrateOptions{
	StartupCallback: func() error {
		err := srv.ListenAndServe()
		if errors.Is(err, http.ErrServerClosed) {
			return nil
		}
		return err
	},
	ShutdownCallback: func(ctx context.Context) error {
		return srv.Shutdown(ctx)
	},
})

Orchestration Model

  • StartupCallback runs first. It should block while your app is running.
  • On signal (or startup failure), ShutdownCallback runs with a timeout context.
  • Defaults:
  • ShutdownTimeout = 30s
  • signals: SIGHUP, SIGINT, SIGTERM, SIGQUIT (Windows: os.Interrupt)
  • logger: package default logger

Practical guidance:

  • return errors from callbacks; do not call os.Exit/log.Fatal inside callbacks
  • if startup exits quickly, Orchestrate still waits for signal-triggered shutdown flow
  • for http.Server, treat http.ErrServerClosed as normal shutdown, not a startup failure

Terminating Child Processes

TerminateProcess sends a graceful termination signal and waits up to timeToWait.
If the process has not exited, it force-kills it.

if err := grace.TerminateProcess(cmd.Process, 5*time.Second, logger); err != nil {
	return err
}

API Coverage

Types
  • type OrchestrateOptions
Exported Struct Fields
  • OrchestrateOptions.Logger *slog.Logger
  • OrchestrateOptions.ShutdownCallback func(context.Context) error
  • OrchestrateOptions.ShutdownTimeout time.Duration
  • OrchestrateOptions.Signals []os.Signal
  • OrchestrateOptions.StartupCallback func() error
Functions
  • func Orchestrate(options OrchestrateOptions)
  • func TerminateProcess(process *os.Process, timeToWait time.Duration, logger *slog.Logger) error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Orchestrate

func Orchestrate(options OrchestrateOptions)

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

func TerminateProcess

func TerminateProcess(process *os.Process, timeToWait time.Duration, logger *slog.Logger) error

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

Types

type OrchestrateOptions

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

	// StartupCallback 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.
	StartupCallback func() error

	// ShutdownCallback 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.
	ShutdownCallback func(context.Context) error
}

Jump to

Keyboard shortcuts

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