nest
nest copied to clipboard
feat(microservices): add gracefull shutdown option for nats server
PR Checklist
Please check if your PR fulfills the following requirements:
- [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Other... Please describe:
What is the current behavior?
When using NATS transport for request/reply, there is no option for gracefully shutting down the server.
The NATS server close method implementation calls natsClient.close() and nullifies the client, meaning all the ongoing requests will be dropped.
Issue Number: #13920
What is the new behavior?
Two options are added to NatsOptions, gracefulShutdown and gracePeriod. If gracefulShutdown flag is provided, when calling the close method of ServerNats, first it will unsubscribe all the subscribed topics so the ServerNats will accept no new message. Also, it will wait for gracePeriod and then try to nullify the client.
When the shutdown hook is enabled, the app.close will be executed when some signal is sent to the app. The app.close first calls the server.close and in the case of ServerNats and described behavior leads to processing all ongoing requests.
Does this PR introduce a breaking change?
- [ ] Yes
- [x] No
Other information
Pull Request Test Coverage Report for Build c709f725-a1f9-4942-825f-149fab1d83f1
Details
- 14 of 17 (82.35%) changed or added relevant lines in 2 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage decreased (-0.03%) to 92.186%
| Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
|---|---|---|---|
| packages/microservices/server/server-nats.ts | 13 | 16 | 81.25% |
| <!-- | Total: | 14 | 17 |
| Totals | |
|---|---|
| Change from base Build df6d3a45-faa0-4d3b-9635-9533bb6c4560: | -0.03% |
| Covered Lines: | 6760 |
| Relevant Lines: | 7333 |
💛 - Coveralls
Hey Kamil. I hope you are doing well
Any update about this pull request? @kamilmysliwiec
Thank you.