# CoordinatedShutdown

`class` in `services` · v0.8.2

<https://docs.seedcord.org/packages/services/0.8.2/classes/coordinated-shutdown>

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.

```ts
class CoordinatedShutdown extends CoordinatedLifecycle<
    ShutdownPhase,
    CoordinatedShutdownEvents
>
```

## Constructors

### constructor

```ts
CoordinatedShutdown(enabled?: boolean)
```

Constructs a new instance of the `CoordinatedShutdown` class

## Properties

### logger

```ts
protected readonly logger: Logger
```

### phaseEnum

```ts
protected readonly phaseEnum: Record<number, string>
```

### phaseOrder

```ts
protected readonly phaseOrder: TPhase[]
```

### tasksMap

```ts
protected readonly tasksMap: Map<TPhase, LifecycleTask[]>
```

## Methods

### addListener

```ts
addListener<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Alias of [`on`](/packages/services/0.8.2/classes/strict-event-emitter#on) for compatibility with Node.js EventEmitter APIs.

### addTask

```ts
public addTask(
    phase: ShutdownPhase,
    taskName: string,
    task: () => Promise<void>,
    timeoutMs?: number
): void
```

Adds a task to a specific shutdown phase with timeout.

### canAddTask

```ts
protected canAddTask(): boolean
```

### canRemoveTask

```ts
protected canRemoveTask(): boolean
```

### emit

```ts
emit<TEventKey>(
    event: TEventKey,
    args: TEvents[TEventKey]
): boolean
```

Emits an event with the strictly typed argument tuple for the event name.

### eventNamesTyped

```ts
eventNamesTyped(): SEEventKey<TEvents>[]
```

Returns the list of event names known to the emitter with the mapped key type.

### executeTasksInPhase

```ts
protected async executeTasksInPhase(
    phase: ShutdownPhase,
    tasks: LifecycleTask[]
): Promise<PromiseSettledResult<void>[]>
```

### getTaskType

```ts
protected getTaskType(): string
```

### listenerCountTyped

```ts
listenerCountTyped<TEventKey>(event: TEventKey): number
```

Counts listeners for an event without widening the return type of listenerCount.

### listeners

```ts
listeners<TEventKey>(
    event: TEventKey
): ((...args: TEvents[TEventKey]) => void)[]
```

Retrieves the listener list for a given event with the correct tuple signature.

### off

```ts
off<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Removes a previously registered listener for the given event.

### on

```ts
on<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a persistent listener with tuple-safe arguments for the given event.

### once

```ts
once<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a one time listener that is removed after the first invocation.

### removeListener

```ts
removeListener<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Alias of [`off`](/packages/services/0.8.2/classes/strict-event-emitter#off) for compatibility with Node.js EventEmitter APIs.

### removeTask

```ts
public removeTask(
    phase: ShutdownPhase,
    taskName: string
): boolean
```

Removes a task from a specific shutdown phase.

### run

```ts
public async run(
    exitCode?: number,
    exitProcess?: boolean
): Promise<void>
```

Executes the coordinated shutdown sequence.

Runs all registered tasks across shutdown phases in reverse order. Tasks within each phase are executed in parallel for faster shutdown. Process exits with the specified code when complete.

### runPhase

```ts
protected async runPhase(phase: TPhase): Promise<void>
```

Run all tasks in a specific phase

### runTaskWithTimeout

```ts
protected async runTaskWithTimeout(
    phase: TPhase,
    task: LifecycleTask
): Promise<void>
```

Run a single task with timeout

### waitFor

```ts
async waitFor<TEventKey>(
    event: TEventKey,
    opts?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<TEvents[TEventKey]>
```

Waits for an event to be emitted, resolving with the listener arguments tuple once triggered. Supports optional abort signals and timeouts for cancellation semantics.
