Skip to content

Commit bdd533a

Browse files
committed
Update documentation for lifecycles hooks.
Describing the context around shutdown hooks and explain that it must be opt-in to work.
1 parent c0a1b1e commit bdd533a

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

content/fundamentals/lifecycle-events.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ The following diagram depicts the sequence of key application lifecycle events,
1414

1515
Lifecycle events happen during application bootstrapping and shutdown. Nest calls registered lifecycle hook methods on `modules`, `injectables` and `controllers` at each of the following lifecycle events (**shutdown hooks** need to be enabled first, as described [below](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown)). As shown in the diagram above, Nest also calls the appropriate underlying methods to begin listening for connections, and to stop listening for connections.
1616

17-
| Lifecycle hook method | Lifecycle event triggering the hook method call |
18-
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
19-
| `onModuleInit()` | Called once the host module's dependencies have been resolved. |
20-
| `onApplicationBootstrap()` | Called once all modules have been initialized, but before listening for connections. |
21-
| `onModuleDestroy()` | Called after a termination signal (e.g., `SIGTERM`) has been received. |
22-
| `beforeApplicationShutdown()` | Called after all `onModuleDestroy()` handlers have completed (Promises resolved or rejected);<br />once complete (Promises resolved or rejected), all existing connections will be closed (`app.close()` called). |
23-
| `onApplicationShutdown()` | Called after connections close (`app.close()` resolves). |
17+
In the following table, `onModuleDestroy`, `beforeApplicationShutdown` and `onApplicationShutdown` are only triggered if you explicitly call `app.close()` or if the process receives a special system signal (such as SIGTERM) and you have correctly called `enableShutdownHooks` at application bootstrap (see below **Application shutdown** part).
18+
19+
| Lifecycle hook method | Lifecycle event triggering the hook method call |
20+
| ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
21+
| `onModuleInit()` | Called once the host module's dependencies have been resolved. |
22+
| `onApplicationBootstrap()` | Called once all modules have been initialized, but before listening for connections. |
23+
| `onModuleDestroy()`\* | Called after a termination signal (e.g., `SIGTERM`) has been received. |
24+
| `beforeApplicationShutdown()`\* | Called after all `onModuleDestroy()` handlers have completed (Promises resolved or rejected);<br />once complete (Promises resolved or rejected), all existing connections will be closed (`app.close()` called). |
25+
| `onApplicationShutdown()`\* | Called after connections close (`app.close()` resolves. |
26+
27+
\* For those events, if you're not calling `app.close()` explicitly, you must opt-in to make them work with system's signals: see `Shutdown application` section below.
2428

2529
#### Usage
2630

@@ -64,28 +68,30 @@ async onModuleInit() {
6468

6569
#### Application shutdown
6670

67-
The `beforeApplicationShutdown()` and `onApplicationShutdown()` hooks are called in the **terminating** phase (in response to system signals such as `SIGTERM`). This feature is often used with [Kubernetes](https://kubernetes.io/), [Heroku](https://www.heroku.com/) or similar services.
68-
69-
> warning **warning** Due to inherent platform limitations, NestJS has limited support for application shutdown hooks on Windows. You can expect `SIGINT` to work, as well as `SIGBREAK` and to some extent `SIGHUP` - [read more](https://nodejs.org/api/process.html#process_signal_events). However `SIGTERM` will never work on Windows because killing a process in the task manager is unconditional, "i.e., there's no way for an application to detect or prevent it". Here's some [relevant documentation](http://docs.libuv.org/en/v1.x/signal.html) from libuv to learn more about how `SIGINT`, `SIGBREAK` and others are handled on Windows. Also, see Node.js documentation of [Process Signal Events](https://nodejs.org/api/process.html#process_signal_events)
71+
The `onModuleDestroy()`, `beforeApplicationShutdown()` and `onApplicationShutdown()` hooks are called in the terminating phase (in response to an explicit call to `app.close()` or upon receipt of system signals such as SIGTERM if opted-in). This feature is often used with [Kubernetes](https://kubernetes.io/) to manage containers' lifecycles, by [Heroku](https://www.heroku.com/) for dynos or similar services.
7072

71-
To use these hooks you must activate a listener which listens to shutdown signals.
73+
Disabled by default for performances optimizations, to use these hooks **you must enable them** by calling `enableShutdownHooks()`, which listens to shutdown signals:
7274

7375
```typescript
7476
import { NestFactory } from '@nestjs/core';
7577
import { AppModule } from './app.module';
7678

7779
async function bootstrap() {
7880
const app = await NestFactory.create(AppModule);
81+
7982
// Starts listening for shutdown hooks
8083
app.enableShutdownHooks();
84+
8185
await app.listen(3000);
8286
}
8387
bootstrap();
8488
```
8589

90+
> warning **warning** Due to inherent platform limitations, NestJS has limited support for application shutdown hooks on Windows. You can expect `SIGINT` to work, as well as `SIGBREAK` and to some extent `SIGHUP` - [read more](https://nodejs.org/api/process.html#process_signal_events). However `SIGTERM` will never work on Windows because killing a process in the task manager is unconditional, "i.e., there's no way for an application to detect or prevent it". Here's some [relevant documentation](http://docs.libuv.org/en/v1.x/signal.html) from libuv to learn more about how `SIGINT`, `SIGBREAK` and others are handled on Windows. Also, see Node.js documentation of [Process Signal Events](https://nodejs.org/api/process.html#process_signal_events)
91+
8692
> info **Info** `enableShutdownHooks` consumes memory by starting listeners. In cases where you are running multiple Nest apps in a single Node process (e.g., when running parallel tests with Jest), Node may complain about excessive listener processes. For this reason, `enableShutdownHooks` is not enabled by default. Be aware of this condition when you are running multiple instances in a single Node process.
8793
88-
When the application receives a termination signal it will call any registered `beforeApplicationShutdown()`, then `onApplicationShutdown()` methods (in the sequence described above) with the corresponding signal as the first parameter. If a registered function awaits an asynchronous call (returns a promise), Nest will not continue in the sequence until the promise is resolved or rejected.
94+
When the application receives a termination signal it will call any registered `onModuleDestroy()`, `beforeApplicationShutdown()`, then `onApplicationShutdown()` methods (in the sequence described above) with the corresponding signal as the first parameter. If a registered function awaits an asynchronous call (returns a promise), Nest will not continue in the sequence until the promise is resolved or rejected.
8995

9096
```typescript
9197
@@filename()

0 commit comments

Comments
 (0)