Wei Liang b5a93364c5
feat(core): add shutdown lifecycle hook to node executor (#27354)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
When the application are received a shutdown signal, the application
doesn't execute before shutdown functions and directly shutdown whole
application. The situation cannot execute before shutdown functions like
[NestJS Lifecycle
Events](https://docs.nestjs.com/fundamentals/lifecycle-events) and
custom shutdown hooks.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The application can run shutdown hooks like below output:

 NX   Successfully ran target build for project nest-test (5s)

Debugger listening on
ws://localhost:9229/e4bd44c0-9a6a-468a-8b46-b6fef1cef1c7
For help, see: https://nodejs.org/en/docs/inspector

```
 NX   Successfully ran target build for project nest-test (4s)

Debugger listening on ws://localhost:9229/75c8449b-43a4-4d8b-88c0-231761d7248c
For help, see: https://nodejs.org/en/docs/inspector

To exit the process with SIGINT, press Ctrl+C
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG [NestFactory] Starting Nest application...
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG [InstanceLoader] AppModule dependencies initialized +10ms
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG [RoutesResolver] AppController {/api}: +7ms
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG [RouterExplorer] Mapped {/api, GET} route +3ms
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG [NestApplication] Nest application successfully started +2ms
[Nest] 393107  - 08/09/2024, 6:31:26 PM     LOG 🚀 Application is running on: http://localhost:3000/api
[Nest] 393107  - 08/09/2024, 6:31:29 PM     LOG onModuleDestroy
onModuleDestroy: 5.001s
[Nest] 393107  - 08/09/2024, 6:31:34 PM     LOG beforeApplicationShutdown SIGINT
beforeApplicationShutdown: 5.004s
[Nest] 393107  - 08/09/2024, 6:31:39 PM     LOG onApplicationShutdown SIGINT
onApplicationShutdown: 5.005s

 NX  Process exited with code 130, waiting for changes to restart...
```

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #9237 and #18037

---------

Co-authored-by: Jack Hsu <jack.hsu@gmail.com>
2024-08-16 14:23:18 -04:00
..

Nx - Smart Monorepos · Fast CI

{{links}}


Nx: Smart Monorepos · Fast CI

Nx is a build system with built-in tooling and advanced CI capabilities. It helps you maintain and scale monorepos, both locally and on CI.

This package is a JavaScript/TypeScript plugin for Nx.

{{content}}