Lifecycle
Start and stop runtime resources with the application context.
Components that implement runtime.Lifecycle participate in application
startup and shutdown. Use lifecycle for resources that must be explicitly
started after the component graph is ready and stopped before singleton cleanup.
type Worker struct {
running bool
}
func NewWorker() *Worker {
return &Worker{}
}
func (w *Worker) Start(ctx context.Context) error {
w.running = true
return nil
}
func (w *Worker) Stop(ctx context.Context) error {
w.running = false
return nil
}
func (w *Worker) IsRunning() bool {
return w.running
}func init() {
component.Register(NewWorker)
}Startup
During context startup, the lifecycle manager resolves lifecycle components from
the container and calls Start.
Use lifecycle components for resources that should begin with the application: background workers, subscriptions, connection managers, schedulers, and other long-running infrastructure.
Start should not do dependency lookup or configuration parsing. Put
dependencies in the constructor and typed settings in configuration structs, so
startup only starts the already-built resource.
Shutdown
During shutdown, the lifecycle manager calls Stop on running lifecycle
components. Keep Stop idempotent and bounded so application shutdown can
finish cleanly.
If a component also implements component.Disposable, disposal happens when
singletons are destroyed. Use Stop for runtime activity and Dispose for
final resource cleanup.
Custom lifecycle manager
Procyon provides a default lifecycle manager. Advanced integrations can provide
a custom runtime.LifecycleManager component when startup or shutdown ordering
needs framework-level control.
