#Does Nestjs handle gracefully shutdown within a docker container

6 messages · Page 1 of 1 (latest)

fluid dome
#

As i know, one of the best practice for nodejs application development is gracefully shutdown and make response to terminal signals like SIGTERM and SIGINT. I want to know is nestjs handles it automaticly or I should add listener to these signals to handle this kind of intruptions.
What I seen so far, is that when i press ctrl+c when nestjs application running within a docker container, it logs "gracefully shutdown..." but i want to get confident about that.
Thank you all.

ivory mountain
#

In your main.ts file you can use app.enableShutdownHooks() on your app instance to enable shutdown hooks.
This means that any module that implements OnApplicationShutdown will run it's OnApplicationShutdown method before the node process is terminated

fluid dome
#

Thank you from your response, Although I can use enableShutdownHooks() but I want to know is there a mechanism in nestjs to handle them properly? As is mentioned, when I terminate docker container which is up using docker compose, I see something logged with the content of "Gracefully shutdown...". I want to know, without enabling shutdown hooks in nestjs, Is there something to handle it? if not, which platform logged that line?

wary rampart
scenic zephyr
#

I don't think there is anything to handle it without calling enableShutDownHooks() .

To my knowledge, calling app.enableShutdownHooks() calls the method listenToShutdownHooks (see here https://github.com/nestjs/nest/blob/7a90b7c9b4e96c85a5f8339d5f72ab5f6bb62526/packages/core/nest-application-context.ts#L309). And this method calls process.on() , which is what listens for OS signals. (see here https://github.com/nestjs/nest/blob/7a90b7c9b4e96c85a5f8339d5f72ab5f6bb62526/packages/core/nest-application-context.ts#L354)

However, any NestJS dependency or any additional dependency you added can call process.on() directly to log this line.

Personally, I think your log Gracefully stopping... (press Ctrl+C again to force) is coming from docker-compose

stark crow
#

I'm pretty sure that's from docker compose itself