lifecycle

package
v11.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package lifecycle provides a minimal orchestration primitive for running concurrent components that share a single context for cancellation and graceful shutdown via OS signals.

Index

Constants

This section is empty.

Variables

View Source
var AnnounceTimeout = 5 * time.Second

AnnounceTimeout is the maximum time to wait for lifecycle announcements (started/stopping). Announcements are best-effort and must not block the service lifecycle. Set before calling Run; not safe for concurrent modification.

Functions

func Run

func Run(ctx context.Context, args ...any) error

Run orchestrates one or more components. It accepts Component values (or bare func(ctx context.Context) error) and Option values. It creates a context cancelled on SIGTERM or SIGINT, launches every component as a goroutine in an errgroup, and waits for all of them to finish. If any component returns an error the shared context is cancelled, signalling the remaining components to shut down. The first non-nil error (if any) is returned.

When WithKafkaConfig is provided and the config is enabled, Run automatically starts heartbeatkit and announcekit, and shuts them down on exit.

func RunComponents

func RunComponents(ctx context.Context, components ...Component) error

RunComponents is the type-safe variant of Run that accepts only Component values and optional Option values. Prefer this over Run when all components are known at compile time.

Types

type Component

type Component func(ctx context.Context) error

Component is a long-running function that participates in the application lifecycle. It must respect ctx.Done() to allow graceful shutdown.

type Option

type Option func(*options)

Option configures optional lifecycle behavior. Pass Option values alongside Component values in the args to Run.

func WithKafkaConfig

func WithKafkaConfig(cfg kafkakit.Config) Option

WithKafkaConfig enables kafkakit integration. When the config has BootstrapServers set, lifecycle.Run will automatically:

  • Create a kafkakit.Publisher
  • Start heartbeatkit (publishes liveness events)
  • Announce service started/stopping via announcekit

If BootstrapServers is empty the option is silently ignored.

func WithServiceName

func WithServiceName(name string) Option

WithServiceName overrides the service name used by heartbeatkit and announcekit. If not set, the name is resolved from the CHASSIS_SERVICE_NAME environment variable or the working directory basename (same logic as registry).

Jump to

Keyboard shortcuts

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