Kind: Function
Source: packages/core/hooks/before-app-shutdown.hook.ts
Part of: Core
Calls the beforeApplicationShutdown function on the module and its children
(providers / controllers).
callBeforeAppShutdownHook invokes the beforeApplicationShutdown lifecycle hook for a module’s providers, controllers, and module class. It coordinates shutdown hook execution for static dependency trees and forwards the optional operating-system shutdown signal to each eligible instance.
Signature
tsasync function callBeforeAppShutdownHook(module: Module, signal: string): Promise<void>
Parameters
| Name | Type |
|---|---|
module | Module |
signal | string |
Returns: Promise<void>
Diagram
mermaidgraph LR A[Application shutdown signal] --> B[callBeforeAppShutdownHook] B --> C[Module providers] B --> D[Module controllers] B --> E[Module class instance] C --> F[beforeApplicationShutdown(signal)] D --> F E --> F
Usage
tsimport { callBeforeAppShutdownHook } from '@nestjs/core/hooks/before-app-shutdown.hook';
import { ModulesContainer } from '@nestjs/core';
async function runShutdownHooks(
modulesContainer: ModulesContainer,
signal = 'SIGTERM',
) {
for (const moduleRef of modulesContainer.values()) {
await callBeforeAppShutdownHook(moduleRef, signal);
}
}
AI Coding Instructions
- Preserve the lifecycle execution order: providers and controllers should run before the module class hook.
- Always forward the optional shutdown signal, such as
SIGTERMorSIGINT, tobeforeApplicationShutdown. - Await hook execution so asynchronous cleanup tasks, such as closing database connections, finish before shutdown continues.
- Only invoke hooks for eligible static dependency trees; transient or request-scoped instances require the existing instance-selection logic.
- Treat this as framework lifecycle infrastructure—prefer enabling application shutdown hooks rather than calling it directly in application code.
Relationships
- IMPORTS →
BeforeApplicationShutdown - IMPORTS →
isFunction - IMPORTS →
isNil
Was this page helpful?