improve context ux, prevent duplicate setup & teardown

This commit is contained in:
Elijah Duffy
2025-06-04 17:26:12 -07:00
parent 6e7acc758e
commit 1cb207964c
2 changed files with 55 additions and 10 deletions

View File

@@ -54,14 +54,6 @@ func NewLifecycle(modules ...*Module) *Lifecycle {
}
}
// LifecycleToContext adds the Lifecycle to the context.
func LifecycleToContext(ctx context.Context, lifecycle *Lifecycle) context.Context {
if lifecycle == nil {
return ctx
}
return context.WithValue(ctx, lifecycleContextKey, lifecycle)
}
// LifecycleFromContext retrieves the Lifecycle from the context. Returns nil if not found.
func LifecycleFromContext(ctx context.Context) *Lifecycle {
if lifecycle, ok := ctx.Value(lifecycleContextKey).(*Lifecycle); ok {
@@ -70,6 +62,14 @@ func LifecycleFromContext(ctx context.Context) *Lifecycle {
return nil
}
// Context adds the Lifecycle to a context and returns the new context.
func (app *Lifecycle) Context(ctx context.Context) context.Context {
if app == nil {
return ctx
}
return context.WithValue(ctx, lifecycleContextKey, app)
}
// WithOpts sets the options for the lifecycle.
func (app *Lifecycle) WithOpts(opts LifecycleOpts) *Lifecycle {
app.opts = opts
@@ -101,6 +101,10 @@ func (app *Lifecycle) Logger() *slog.Logger {
// if dependencies are not satisfied. Lifecycle.Teardown should always be run at the end
// of the application lifecycle to ensure all resources are cleaned up properly.
func (app *Lifecycle) Setup() error {
if app.setupCount > 0 {
return fmt.Errorf("lifecycle already set up, cannot set up again")
}
for _, mod := range app.modules {
if err := app.setupSingle(nil, mod); err != nil {
return err
@@ -118,6 +122,10 @@ func (app *Lifecycle) Setup() error {
// to ensure all resources are cleaned up properly. All module tear down
// errors are returned as a single error (non-blocking).
func (app *Lifecycle) Teardown() error {
if app.teardownCount > 0 {
return fmt.Errorf("lifecycle already torn down, cannot tear down again")
}
var err error
for i := len(app.modules) - 1; i >= 0; i-- {
if singleErr := app.teardownSingle(app.modules[i]); singleErr != nil {