add pause resume controls to ui
This commit is contained in:
1 parent
3c7cb45bec
commit
d81e00ddbc
13 files changed
+367
-12
No files matched your search
@@ -47,6 +47,7 @@ from gui.web_gui_pkg.net_config import get_web_gui_host, get_web_gui_port
|
||||
from gui.web_gui_pkg.routes import handle_get, handle_post
|
||||
from gui.web_gui_pkg import proc_runner_ops as _proc_ops
|
||||
from gui.web_gui_pkg import proc_runner_stats as _proc_stats
|
||||
from gui.process_pause import resume_process_tree, suspend_process_tree
|
||||
|
||||
BASE_DIR = path_config.PROJECT_ROOT
|
||||
CONFIG_PATH = path_config.get_config_path()
|
||||
@@ -92,6 +93,7 @@ class ProcRunner:
|
||||
self._last_log_ts: Optional[float] = None
|
||||
self._exit_code: Optional[int] = None
|
||||
self._pid: Optional[int] = None
|
||||
self._paused: bool = False
|
||||
self._owned_run_cli_pids: set[int] = set()
|
||||
|
||||
# 运行统计(基于 stdout 日志解析)
|
||||
@@ -215,6 +217,10 @@ class ProcRunner:
|
||||
with self._lock:
|
||||
return self._proc is not None and self._proc.poll() is None
|
||||
|
||||
def is_paused(self) -> bool:
|
||||
with self._lock:
|
||||
return bool(self._paused and self._proc is not None and self._proc.poll() is None)
|
||||
|
||||
def _list_project_run_cli_processes(self) -> list[tuple[int, str]]:
|
||||
"""列出当前仓库下运行的 run_cli.py 进程(含 pid 与命令行)。"""
|
||||
entries: list[tuple[int, str]] = []
|
||||
@@ -363,6 +369,11 @@ class ProcRunner:
|
||||
pid = self._pid
|
||||
p = self._proc
|
||||
self._append_log(f"[停止] 强制停止请求 pid={pid or '-'}")
|
||||
if self.is_paused() and pid:
|
||||
try:
|
||||
resume_process_tree(int(pid))
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if p is not None and p.poll() is None:
|
||||
p.kill()
|
||||
@@ -379,6 +390,7 @@ class ProcRunner:
|
||||
with self._lock:
|
||||
self._proc = None
|
||||
self._pid = None
|
||||
self._paused = False
|
||||
if pid:
|
||||
self._owned_run_cli_pids.discard(int(pid))
|
||||
self._exit_code = self._exit_code if self._exit_code is not None else -9
|
||||
@@ -395,6 +407,8 @@ class ProcRunner:
|
||||
p = self._proc
|
||||
if p is None or p.poll() is not None:
|
||||
return
|
||||
if self.is_paused():
|
||||
self.resume()
|
||||
self._append_log("[网页] 停止:终止进程")
|
||||
try:
|
||||
p.terminate()
|
||||
@@ -409,12 +423,59 @@ class ProcRunner:
|
||||
try:
|
||||
self._proc = None
|
||||
self._pid = None
|
||||
self._paused = False
|
||||
if getattr(p, "pid", None):
|
||||
self._owned_run_cli_pids.discard(int(p.pid))
|
||||
except Exception:
|
||||
pass
|
||||
self._append_log("[网页] 停止:已尝试终止并清理进程引用")
|
||||
|
||||
def pause(self) -> bool:
|
||||
with self._lock:
|
||||
p = self._proc
|
||||
pid = self._pid or (p.pid if p is not None else None)
|
||||
already_paused = self._paused
|
||||
if p is None or p.poll() is not None or not pid:
|
||||
self._append_log("[网页] 暂停:当前没有正在运行的进程")
|
||||
with self._lock:
|
||||
self._paused = False
|
||||
return False
|
||||
if already_paused:
|
||||
self._append_log("[网页] 暂停:当前已经是暂停状态")
|
||||
return True
|
||||
try:
|
||||
suspend_process_tree(int(pid))
|
||||
with self._lock:
|
||||
self._paused = True
|
||||
self._append_log("[网页] 暂停:已暂停运行进程")
|
||||
return True
|
||||
except Exception as e:
|
||||
self._append_log(f"[网页] 暂停失败:{e}")
|
||||
return False
|
||||
|
||||
def resume(self) -> bool:
|
||||
with self._lock:
|
||||
p = self._proc
|
||||
pid = self._pid or (p.pid if p is not None else None)
|
||||
already_paused = self._paused
|
||||
if p is None or p.poll() is not None or not pid:
|
||||
self._append_log("[网页] 继续:当前没有可恢复的运行进程")
|
||||
with self._lock:
|
||||
self._paused = False
|
||||
return False
|
||||
if not already_paused:
|
||||
self._append_log("[网页] 继续:当前不在暂停状态")
|
||||
return True
|
||||
try:
|
||||
resume_process_tree(int(pid))
|
||||
with self._lock:
|
||||
self._paused = False
|
||||
self._append_log("[网页] 继续:已恢复运行进程")
|
||||
return True
|
||||
except Exception as e:
|
||||
self._append_log(f"[网页] 继续失败:{e}")
|
||||
return False
|
||||
|
||||
def _append_log(self, line: str) -> None:
|
||||
# 清理ANSI转义序列,避免在Web界面显示异常字符
|
||||
line = strip_ansi_escape_codes(line)
|
||||
@@ -739,6 +800,7 @@ class ProcRunner:
|
||||
with self._lock:
|
||||
self._proc = p
|
||||
self._pid = p.pid
|
||||
self._paused = False
|
||||
self._owned_run_cli_pids.add(int(p.pid))
|
||||
self._start_ts = time.time()
|
||||
self._exit_code = None
|
||||
@@ -772,6 +834,7 @@ class ProcRunner:
|
||||
self._append_log(f"[网页] 进程退出(无输出):退出码={rc}")
|
||||
with self._lock:
|
||||
self._exit_code = rc
|
||||
self._paused = False
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -810,6 +873,7 @@ class ProcRunner:
|
||||
try:
|
||||
self._proc = None
|
||||
self._pid = None
|
||||
self._paused = False
|
||||
self._owned_run_cli_pids.discard(int(p.pid))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in new issue
Block a user