I need to close some db connections as soon as my application is killed.I decided to close the db connections in onModuleDestroy function but it never gets called.I have implemented the interface for onModuleDestroy in my class though.
Anything I'm missing here?
Please let me know will onModuleDestroy () not be called when I kill my application?
Did you add app.enableShutdwonHooks() in your main.ts?
Nopes I didn't. Also do I need to add listenasync instead listen? Both seems to be same to me.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Jay McDoniel notifications@github.com
Sent: Friday, September 6, 2019 1:34:06 AM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
Did you add app.enableShutdwonHooks() in your main.ts?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JDNJTDYUPIBHXQJAHDQIFQ3NA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6ASJQA#issuecomment-528557248, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JAKJKVT64KF52T7QP3QIFQ3NANCNFSM4IUBIARQ.
Apologies, shutdown hooks are not necessarily needed, it should be okay with just OnModuleDestroy which gets called with app.close(). Do you have a repo showing the problem?
I don't want to call app.close() explicitly as I want onModuleDestroy should be called on app killed through let's say console etc.
Do you really need the repo as my use case is straight forward.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Jay McDoniel notifications@github.com
Sent: Friday, September 6, 2019 1:44:40 AM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
Apologies, shutdown hooks are not necessarily needed, it should be okay with just OnModuleDestroy which gets called with app.close(). Do you have a repo showing the problem?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JGAWLK7ATKN3HTIM33QIFSDBA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6ATS6Q#issuecomment-528562554, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JHZ5UMGOC625MVSJL3QIFSDBANCNFSM4IUBIARQ.
Apparently, when I call app.close() after app.listen() onModuleDestroy () does get called but my app is also killed which is what don't to kill the app through code rather it when killed abruptly or deliberately by ctrl+ c in terminal onModuleDestroy should be called.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Amit Dhawan amit.dhawan@outlook.com
Sent: Friday, September 6, 2019 1:46:20 AM
To: nestjs/nest reply@reply.github.com; nestjs/nest nest@noreply.github.com
Cc: Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
I don't want to call app.close() explicitly as I want onModuleDestroy should be called on app killed through let's say console etc.
Do you really need the repo as my use case is straight forward.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Jay McDoniel notifications@github.com
Sent: Friday, September 6, 2019 1:44:40 AM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
Apologies, shutdown hooks are not necessarily needed, it should be okay with just OnModuleDestroy which gets called with app.close(). Do you have a repo showing the problem?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JGAWLK7ATKN3HTIM33QIFSDBA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6ATS6Q#issuecomment-528562554, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JHZ5UMGOC625MVSJL3QIFSDBANCNFSM4IUBIARQ.
If your use case is for ctrl + c then that is a SIGKILL and should be handled with the shutdown hooks instead of the module destroy events. Here is some more information about application shutdown
Another use case is app is running in a container on k8s pod and we bring down the container.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Jay McDoniel notifications@github.com
Sent: Friday, September 6, 2019 1:53:37 AM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
If your use case is for ctrl + c then that is a SIGKILL and should be handled with the shutdown hooks instead of the module destroy events. Here is some more information about application shutdownhttps://docs.nestjs.com/fundamentals/lifecycle-events#onapplicationshutdown
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JF46EIDLHMMTVBLWJ3QIFTETA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6AUMNI#issuecomment-528565813, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JFN6ZDAFVKOTY72YO3QIFTETANCNFSM4IUBIARQ.
K8s and Heroku are also mentioned as use cases for using application shutdown hooks. It's mentioned in the link I posted
Ok..Thanks. So that means I need to use app shutdown events rather than onModuleDestroy.
Last question:- what's the use case for onModuleDestroy then?
Get Outlook for Androidhttps://aka.ms/ghei36
From: Jay McDoniel notifications@github.com
Sent: Friday, September 6, 2019 1:57:13 AM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Author author@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
K8s and Heroku are also mentioned as use cases for using application shutdown hooks. It's mentioned in the link I posted
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JHH5274RYA3YFX5PLLQIFTSDA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6AUXFQ#issuecomment-528567190, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JGM6ECWCJLV3O6TGPLQIFTSDANCNFSM4IUBIARQ.
@amit12cool It also often depends on the integrations. For example, if you use nestjs/typeorm, then the DB closes on onApplicationShutdown (currently it is still using onModuleDestroy but we are going to change it to onApplicationShutdown). With the upcoming version of nestjs/terminus, all HTTP routes would start to send 503 responses on beforeApplicationShutdown. Depending on what you are trying to achieve, you’d need to place it into the correct lifecycle event. For instance, if you’d need to do DB operations, right before the application closes, you’d go for beforeApplicationShutdown. Maybe you’ll need to do other operations, while HTTP still is able to respond with 2xx status code, then you’d do it in onModuleDestroy. @johnbiundo and I will improve the docs, so you’ll have a better understanding on when to use what lifecycle event :)
In general; if you do not need to do super special operations on shutdown (e.g. graceful shutdown), you can use onModuleDestroy. before-/onApplicationShutdown are primarily useful for k8s stuff or similar stuff.
*beforeApplicationShutdown is not documented yet. It is basically the same as onApplicationShutdown, but it just gets called before it. Not anything special NestJS does in-between.
Also since onModuleDestroy and onApplicationShutdown() are not promise based how can i call my promise based functions in onModuleDestroy or onApplicationShutDown functions?
Is below the correct way to do it:-
async onApplicationShutdown(signal?: string) : Promise<void>{
await Promise.all(this.eventHubClients.map(async (client) => {
await client.close();
}));
Logger.log(`Application shutdown. Event received: ${signal}`);
}
@amit12cool what do you mean with not promise based? As you can see here we await all the hooks, so your example should work fine.
Closing this issue since it is not a bug of Nest.
This works. Thanks for your replies.
Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10
From: Livio Brunner notifications@github.com
Sent: Friday, September 6, 2019 9:36:52 PM
To: nestjs/nest nest@noreply.github.com
Cc: amit12cool amit.dhawan@outlook.com; Mention mention@noreply.github.com
Subject: Re: [nestjs/nest] onModuleDestroy() not called (#2893)
@amit12coolhttps://github.com/amit12cool what do you mean with not promise based? As you can see here we await all the hooks, so your example should work fine.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/nestjs/nest/issues/2893?email_source=notifications&email_token=ABTB2JBO3PB5ZNIX6YCN3QLQIJ5ZZA5CNFSM4IUBIAR2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6DJVXY#issuecomment-528915167, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABTB2JFMOD5D3TVJ2XGM5BTQIJ5ZZANCNFSM4IUBIARQ.
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.
Most helpful comment
@amit12cool It also often depends on the integrations. For example, if you use nestjs/typeorm, then the DB closes on
onApplicationShutdown(currently it is still usingonModuleDestroybut we are going to change it toonApplicationShutdown). With the upcoming version ofnestjs/terminus, all HTTP routes would start to send 503 responses onbeforeApplicationShutdown. Depending on what you are trying to achieve, you’d need to place it into the correct lifecycle event. For instance, if you’d need to do DB operations, right before the application closes, you’d go forbeforeApplicationShutdown. Maybe you’ll need to do other operations, while HTTP still is able to respond with 2xx status code, then you’d do it inonModuleDestroy. @johnbiundo and I will improve the docs, so you’ll have a better understanding on when to use what lifecycle event :)In general; if you do not need to do super special operations on shutdown (e.g. graceful shutdown), you can use
onModuleDestroy.before-/onApplicationShutdownare primarily useful for k8s stuff or similar stuff.*beforeApplicationShutdown is not documented yet. It is basically the same as onApplicationShutdown, but it just gets called before it. Not anything special NestJS does in-between.