-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): Add beforeApplicationShutdown hook #2567
feat(core): Add beforeApplicationShutdown hook #2567
Conversation
32f5004
to
fc37541
Compare
@@ -81,8 +83,12 @@ export class NestApplicationContext implements INestApplicationContext { | |||
return this; | |||
} | |||
|
|||
protected async stopServer(): Promise<void> {} | |||
|
|||
public async close(): Promise<void> { | |||
await this.callDestroyHook(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onModuleDestroy
hook gets now called before the server stops taking any requests. Before this PR it was called afterward. Is that a problem? I thought the following order makes more sense, that is why I call onModuleDestroy
before now.
onModuleDestroy
beforeApplicationShutdown
onApplicationShutdown
Pull Request Test Coverage Report for Build 4157
💛 - Coveralls |
Does the shutdown lifecycle hooks react to |
@marcus-sa the This is why we have added nest/packages/core/nest-application-context.ts Lines 102 to 125 in fc99ccd
I can extend the docs further, once this PR gets merged |
@@ -220,19 +232,6 @@ export class NestApplication extends NestApplicationContext | |||
}); | |||
} | |||
|
|||
public async close(): Promise<any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why close()
is removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still exists on NestApplicationContext
and behaves the same way as before.
NestApplicationContext.close()
will just call NestApplicationContext.stopServer()
, which in the case of NestApplicationContext
will be a noop Promise. Because of inheritance NestApplication
will override NestApplicationContext.stopServer
and stop the actual server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see await this.stopServer();
in NestApplicationContext
now, thanks! I missed that one.
Nonetheless, NestApplicationContext
was intended to be platform-agnostic = term "server" will not always exist in case of just "context-based" applications. Maybe we should adjust the naming here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe disposeResources()
or just dispose()
? In the case of HTTP app, resource = server
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dispose()
sounds alright. It is an internal method so we can still change it if we come up with something better :)
fc37541
to
b81582d
Compare
@kamilmysliwiec changed |
b81582d
to
21325a3
Compare
🤔 |
21325a3
to
0f1124c
Compare
0f1124c
to
351c780
Compare
Rebased with changes from #2790: Checks @kamilmysliwiec any reason why this PR has not been merged yet? |
351c780
to
106cdcc
Compare
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
At the moment only
OnApplicationShutdown
exists, which gets called once the server stops taking any requests.What is the new behavior?
BeforeApplicationShutdown
gets called once an e.g. system signal got sent to the process, but the app still accepts any requests, as long as allBeforeApplicationShutdown
-Promises have been resolved.Does this PR introduce a breaking change?
Other information
The shutdown order
Being able to still process requests, even if a system signal has been sent, is crucial in Kubernetes.
@nestjs/terminus
can be further improved once this PR gets merged. As soon asbeforeApplicationShutdown
gets called, Terminus will send unhealthy responses, notifying Kubernetes to switch out the Pod.I will update the docs once this PR has been merged :)