Keep dependency installs responsive

This commit is contained in:
王鹏
2026-10-09 19:31:21 +08:00
parent 6c79ce0b26
commit 66dd1da2d1
4 changed files with 140 additions and 65 deletions

View File

@@ -107,7 +107,7 @@ def build_deployment_plan(scan: ScanResult, config: DeploymentConfig) -> list[De
steps.append( steps.append(
DeploymentStep( DeploymentStep(
f"安装{display_name}依赖", f"安装{display_name}依赖",
[npm_command, "install"], [npm_command, "install", "--no-audit", "--no-fund", "--progress=false"],
frontend.path, frontend.path,
f"安装{display_name} package.json 中的依赖", f"安装{display_name} package.json 中的依赖",
kind="frontend_install", kind="frontend_install",
@@ -778,6 +778,8 @@ def run_steps(
on_output(f"\n[{step.label}]{' ' + command_text if command_text else ''}") on_output(f"\n[{step.label}]{' ' + command_text if command_text else ''}")
if step.input_path: if step.input_path:
on_output(f"stdin: {step.input_path}") on_output(f"stdin: {step.input_path}")
if step.kind in {"backend_build", "frontend_install"}:
on_output("依赖下载和构建可能需要几分钟;窗口可继续操作,请留意后续日志。")
if _is_skip_step(step): if _is_skip_step(step):
on_output(step.description) on_output(step.description)
continue continue

View File

@@ -40,6 +40,9 @@ TEXT = {
} }
SIDEBAR_ITEMS = ("项目识别", "环境检测", "工具路径", "数据库", "部署任务", "运行日志") SIDEBAR_ITEMS = ("项目识别", "环境检测", "工具路径", "数据库", "部署任务", "运行日志")
UI_QUEUE_BATCH_SIZE = 500
MAX_LOG_LINES = 5000
TRIM_LOG_LINES_TO = 4000
def database_name_from_sql_files(sql_files: list[Path]) -> str: def database_name_from_sql_files(sql_files: list[Path]) -> str:
@@ -647,23 +650,52 @@ class DeployWorkbench(tk.Tk):
self.result_text.insert(tk.END, value) self.result_text.insert(tk.END, value)
def _append_log(self, value: str) -> None: def _append_log(self, value: str) -> None:
self._append_logs([value])
def _append_logs(self, values: list[str]) -> None:
values = [value for value in values if value]
if not values:
return
self.log_text.configure(state=tk.NORMAL) self.log_text.configure(state=tk.NORMAL)
self.log_text.insert(tk.END, value + "\n") self.log_text.insert(tk.END, "\n".join(values) + "\n")
try:
line_count = int(self.log_text.index("end-1c").split(".", 1)[0])
if line_count > MAX_LOG_LINES:
delete_to = line_count - TRIM_LOG_LINES_TO + 1
self.log_text.delete("1.0", f"{delete_to}.0")
except (AttributeError, tk.TclError, ValueError):
pass
self.log_text.see(tk.END) self.log_text.see(tk.END)
def _thread_log(self, value: str) -> None: def _thread_log(self, value: str) -> None:
self.ui_queue.put(("log", value)) self.ui_queue.put(("log", value))
def _drain_queue(self) -> None: def _drain_queue(self) -> None:
while True: events: list[tuple[str, object]] = []
for _ in range(UI_QUEUE_BATCH_SIZE):
try: try:
event, payload = self.ui_queue.get_nowait() events.append(self.ui_queue.get_nowait())
except queue.Empty: except queue.Empty:
break break
pending_logs: list[str] = []
for event, payload in events:
if event == "log":
pending_logs.append(str(payload))
continue
if pending_logs:
self._append_logs(pending_logs)
pending_logs.clear()
self._handle_ui_event(event, payload)
if pending_logs:
self._append_logs(pending_logs)
delay = 20 if len(events) == UI_QUEUE_BATCH_SIZE else 120
self.after(delay, self._drain_queue)
def _handle_ui_event(self, event: str, payload: object) -> None:
if event == "env": if event == "env":
self._render_environment(payload) # type: ignore[arg-type] self._render_environment(payload) # type: ignore[arg-type]
elif event == "log":
self._append_log(str(payload))
elif event == "deploy_done": elif event == "deploy_done":
self._set_deployment_running(False) self._set_deployment_running(False)
self._append_log(f"部署任务结束,退出码: {payload}") self._append_log(f"部署任务结束,退出码: {payload}")
@@ -677,7 +709,7 @@ class DeployWorkbench(tk.Tk):
if error: if error:
self._append_log(f"Maven 安装失败: {error}") self._append_log(f"Maven 安装失败: {error}")
messagebox.showerror("Maven 安装失败", str(error)) messagebox.showerror("Maven 安装失败", str(error))
continue return
self.tool_path_vars["Maven"].set(str(executable)) self.tool_path_vars["Maven"].set(str(executable))
self.save_tool_path_settings(silent=True) self.save_tool_path_settings(silent=True)
self._append_log(f"Maven 路径已保存: {executable}") self._append_log(f"Maven 路径已保存: {executable}")
@@ -688,7 +720,7 @@ class DeployWorkbench(tk.Tk):
if error: if error:
self._append_log(f"Tomcat 安装失败: {error}") self._append_log(f"Tomcat 安装失败: {error}")
messagebox.showerror("Tomcat 安装失败", str(error)) messagebox.showerror("Tomcat 安装失败", str(error))
continue return
self.tool_path_vars["Tomcat"].set(str(executable)) self.tool_path_vars["Tomcat"].set(str(executable))
self.save_tool_path_settings(silent=True) self.save_tool_path_settings(silent=True)
self._append_log(f"Tomcat 路径已保存: {executable}") self._append_log(f"Tomcat 路径已保存: {executable}")
@@ -699,7 +731,7 @@ class DeployWorkbench(tk.Tk):
if error: if error:
self._append_log(f"MySQL 安装或配置失败: {error}") self._append_log(f"MySQL 安装或配置失败: {error}")
messagebox.showerror("MySQL 安装或配置失败", str(error)) messagebox.showerror("MySQL 安装或配置失败", str(error))
continue return
self.tool_path_vars["MySQL"].set(str(executable)) self.tool_path_vars["MySQL"].set(str(executable))
self.save_tool_path_settings(silent=True) self.save_tool_path_settings(silent=True)
self.mysql_password.set(MYSQL_ROOT_PASSWORD) self.mysql_password.set(MYSQL_ROOT_PASSWORD)
@@ -711,7 +743,7 @@ class DeployWorkbench(tk.Tk):
if error: if error:
self._append_log(f"Node.js 安装失败: {error}") self._append_log(f"Node.js 安装失败: {error}")
messagebox.showerror("Node.js 安装失败", str(error)) messagebox.showerror("Node.js 安装失败", str(error))
continue return
self.tool_path_vars["Node.js"].set(str(node)) self.tool_path_vars["Node.js"].set(str(node))
self.tool_path_vars["npm"].set(str(npm)) self.tool_path_vars["npm"].set(str(npm))
self.save_tool_path_settings(silent=True) self.save_tool_path_settings(silent=True)
@@ -719,7 +751,6 @@ class DeployWorkbench(tk.Tk):
self._append_log(f"npm 路径已保存: {npm}") self._append_log(f"npm 路径已保存: {npm}")
self.check_environment_async() self.check_environment_async()
self.after(1800, lambda: self._prompt_add_tool_after_install("Node.js")) self.after(1800, lambda: self._prompt_add_tool_after_install("Node.js"))
self.after(120, self._drain_queue)
def _prompt_add_tool_after_install(self, tool_name: str) -> None: def _prompt_add_tool_after_install(self, tool_name: str) -> None:
result = self._find_environment_result(tool_name) result = self._find_environment_result(tool_name)

View File

@@ -962,7 +962,10 @@ class DeployerTests(unittest.TestCase):
[step.kind for step in plan], [step.kind for step in plan],
["frontend_install", "frontend_start", "deployment_summary"], ["frontend_install", "frontend_start", "deployment_summary"],
) )
self.assertEqual(plan[0].command, [npm, "install"]) self.assertEqual(
plan[0].command,
[npm, "install", "--no-audit", "--no-fund", "--progress=false"],
)
self.assertEqual(plan[1].command[1:3], ["run", "serve"]) self.assertEqual(plan[1].command[1:3], ["run", "serve"])
def test_resolves_windows_cmd_shims_before_starting_process(self): def test_resolves_windows_cmd_shims_before_starting_process(self):

View File

@@ -18,6 +18,29 @@ class FakeWidget:
self.seen = index self.seen = index
class FakeLogWidget:
def __init__(self):
self.value = ""
self.insert_calls = 0
def configure(self, **options):
pass
def insert(self, index, value):
self.insert_calls += 1
self.value += value
def index(self, index):
return f"{self.value.count(chr(10)) + 1}.0"
def delete(self, start, end):
delete_lines = max(int(end.split('.', 1)[0]) - 1, 0)
self.value = "\n".join(self.value.split("\n")[delete_lines:])
def see(self, index):
pass
class FakeButton: class FakeButton:
def __init__(self): def __init__(self):
self.state = None self.state = None
@@ -35,6 +58,22 @@ class FakeVariable:
class GuiNavigationTests(unittest.TestCase): class GuiNavigationTests(unittest.TestCase):
def test_log_queue_is_rendered_in_batches(self):
app = DeployWorkbench.__new__(DeployWorkbench)
app.ui_queue = queue.Queue()
app.log_text = FakeLogWidget()
scheduled = []
app.after = lambda delay, callback: scheduled.append(delay)
for index in range(100):
app.ui_queue.put(("log", f"line {index}"))
DeployWorkbench._drain_queue(app)
self.assertEqual(app.log_text.insert_calls, 1)
self.assertIn("line 0", app.log_text.value)
self.assertIn("line 99", app.log_text.value)
self.assertEqual(scheduled, [120])
def test_database_name_comes_from_first_sql_filename(self): def test_database_name_comes_from_first_sql_filename(self):
from pathlib import Path from pathlib import Path