UNPKG

safe-commander-mcp

Version:

A secure MCP server for executing whitelisted development commands with comprehensive security controls and resource limits

40 lines (39 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupGracefulShutdown = setupGracefulShutdown; const rate_limiter_1 = require("../security/rate-limiter"); const logger_1 = require("./logger"); /** * Graceful shutdown with proper cleanup * Sets up signal handlers for SIGINT and SIGTERM */ function setupGracefulShutdown() { const shutdown = (signal) => { (0, logger_1.log)('info', `Received ${signal}, initiating graceful shutdown...`); // Set a timeout for forced shutdown const shutdownTimeout = setTimeout(() => { (0, logger_1.log)('warn', 'Shutdown timeout reached, forcing exit'); process.exit(1); }, 5000); const runningCommands = (0, rate_limiter_1.getRunningCommands)(); if (runningCommands.size === 0) { clearTimeout(shutdownTimeout); (0, logger_1.log)('info', 'Shutdown complete - no running commands'); process.exit(0); } else { (0, logger_1.log)('info', `Waiting for ${runningCommands.size} running commands to complete...`); const checkInterval = setInterval(() => { const currentRunningCommands = (0, rate_limiter_1.getRunningCommands)(); if (currentRunningCommands.size === 0) { clearInterval(checkInterval); clearTimeout(shutdownTimeout); (0, logger_1.log)('info', 'Shutdown complete - all commands finished'); process.exit(0); } }, 100); } }; process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGTERM', () => shutdown('SIGTERM')); }