Documentation
¶
Overview ¶
Package app owns the process lifecycle: resources start together and close in reverse order, so a worker that fails cannot suppress the cleanup of the connections it was using.
Index ¶
- Variables
- func Shutdown(ctx context.Context, resources ...Resource) error
- type CloseFunc
- type Lifecycle
- func (lifecycle *Lifecycle) Add(resource Resource) error
- func (lifecycle *Lifecycle) AddCloser(name string, closer interface{ ... }) error
- func (lifecycle *Lifecycle) AddContextCloser(name string, closer interface{ ... }) error
- func (lifecycle *Lifecycle) Context() context.Context
- func (lifecycle *Lifecycle) Go(name string, run RunFunc) error
- func (lifecycle *Lifecycle) Shutdown(ctx context.Context) error
- func (lifecycle *Lifecycle) Wait() error
- type Resource
- type RunFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLifecycleClosed reports an attempt to add work after shutdown began. ErrLifecycleClosed = errors.New("application lifecycle is closed") // ErrLifecycleStarted reports an attempt to add work after waiting began. ErrLifecycleStarted = errors.New("application lifecycle has already started") )
Functions ¶
Types ¶
type CloseFunc ¶
CloseFunc closes one application-owned resource. The context bounds work such as a worker's graceful SHUTDOWN; resources that do not need it can ignore it through Lifecycle.AddCloser.
type Lifecycle ¶
type Lifecycle struct {
// contains filtered or unexported fields
}
Lifecycle coordinates long-lived loops and their owned resources. A caller registers resources in shutdown order: MCP ingress, watcher, worker, connections, and LadybugDB/storage. Run loops are stopped by cancelling Context and are waited after resource close.
func NewLifecycle ¶
NewLifecycle creates an application lifecycle rooted at parent. A nil parent is treated as context.Background.
func (*Lifecycle) Add ¶
Add registers a close operation. Resources must be added in the order they must close, not in reverse construction order.
func (*Lifecycle) AddContextCloser ¶
func (lifecycle *Lifecycle) AddContextCloser(name string, closer interface { Close(context.Context) error }) error
AddContextCloser registers a resource whose close operation accepts a context, such as tsworker.Supervisor.
func (*Lifecycle) Go ¶
Go starts one long-lived loop. Calls after Wait or Shutdown are rejected so the wait group cannot be extended while it is being drained.
func (*Lifecycle) Shutdown ¶
Shutdown cancels all loops, closes resources in registration order, and waits for the loops to exit. It is idempotent and safe for concurrent callers. The first caller supplies the context used by close operations; later callers may use a different context to bound their own wait for that same shutdown.
type Resource ¶
Resource is one close operation in shutdown order. Resources are closed in the order supplied to Shutdown: ingress first, then its producers and finally storage. That ordering is intentional; reverse-registration order would let new work arrive while its dependencies were already closing.