ai-debug-local-mcp
Version:
๐ฏ ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh
81 lines โข 2.98 kB
JavaScript
/**
* Server cleanup utilities
*
* Extracted from server.ts to reduce file size and improve maintainability
*/
export class ServerCleanup {
/**
* Clean up stale sessions older than maxAge
*/
static cleanupStaleSessions(sessions, maxAge = 2 * 60 * 60 * 1000) {
const now = Date.now();
let cleanedCount = 0;
for (const [sessionId, session] of sessions.entries()) {
if (session.startTime && (now - session.startTime.getTime()) > maxAge) {
this.cleanupSession(sessionId, sessions).catch(error => console.error(`Failed to cleanup stale session ${sessionId}:`, error));
cleanedCount++;
}
}
if (cleanedCount > 0) {
console.error(`๐งน Cleaned up ${cleanedCount} stale sessions`);
}
}
/**
* Clean up a single session
*/
static async cleanupSession(sessionId, sessions) {
const session = sessions.get(sessionId);
if (!session)
return;
try {
// Close browser resources
if (session.page) {
await session.page.close().catch(() => { });
}
if (session.browser) {
await session.browser.close().catch(() => { });
}
// Remove from sessions
sessions.delete(sessionId);
}
catch (error) {
console.error(`Error cleaning up session ${sessionId}:`, error);
// Force remove even if cleanup failed
sessions.delete(sessionId);
}
}
/**
* Perform emergency cleanup of all sessions
*/
static async emergencyCleanup(sessions) {
try {
const cleanupPromises = Array.from(sessions.keys()).map(sessionId => this.cleanupSession(sessionId, sessions));
await Promise.all(cleanupPromises);
}
catch (error) {
console.error('โ Emergency cleanup failed:', error);
}
}
/**
* Setup graceful shutdown handlers
*/
static setupShutdownHandlers(server) {
const handleShutdown = (signal) => {
console.error(`\n๐ Received ${signal}. Shutting down gracefully...`);
server.gracefulShutdown().catch(console.error);
};
process.on('SIGINT', () => handleShutdown('SIGINT'));
process.on('SIGTERM', () => handleShutdown('SIGTERM'));
process.on('SIGQUIT', () => handleShutdown('SIGQUIT'));
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('โ Uncaught exception:', error);
server.emergencyCleanup().catch(console.error);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('โ Unhandled rejection at:', promise, 'reason:', reason);
server.emergencyCleanup().catch(console.error);
});
}
}
//# sourceMappingURL=server-cleanup.js.map