From 1bb2b56e1c049e9d24caea626e073c270acb2a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=9C=B1?= <10714957+xiao-zhu245@user.noreply.gitee.com> Date: Fri, 11 Jul 2025 21:46:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=87=8D=E8=AF=95=E6=AC=A1?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/routes/instances.ts | 59 +++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/server/src/routes/instances.ts b/server/src/routes/instances.ts index a6dff08..78c61b6 100644 --- a/server/src/routes/instances.ts +++ b/server/src/routes/instances.ts @@ -490,6 +490,11 @@ const PYTHON_SCRIPT_PATH = path.join(__dirname, '..', 'Python', 'game_config_man // Python依赖是否已安装的标志 let pythonDepsInstalled = false +// Python环境状态管理 +let pythonEnvironmentFailed = false +let pythonFailureCount = 0 +const MAX_PYTHON_RETRY_COUNT = 3 + // 获取正确的Python命令 function getPythonCommand(): string { const platform = os.platform() @@ -510,6 +515,13 @@ function getPipCommand(): string { } } +// 重置Python环境状态 +function resetPythonEnvironmentStatus(): void { + pythonEnvironmentFailed = false + pythonFailureCount = 0 + logger.info('Python环境状态已重置') +} + function installPythonDependencies(): Promise { return new Promise((resolve, reject) => { if (pythonDepsInstalled) { @@ -596,6 +608,13 @@ function installPythonDependencies(): Promise { function callPythonScript(method: string, args: any[] = []): Promise { return new Promise(async (resolve, reject) => { try { + // 检查Python环境是否已经失败过多次 + if (pythonEnvironmentFailed && pythonFailureCount >= MAX_PYTHON_RETRY_COUNT) { + logger.error(`Python环境已失败${pythonFailureCount}次,停止尝试启动`) + reject(new Error('Python环境不可用,已停止重试')) + return + } + // 确保Python依赖已安装 await installPythonDependencies() @@ -628,20 +647,28 @@ function callPythonScript(method: string, args: any[] = []): Promise { if (code === 0) { try { const result = JSON.parse(stdout) + // 成功时重置失败状态 + pythonEnvironmentFailed = false + pythonFailureCount = 0 resolve(result) } catch (error) { logger.error(`JSON解析失败: ${error}, stdout: ${stdout}`) reject(new Error(`JSON解析失败: ${error}`)) } } else { - logger.error(`Python脚本执行失败,退出码: ${code}, stderr: ${stderr}, stdout: ${stdout}`) + pythonFailureCount++ + pythonEnvironmentFailed = true + logger.error(`Python脚本执行失败 (第${pythonFailureCount}次),退出码: ${code}, stderr: ${stderr}, stdout: ${stdout}`) reject(new Error(`Python脚本执行失败: ${stderr}`)) } }) pythonProcess.on('error', (error) => { - reject(new Error(`启动Python进程失败: ${error.message}`)) - }) + pythonFailureCount++ + pythonEnvironmentFailed = true + logger.error(`Python进程启动失败 (第${pythonFailureCount}次): ${error.message}`) + reject(new Error(`启动Python进程失败: ${error.message}`)) + }) } catch (error) { reject(error) } @@ -823,6 +850,24 @@ router.post('/:instanceId/configs/:configId', authenticateToken, async (req: Req } }) +// 重置Python环境状态 +router.post('/python/reset', authenticateToken, async (req: Request, res: Response) => { + try { + resetPythonEnvironmentStatus() + res.json({ + success: true, + message: 'Python环境状态已重置' + }) + } catch (error: any) { + logger.error('重置Python环境状态失败:', error) + res.status(500).json({ + success: false, + error: '重置Python环境状态失败', + message: error.message + }) + } +}) + // Python环境检测 router.get('/python/check', authenticateToken, async (req: Request, res: Response) => { try { @@ -842,7 +887,9 @@ router.get('/python/check', authenticateToken, async (req: Request, res: Respons available: true, version: version, command: pythonCommand, - platform: platform + platform: platform, + failureCount: pythonFailureCount, + environmentFailed: pythonEnvironmentFailed } }) } catch (error: any) { @@ -853,7 +900,9 @@ router.get('/python/check', authenticateToken, async (req: Request, res: Respons data: { available: false, error: `未检测到Python环境: ${error.message}`, - platform: os.platform() + platform: os.platform(), + failureCount: pythonFailureCount, + environmentFailed: pythonEnvironmentFailed } }) }