diff --git a/deploy_tool/deployer.py b/deploy_tool/deployer.py index 67d0b64..843368b 100644 --- a/deploy_tool/deployer.py +++ b/deploy_tool/deployer.py @@ -3,12 +3,14 @@ from __future__ import annotations import json import codecs import os +import queue import re import socket import shutil import subprocess import sys import tempfile +import threading import time from dataclasses import dataclass from datetime import datetime @@ -52,6 +54,7 @@ class DeploymentStep: OutputHandler = Callable[[str], None] ProcessRunner = Callable[[list[str], Path, OutputHandler, Path | None, bool], int] +PROCESS_HEARTBEAT_SECONDS = 30.0 def build_deployment_plan(scan: ScanResult, config: DeploymentConfig) -> list[DeploymentStep]: @@ -917,14 +920,68 @@ def _run_process( creationflags=_hidden_process_creation_flags(), ) assert process.stdout is not None - for raw_line in process.stdout: - on_output(_decode_process_output(raw_line).rstrip()) - return process.wait() + return _stream_process_output(process, command, on_output) finally: if input_file: input_file.close() +def _stream_process_output( + process, + command: list[str], + on_output: OutputHandler, + heartbeat_seconds: float = PROCESS_HEARTBEAT_SECONDS, +) -> int: + assert process.stdout is not None + output_queue: queue.Queue[bytes | str | object] = queue.Queue() + finished = object() + + def read_output() -> None: + try: + for raw_line in process.stdout: + output_queue.put(raw_line) + finally: + output_queue.put(finished) + + threading.Thread(target=read_output, daemon=True).start() + started_at = time.monotonic() + last_output_at = started_at + last_heartbeat_at = started_at + executable_name = Path(command[0]).name.lower() if command else "" + + while True: + try: + item = output_queue.get(timeout=min(1.0, heartbeat_seconds)) + except queue.Empty: + now = time.monotonic() + if now - last_output_at >= heartbeat_seconds and now - last_heartbeat_at >= heartbeat_seconds: + elapsed = max(1, int(now - started_at)) + silent = max(1, int(now - last_output_at)) + on_output(_process_heartbeat_message(executable_name, elapsed, silent)) + last_heartbeat_at = now + poll = getattr(process, "poll", None) + if callable(poll) and poll() is not None: + break + continue + if item is finished: + break + decoded = _decode_process_output(item).rstrip() + if decoded: + on_output(decoded) + last_output_at = time.monotonic() + + return process.wait() + + +def _process_heartbeat_message(executable_name: str, elapsed: int, silent: int) -> str: + status = f"命令仍在执行:已运行 {elapsed} 秒,{silent} 秒没有新输出。" + if executable_name in {"npm", "npm.cmd", "npm.exe"}: + return status + " npm 可能正在解析依赖或等待软件源;若持续数分钟,请检查网络、代理和证书。" + if executable_name in {"mvn", "mvn.cmd", "mvn.bat"}: + return status + " Maven 可能正在下载依赖;若持续数分钟,请检查 Maven 仓库和网络。" + return status + + def _build_process_environment( command: list[str], overrides: Mapping[str, str] | None = None ) -> dict[str, str]: diff --git a/tests/test_deployer.py b/tests/test_deployer.py index 3c3c377..043b445 100644 --- a/tests/test_deployer.py +++ b/tests/test_deployer.py @@ -1,6 +1,7 @@ import unittest import os import subprocess +import time from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import patch @@ -11,6 +12,33 @@ from deploy_tool.scanner import ProjectComponent, ScanResult class DeployerTests(unittest.TestCase): + def test_silent_npm_process_reports_heartbeat(self): + output = [] + + class SlowOutput: + def __iter__(self): + time.sleep(0.03) + return iter(()) + + class FakeProcess: + stdout = SlowOutput() + + def poll(self): + return None + + def wait(self): + return 0 + + exit_code = deployer_module._stream_process_output( + FakeProcess(), + ["npm.cmd", "install"], + output.append, + heartbeat_seconds=0.01, + ) + + self.assertEqual(exit_code, 0) + self.assertTrue(any("npm 可能正在解析依赖" in line for line in output)) + def test_process_output_accepts_utf8_and_gbk_without_decode_failure(self): output = [] popen_options = {}