UNPKG

@anyme/anymejs

Version:
63 lines (58 loc) 2.06 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var index = require('../_virtual/index5.js'); class GracefulExit { logger; isRegistered = false; isShuttingDown = false; cleanupTasks = new Set(); constructor(logger) { this.logger = logger; } register(server, options) { if (this.isRegistered) return this; if (!server.listening) throw new Error("Server Not Listening"); this.isRegistered = true; this.setupProcessHandlers(); index.terminusExports.createTerminus(server, { logger: this.logger.error, timeout: options?.timeout || 30000, signals: options?.signals || ["SIGINT", "SIGTERM", "exit"], healthChecks: options?.healthCheck || {}, onSignal: async () => await this.handleSignal("🚦 Received termination signal"), onShutdown: async () => { this.logger.info("✅ Server gracefully shutdown"); }, }); return this; } addCleanupTask(...tasks) { tasks.forEach((task) => this.cleanupTasks.add(task)); } async cleanup() { if (this.cleanupTasks.size === 0) return; await Promise.all(Array.from(this.cleanupTasks).map((task) => task())); this.cleanupTasks.clear(); this.logger.info("✅ All resources closed").end(); } setupProcessHandlers() { process.on("uncaughtException", (err) => this.handleSignal("⚠️ Uncaught Exception:", err)); process.on("unhandledRejection", (err) => this.handleSignal("⚠️ Unhandled Rejection:", err)); } async handleSignal(msg, err) { if (this.isShuttingDown) return; this.isShuttingDown = true; if (err) this.logger.error(msg, err); else this.logger.info(msg); await this.cleanup(); } } exports.GracefulExit = GracefulExit; exports.default = GracefulExit; //# sourceMappingURL=graceful-exit.js.map