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 ¶
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 ¶
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.
Types ¶
type Component ¶
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 ¶
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 ¶
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).