UNPKG

@sidequest/engine

Version:

@sidequest/engine is the core engine of SideQuest, a distributed background job processing system for Node.js and TypeScript.

54 lines (50 loc) 1.72 kB
'use strict'; var core = require('@sidequest/core'); let shuttingDown = false; /** * Handles the shutdown process, ensuring it only runs once and logs appropriately. * @param fn The async function to run during shutdown. * @param tag A label for logging. * @param signal The signal that triggered shutdown. */ async function shutdown(fn, tag, signal) { try { if (!shuttingDown) { shuttingDown = true; core.logger("Engine").info(`[${tag}] Received ${signal}. Shutting down gracefully...`); await fn(); } else { core.logger("Engine").info(`[${tag}] Received ${signal} while already shutting down. Forcing shutdown.`); } core.logger("Engine").info(`[${tag}] Shutdown complete.`); process.exit(0); } catch (error) { core.logger("Engine").error(`[${tag}] Error during shutdown:`, error); process.exit(1); } } /* eslint-disable @typescript-eslint/no-misused-promises */ /** * Registers handlers for SIGINT and SIGTERM to gracefully shut down the process. * @param fn The async function to run during shutdown. * @param tag A label for logging. */ function gracefulShutdown(fn, tag, enabled) { if (enabled) { process.on("SIGINT", async () => { await shutdown(fn, tag, "SIGINT"); }); process.on("SIGTERM", async () => { await shutdown(fn, tag, "SIGTERM"); }); } } function clearGracefulShutdown() { process.removeAllListeners("SIGINT"); process.removeAllListeners("SIGTERM"); } exports.clearGracefulShutdown = clearGracefulShutdown; exports.gracefulShutdown = gracefulShutdown; //# sourceMappingURL=shutdown.cjs.map