✨ Simplify arrow function syntax and remove unnecessary whitespace in graceful shutdown utility
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Graceful Shutdown Handler
|
* Graceful Shutdown Handler
|
||||||
*
|
*
|
||||||
* Handles server shutdown gracefully by:
|
* Handles server shutdown gracefully by:
|
||||||
* - Closing server to stop accepting new connections
|
* - Closing server to stop accepting new connections
|
||||||
* - Waiting for existing connections to finish
|
* - Waiting for existing connections to finish
|
||||||
@@ -25,7 +25,7 @@ export function setupGracefulShutdown(server, options = {}) {
|
|||||||
const connections = new Set();
|
const connections = new Set();
|
||||||
|
|
||||||
// Track all connections
|
// Track all connections
|
||||||
server.on('connection', (connection) => {
|
server.on('connection', connection => {
|
||||||
connections.add(connection);
|
connections.add(connection);
|
||||||
connection.on('close', () => {
|
connection.on('close', () => {
|
||||||
connections.delete(connection);
|
connections.delete(connection);
|
||||||
@@ -54,7 +54,7 @@ export function setupGracefulShutdown(server, options = {}) {
|
|||||||
// Set shutdown timeout
|
// Set shutdown timeout
|
||||||
const shutdownTimeout = setTimeout(() => {
|
const shutdownTimeout = setTimeout(() => {
|
||||||
logger.error(`Shutdown timeout (${timeout}ms), forcing exit`);
|
logger.error(`Shutdown timeout (${timeout}ms), forcing exit`);
|
||||||
connections.forEach((conn) => conn.destroy());
|
connections.forEach(conn => conn.destroy());
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}, timeout);
|
}, timeout);
|
||||||
|
|
||||||
@@ -67,16 +67,16 @@ export function setupGracefulShutdown(server, options = {}) {
|
|||||||
|
|
||||||
// Wait for all connections to close
|
// Wait for all connections to close
|
||||||
logger.info(`Waiting for ${connections.size} connections to close...`);
|
logger.info(`Waiting for ${connections.size} connections to close...`);
|
||||||
|
|
||||||
// Close all idle connections
|
// Close all idle connections
|
||||||
connections.forEach((conn) => {
|
connections.forEach(conn => {
|
||||||
if (!conn.destroyed) {
|
if (!conn.destroyed) {
|
||||||
conn.end();
|
conn.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Give connections time to finish
|
// Give connections time to finish
|
||||||
await new Promise((resolve) => {
|
await new Promise(resolve => {
|
||||||
const checkInterval = setInterval(() => {
|
const checkInterval = setInterval(() => {
|
||||||
if (connections.size === 0) {
|
if (connections.size === 0) {
|
||||||
clearInterval(checkInterval);
|
clearInterval(checkInterval);
|
||||||
@@ -97,7 +97,7 @@ export function setupGracefulShutdown(server, options = {}) {
|
|||||||
|
|
||||||
// Register shutdown handlers for various signals
|
// Register shutdown handlers for various signals
|
||||||
const signals = ['SIGTERM', 'SIGINT', 'SIGUSR2'];
|
const signals = ['SIGTERM', 'SIGINT', 'SIGUSR2'];
|
||||||
signals.forEach((signal) => {
|
signals.forEach(signal => {
|
||||||
process.on(signal, () => shutdown(signal));
|
process.on(signal, () => shutdown(signal));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user