CoordinatedShutdown
CoordinatedShutdown manages graceful application shutdown by executing registered tasks across defined phases.
It listens for termination signals (SIGINT, SIGTERM) and runs tasks in parallel within each phase. Tasks can be added or removed dynamically, and each task has an associated timeout.
class CoordinatedShutdown extends CoordinatedLifecycle<
ShutdownPhase,
CoordinatedShutdownEvents
>class CoordinatedShutdown extends CoordinatedLifecycle<
ShutdownPhase,
CoordinatedShutdownEvents
>Member visibility
Filter class members by access level.
Showing members with Protected visibility and higher.
logger
Inherited from:CoordinatedLifecycle
phaseEnum
phaseOrder
protected readonly phaseOrder: TPhase[]protected readonly phaseOrder: TPhase[]Inherited from:CoordinatedLifecycle
tasksMap
protected readonly tasksMap: Map<TPhase, LifecycleTask[]>protected readonly tasksMap: Map<TPhase, LifecycleTask[]>Inherited from:CoordinatedLifecycle
addListener()
Alias of onon for compatibility with Node.js EventEmitter APIs.
eventThe event name to attach to
listenerCallback operating on the typed argument tuple for the event
Inherited from:StrictEventEmitter
phaseThe shutdown phase from ShutdownPhaseShutdownPhase
taskNameUnique identifier for the task
taskAsync function to execute
timeoutMsTask timeout in milliseconds
emit()
Emits an event with the strictly typed argument tuple for the event name.
eventThe event name to emit
argsTuple payload for the event
Inherited from:StrictEventEmitter
eventNamesTyped()
Returns the list of event names known to the emitter with the mapped key type.
eventNamesTyped(): SEEventKey<TEvents>[]eventNamesTyped(): SEEventKey<TEvents>[]Inherited from:StrictEventEmitter
protected async executeTasksInPhase(
phase: ShutdownPhase,
tasks: LifecycleTask[]
): Promise<PromiseSettledResult<void>[]>protected async executeTasksInPhase(
phase: ShutdownPhase,
tasks: LifecycleTask[]
): Promise<PromiseSettledResult<void>[]>listenerCountTyped()
Counts listeners for an event without widening the return type of listenerCountlistenerCount.
eventThe event name to inspect
Inherited from:StrictEventEmitter
listeners()
Retrieves the listener list for a given event with the correct tuple signature.
eventThe event name to inspect
Inherited from:StrictEventEmitter
off()
Removes a previously registered listener for the given event.
eventThe event name whose listener should be removed
listenerCallback originally registered for the event
Inherited from:StrictEventEmitter
on()
Registers a persistent listener with tuple-safe arguments for the given event.
eventThe event name to attach to
listenerCallback operating on the typed argument tuple for the event
Inherited from:StrictEventEmitter
once()
Registers a one time listener that is removed after the first invocation.
eventThe event name to attach to
listenerCallback operating on the typed argument tuple for the event
Inherited from:StrictEventEmitter
removeListener()
Alias of offoff for compatibility with Node.js EventEmitter APIs.
eventThe event name whose listener should be removed
listenerCallback originally registered for the event
Inherited from:StrictEventEmitter
public removeTask(
phase: ShutdownPhase,
taskName: string
): booleanpublic removeTask(
phase: ShutdownPhase,
taskName: string
): booleanphaseThe shutdown phase to remove from
taskNameName of the task to remove
runPhase()
Run all tasks in a specific phase
Inherited from:CoordinatedLifecycle
runTaskWithTimeout()
Run a single task with timeout
protected async runTaskWithTimeout(
phase: TPhase,
task: LifecycleTask
): Promise<void>protected async runTaskWithTimeout(
phase: TPhase,
task: LifecycleTask
): Promise<void>Inherited from:CoordinatedLifecycle
waitFor()
Waits for an event to be emitted, resolving with the listener arguments tuple once triggered. Supports optional abort signals and timeouts for cancellation semantics.
async waitFor<TEventKey>(
event: TEventKey,
opts?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<TEvents[TEventKey]>async waitFor<TEventKey>(
event: TEventKey,
opts?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<TEvents[TEventKey]>eventThe event name to wait for
optsOptional abort signal or timeout in milliseconds
Inherited from:StrictEventEmitter