Skip to content

Supervisor

Supervisor<E> = object

Defined in: Core/Supervisor.ts:28

Keeps a fallible task running for the life of a process, restarting it under a retry policy.

One run of the task is one attempt. A successful attempt ends the lifetime as Stopped("completed"). A failed attempt is restarted after restart.getDelay while the policy’s when accepts the error and attempts remain; otherwise the lifetime settles with that error and state Failed.

const outbox = Supervisor.make(processOutbox, {
  restart: RetryPolicy.exponential({ attempts: 5, initial: Duration.seconds(1), jitter: true }),
});

const lifetime = outbox.run();
process.on("SIGTERM", () => outbox.abort());

E = never

readonly abort: () => void

Defined in: Core/Supervisor.ts:49

Stops the lifetime and waits for the running attempt to settle.

A pending backoff is cancelled immediately; a running attempt receives an aborted signal and ends the lifetime as Stopped("aborted") when it settles. await lifetime is the shutdown wait.

void

Supervisor.run to start the lifetime.


readonly run: (options?) => Deferred<Result<E, Stopped>>

Defined in: Core/Supervisor.ts:38

Starts the lifetime and settles when it ends.

A second call while the lifetime is active returns the same deferred. After the lifetime ends, a call starts a new one. Aborting the caller’s signal has the same effect as abort.

Optional cancellation for the lifetime.

AbortSignal

Deferred<Result<E, Stopped>>

Supervisor.abort to stop the lifetime explicitly.


readonly state: () => State<E>

Defined in: Core/Supervisor.ts:56

Returns the current lifecycle state.

State<E>

Supervisor.subscribe to react to changes instead of reading them.


readonly subscribe: (listener) => () => void

Defined in: Core/Supervisor.ts:66

Observes state transitions. Listeners run as the state changes, the final one after the lifetime settles, and they must not throw.

(state) => void

The callback invoked with each new state.

() => void

Supervisor.state to read the current state without subscribing.