From 5e8d0fddbd6d0e5d68dc6a172d888b22899751dc Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Wed, 27 Nov 2024 09:44:42 -0800 Subject: [PATCH] IO-2959 Remove debug for crisp status and add sig term handler. --- server.js | 41 +++++++++++++++++++++++++++++++++- server/utils/statusReporter.js | 16 ++++++------- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index bba9edb77..a408742a3 100644 --- a/server.js +++ b/server.js @@ -24,6 +24,8 @@ const { ElastiCacheClient, DescribeCacheClustersCommand } = require("@aws-sdk/cl const { InstanceRegion } = require("./server/utils/instanceMgr"); const StartStatusReporter = require("./server/utils/statusReporter"); +const cleanupTasks = []; +let isShuttingDown = false; const CLUSTER_RETRY_BASE_DELAY = 100; const CLUSTER_RETRY_MAX_DELAY = 5000; const CLUSTER_RETRY_JITTER = 100; @@ -298,7 +300,14 @@ const main = async () => { applyRoutes({ app }); redisSocketEvents({ io: ioRedis, redisHelpers, ioHelpers, logger }); - StartStatusReporter(); + const StatusReporter = StartStatusReporter(); + registerCleanupTask(async () => { + StatusReporter.end(); + }); + + // Add SIGTERM signal handler + process.on("SIGTERM", handleSigterm); + process.on("SIGINT", handleSigterm); // Optional: Handle Ctrl+C try { await server.listen(port); @@ -317,3 +326,33 @@ main().catch((error) => { // Note: If we want the app to crash on all uncaught async operations, we would // need to put a `process.exit(1);` here }); + +// Register a cleanup task +function registerCleanupTask(task) { + cleanupTasks.push(task); +} + +// SIGTERM handler +async function handleSigterm() { + if (isShuttingDown) { + logger.log("sigterm-api", "WARN", null, null, { message: "Shutdown already in progress, ignoring signal." }); + return; + } + + isShuttingDown = true; + + logger.log("sigterm-api", "WARN", null, null, { message: "SIGTERM Received. Starting graceful shutdown." }); + + try { + for (const task of cleanupTasks) { + logger.log("sigterm-api", "WARN", null, null, { message: `Running cleanup task: ${task.name}` }); + + await task(); + } + logger.log("sigterm-api", "WARN", null, null, { message: `All cleanup tasks completed.` }); + } catch (error) { + logger.log("sigterm-api-error", "ERROR", null, null, { message: error.message, stack: error.stack }); + } + + process.exit(0); +} diff --git a/server/utils/statusReporter.js b/server/utils/statusReporter.js index a2e9bdc26..fc425da33 100644 --- a/server/utils/statusReporter.js +++ b/server/utils/statusReporter.js @@ -11,7 +11,7 @@ const InstanceManager = require("../utils/instanceMgr").default; function StartStatusReporter() { //For ImEX Online. - InstanceManager({ + return InstanceManager({ executeFunction: true, args: [], imex: () => { @@ -31,14 +31,14 @@ function StartStatusReporter() { service_id: process.env.CRISP_SERVICE_IDENTIFIER, // Service ID containing the parent Node for Replica (given by Crisp) node_id: process.env.CRISP_NODE_IDENTIFIER, // Node ID containing Replica (given by Crisp) replica_id: getHostNameOrIP(), // Unique Replica ID for instance (ie. your IP on the LAN) - interval: 30, // Reporting interval (in seconds; defaults to 30 seconds if not set) + interval: 30 // Reporting interval (in seconds; defaults to 30 seconds if not set) - console: { - debug: (log_message, data) => logger.log("crisp-status-update", "DEBUG", null, null, { log_message, data }), - log: (log_message, data) => logger.log("crisp-status-update", "DEBUG", null, null, { log_message, data }), - warn: (log_message, data) => logger.log("crisp-status-update", "WARN", null, null, { log_message, data }), - error: (log_message, data) => logger.log("crisp-status-update", "ERROR", null, null, { log_message, data }) - } // Console instance if you need to debug issues, + // console: { + // debug: (log_message, data) => logger.log("crisp-status-update", "DEBUG", null, null, { log_message, data }), + // log: (log_message, data) => logger.log("crisp-status-update", "DEBUG", null, null, { log_message, data }), + // warn: (log_message, data) => logger.log("crisp-status-update", "WARN", null, null, { log_message, data }), + // error: (log_message, data) => logger.log("crisp-status-update", "ERROR", null, null, { log_message, data }) + // } // Console instance if you need to debug issues, }); return crispStatusReporter;