UNPKG

dynamic-interaction

Version:

Dynamic interaction 动态交互mcp,用于cursor、windsurf、trae 等 AI 智能编辑器 Agent 运行时交互使用

139 lines (138 loc) 5.4 kB
"use strict"; /** * 服务器生命周期管理 * 负责管理服务器的启动、停止和状态 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.lifecycleManager = exports.LifecycleManager = void 0; const logger_1 = require("../../logger"); const server_1 = require("./server"); const connection_1 = require("../websocket/connection"); const manager_1 = require("../session/manager"); const queue_1 = require("../session/queue"); const types_1 = require("../utils/types"); const errors_1 = require("../utils/errors"); class LifecycleManager { static instance; _state = types_1.ServerState.STOPPED; _shutdownTimer = null; _shutdownDelayMs = 5000; _startTime = 0; constructor() { } static getInstance() { if (!LifecycleManager.instance) { LifecycleManager.instance = new LifecycleManager(); } return LifecycleManager.instance; } get state() { return this._state; } get uptime() { return this._startTime > 0 ? Date.now() - this._startTime : 0; } async startServer() { if (this._state === types_1.ServerState.RUNNING || this._state === types_1.ServerState.STARTING) { logger_1.logger.info('服务器已经在运行或正在启动中'); return; } this._state = types_1.ServerState.STARTING; logger_1.logger.info('正在启动服务器...'); // 取消任何可能存在的关闭定时器 this.cancelShutdownTimer(); try { // 启动HTTP服务器 await server_1.httpServer.start(); // 初始化WebSocket服务器 connection_1.webSocketManager.initialize(server_1.httpServer.getServer()); this._state = types_1.ServerState.RUNNING; this._startTime = Date.now(); logger_1.logger.info('服务器启动完成'); } catch (error) { this._state = types_1.ServerState.STOPPED; logger_1.logger.error('服务器启动失败:', error); throw new errors_1.ServerError('服务器启动失败', errors_1.ErrorCodes.SERVER_START_ERROR); } } async stopServer(immediate = false) { if (this._state === types_1.ServerState.STOPPED || this._state === types_1.ServerState.STOPPING) { logger_1.logger.info('服务器已经停止或正在停止中'); return; } // 检查是否还有活跃的连接或会话 if (!immediate && this.hasActiveActivity()) { const stats = this.getServerStats(); logger_1.logger.info(`仍有活跃连接 (${stats.activeConnections}) 或会话 (${stats.activeSessions}),暂不关闭服务器`); return; } // 延迟关闭逻辑 if (!immediate && this._shutdownDelayMs > 0) { this.scheduleShutdown(); return; } // 立即关闭 await this.performShutdown(); } scheduleShutdown() { logger_1.logger.info(`计划在 ${this._shutdownDelayMs}ms 后关闭服务器`); this.cancelShutdownTimer(); this._state = types_1.ServerState.STOPPING; this._shutdownTimer = setTimeout(() => { this.performShutdown().catch(error => { logger_1.logger.error('定时关闭服务器失败:', error); }); }, this._shutdownDelayMs); } async performShutdown() { logger_1.logger.info('正在关闭服务器...'); this._state = types_1.ServerState.STOPPING; try { // 关闭WebSocket连接 connection_1.webSocketManager.closeAll(); // 清理会话 manager_1.sessionManager.clearAll(); // 关闭HTTP服务器 await server_1.httpServer.stop(); this._state = types_1.ServerState.STOPPED; this._startTime = 0; logger_1.logger.info('服务器已成功关闭'); } catch (error) { logger_1.logger.error('关闭服务器时出错:', error); throw new errors_1.ServerError('服务器关闭失败', errors_1.ErrorCodes.SERVER_STOP_ERROR); } } cancelShutdownTimer() { if (this._shutdownTimer) { clearTimeout(this._shutdownTimer); this._shutdownTimer = null; logger_1.logger.info('取消了计划中的服务器关闭'); } } hasActiveActivity() { const stats = this.getServerStats(); return stats.activeConnections > 0 || stats.activeSessions > 0 || stats.queuedRequests > 0; } checkAndStopIfIdle() { if (this._state === types_1.ServerState.RUNNING && !this.hasActiveActivity()) { logger_1.logger.info('没有活跃的连接和会话,准备关闭服务器'); this.stopServer(); } } getServerStats() { return { activeConnections: connection_1.webSocketManager.getConnectionCount(), activeSessions: manager_1.sessionManager.getSessionCount(), queuedRequests: queue_1.sessionQueue.getQueueSize(), uptime: this.uptime }; } setShutdownDelay(delayMs) { this._shutdownDelayMs = delayMs; } } exports.LifecycleManager = LifecycleManager; exports.lifecycleManager = LifecycleManager.getInstance();