Skip to content

Commit f821317

Browse files
Merge pull request #1122 from BrunnerLivio/feature/further-clarification-shutdown-hooks
Feature/further clarification shutdown hooks
2 parents 71c55dc + 7ce850e commit f821317

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

content/fundamentals/lifecycle-events.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following diagram depicts the sequence of key application lifecycle events,
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

1717
| Lifecycle hook method | Lifecycle event triggering the hook method call |
18-
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18+
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1919
| `onModuleInit()` | Called once the host module's dependencies have been resolved. |
2020
| `onApplicationBootstrap()` | Called once all modules have been initialized, but before listening for connections. |
2121
| `onModuleDestroy()` | Called after a termination signal (e.g., `SIGTERM`) has been received. |
@@ -66,6 +66,8 @@ async onModuleInit() {
6666

6767
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.
6868

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)
70+
6971
To use these hooks you must activate a listener which listens to shutdown signals.
7072

7173
```typescript
@@ -81,6 +83,8 @@ async function bootstrap() {
8183
bootstrap();
8284
```
8385

86+
> 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.
87+
8488
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.
8589

8690
```typescript

0 commit comments

Comments
 (0)