Documents injecute 1.x · Migrating from 0.x
Skip to content

Function: startLifecycle() ​

startLifecycle<C, S>(container, options?): Promise<RunningLifecycle>

Defined in: src/utils/lifecycle.ts:685

Starts the application: runs the lifecycle hooks of every stage, stage by stage, and returns a handle to stop them.

  • A hook is a singleton registered under a stage key (lifecycle.start('server'), the key 'server:start'). Its factory is the hook; it may return a function that undoes it. Hooks are found in the container and its namespaces ('Payments.consumer:start'), and run in registration order: one by one, or all at once for the stages in concurrent.
  • Failures: when a hook fails, the hooks that ran are undone in reverse, the container is disposed (unless dispose: false) and the promise rejects with the first failure.
  • Stopping: running.stop() undoes the hooks in reverse, the last stage first, then disposes the container.

Run it on the composition root (in tests, on an isolated fork). A lifecycle runs once per container: calling it again returns the first call's promise (its options are ignored), also when that start failed; after stop(), it rejects with INJECUTE_LIFECYCLE_STOPPED. Invalid options and hooks reject before anything runs, and leave the container as it is.

Type Parameters ​

C ​

C extends LifecycleContainer

S ​

S extends string = "start" | "init" | "ready"

Parameters ​

container ​

C & CheckHooks<C, S>

options? ​

StartLifecycleOptions<S>

Returns ​

Promise<RunningLifecycle>

Throws ​

(as a rejection) INJECUTE_INVALID_HOOK when a hook is not a singleton or depends on another hook, INJECUTE_RESOLUTION_FAILED when a hook fails or the signal aborts, INJECUTE_LIFECYCLE_STOPPED after stop(), INJECUTE_INVALID_OPTION for invalid options.

Example ​

ts
const running = await startLifecycle(app); // init → start → ready
// undo ready → start → init, then app.dispose(); stop() rejects when something failed to stop
process.once('SIGTERM', () => running.stop().catch((error) => logger.error(error)));

Released under the MIT License.