In Express.js, is there someway of setting a callback function to be executed when the application shuts down?
Procedure of Graceful Shutdown: For this purpose, a SIGTERM (the program manager sends it ) signal is sent to the application that tells it that it is going to be killed. After getting this signal, the app stops accepting the new requests, by letting the load balancer know that is not going to accept any new requests.
log("server started at port 3000"); }); Once the server starts listening, it will never stop until the interrupt signal or a code error crash the program. To stop your NodeJS server from running, you can use the ctrl+C shortcut which sends the interrupt signal to the Terminal where you start the server.
A graceful shutdown is when a computer is turned off by software function and the operating system (OS) is allowed to perform its tasks of safely shutting down processes and closing connections. A hard shutdown is when the computer is forcibly shut down by interruption of power.
You could use the node.js core process 'exit' event like so:
process.on('exit', function() { // Add shutdown logic here. });
Of course, the main event loop will stop running after the exit function returns so you can't schedule any timers or callbacks from within that function (e.g. any I/O must be synchronous).
There is process.on('exit', callback):
process.on('exit', function () { console.log('About to exit.'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With