UNPKG

summarizely-cli

Version:

YouTube summarizer that respects your existing subscriptions. No API keys required.

109 lines 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.commonHandlers = exports.ShutdownManager = void 0; exports.getShutdownManager = getShutdownManager; exports.registerShutdownHandler = registerShutdownHandler; class ShutdownManager { constructor() { this.handlers = []; this.isShuttingDown = false; this.shutdownPromise = null; this.setupSignalHandlers(); } setupSignalHandlers() { const signals = ['SIGINT', 'SIGTERM', 'SIGHUP']; signals.forEach(signal => { process.on(signal, async () => { await this.shutdown(signal); }); }); // Handle uncaught exceptions and rejections process.on('uncaughtException', async (error) => { console.error('Uncaught exception:', error); await this.shutdown('uncaughtException'); process.exit(1); }); process.on('unhandledRejection', async (reason, promise) => { console.error('Unhandled rejection at:', promise, 'reason:', reason); await this.shutdown('unhandledRejection'); process.exit(1); }); } register(handler) { this.handlers.push(handler); } async shutdown(signal) { if (this.isShuttingDown) { return this.shutdownPromise; } this.isShuttingDown = true; console.error(`\nReceived ${signal}, shutting down gracefully...`); this.shutdownPromise = this.executeHandlers(); await this.shutdownPromise; console.error('Shutdown complete'); process.exit(0); } async executeHandlers() { const results = await Promise.allSettled(this.handlers.map(async (handler, index) => { try { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000)); await Promise.race([handler(), timeout]); } catch (error) { console.error(`Shutdown handler ${index} failed:`, error); } })); // Log any failures results.forEach((result, index) => { if (result.status === 'rejected') { console.error(`Shutdown handler ${index} failed:`, result.reason); } }); } async forceShutdown(exitCode = 1) { console.error('Forcing shutdown...'); process.exit(exitCode); } } exports.ShutdownManager = ShutdownManager; // Singleton instance let shutdownInstance = null; function getShutdownManager() { if (!shutdownInstance) { shutdownInstance = new ShutdownManager(); } return shutdownInstance; } function registerShutdownHandler(handler) { getShutdownManager().register(handler); } // Common shutdown handlers exports.commonHandlers = { flushCache: (cache) => { return async () => { console.error('Flushing cache...'); await cache.cleanup(); }; }, saveMetrics: (metrics) => { return async () => { console.error('Saving metrics...'); // Implementation would go here }; }, closeDatabaseConnections: (connections) => { return async () => { console.error('Closing database connections...'); await Promise.all(connections.map(conn => conn.close())); }; }, waitForPendingRequests: (requests) => { return async () => { if (requests.size > 0) { console.error(`Waiting for ${requests.size} pending requests...`); await Promise.allSettled(requests); } }; } }; //# sourceMappingURL=shutdown.js.map