Implement Graceful Shutdown

by

in

Tip:

Implement graceful shutdown procedures to ensure ongoing requests are completed before exiting.

Example:

javascriptCopy codeconst server = require('http').createServer((req, res) => {
    res.end('Hello World');
});

server.listen(3000);

function shutdown() {
    server.close(() => {
        console.log('Server closed');
        process.exit(0);
    });

    setTimeout(() => {
        console.error('Forcing shutdown');
        process.exit(1);
    }, 10000); // Force shutdown after 10 seconds
}

process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);

Reason: Ensures that active connections are properly closed and resources are released before the application exits.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *