add pause resume controls to ui

This commit is contained in:
Your Name committed 2026-06-23 02:38:08 +08:00
1 parent 3c7cb45bec
commit d81e00ddbc
13 files changed
+367 -12

No files matched your search

+66
View File
@@ -21,6 +21,7 @@ from dataclasses import dataclass
from typing import Optional
from douyin.core import path_config
from gui.process_pause import resume_process_tree, suspend_process_tree
# 从子包 re-export 顶层符号(保持兼容)
from gui.gui_core_pkg import (
@@ -55,6 +56,7 @@ class Runner:
self._last_log_ts: Optional[float] = None
self._exit_code: Optional[int] = None
self._pid: Optional[int] = None
self._paused: bool = False
self._counters: dict[str, int] = {}
self._fudai_last: dict[str, object] = {
"content": "",
@@ -88,6 +90,10 @@ class Runner:
def is_running(self) -> bool:
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 _append_log_line(self, line: str) -> None:
if line is None:
return
@@ -163,6 +169,7 @@ class Runner:
start_ts = self._start_ts
last_log_ts = self._last_log_ts
exit_code = self._exit_code
paused = bool(self._paused and self._proc is not None and self._proc.poll() is None)
counters = dict(self._counters)
fudai_last = dict(self._fudai_last)
wins = list(self._wins[-20:]) # 只返回最近20条
@@ -175,6 +182,7 @@ class Runner:
return {
"running": bool(running),
"paused": paused,
"pid": pid,
"start_ts": start_ts,
"last_log_ts": last_log_ts,
@@ -383,6 +391,7 @@ class Runner:
with self._lock:
self._proc = p
self._pid = p.pid
self._paused = False
self._start_ts = time.time()
self._exit_code = None
self._last_log_ts = time.time()
@@ -420,6 +429,7 @@ class Runner:
rc = self._proc.wait()
with self._lock:
self._exit_code = rc
self._paused = False
# 如果进程立即退出且没有输出,说明可能有问题
if not has_output and rc != 0:
@@ -437,6 +447,7 @@ class Runner:
if rc is not None:
with self._lock:
self._exit_code = rc
self._paused = False
self._emit_line(f"[进程结束] exit_code={rc}")
except Exception:
pass
@@ -496,10 +507,58 @@ class Runner:
self._emit_line(f"[错误] 发送返回键异常: {str(e)}")
return False
def pause(self) -> bool:
with self._lock:
proc = self._proc
pid = self._pid or (proc.pid if proc is not None else None)
already_paused = self._paused
if proc is None or proc.poll() is not None or not pid:
self._emit_line("[暂停] 当前没有正在运行的进程")
with self._lock:
self._paused = False
return False
if already_paused:
self._emit_line("[暂停] 当前已经是暂停状态")
return True
try:
suspend_process_tree(int(pid))
with self._lock:
self._paused = True
self._emit_line("[暂停] 已暂停运行进程")
return True
except Exception as e:
self._emit_line(f"[暂停] 暂停失败: {e}")
return False
def resume(self) -> bool:
with self._lock:
proc = self._proc
pid = self._pid or (proc.pid if proc is not None else None)
already_paused = self._paused
if proc is None or proc.poll() is not None or not pid:
self._emit_line("[继续] 当前没有可恢复的运行进程")
with self._lock:
self._paused = False
return False
if not already_paused:
self._emit_line("[继续] 当前不在暂停状态")
return True
try:
resume_process_tree(int(pid))
with self._lock:
self._paused = False
self._emit_line("[继续] 已恢复运行进程")
return True
except Exception as e:
self._emit_line(f"[继续] 恢复失败: {e}")
return False
def stop(self) -> None:
if not self.is_running():
return
assert self._proc is not None
if self.is_paused():
self.resume()
self._emit_line("[停止] 正在终止进程...")
self._proc.terminate()
try:
@@ -519,6 +578,12 @@ class Runner:
self._emit_line(f"[停止] 强制终止进程 (pid={pid}) ...")
if self.is_paused() and pid:
try:
resume_process_tree(int(pid))
except Exception:
pass
# 先尝试通过 Popen 对象 kill
try:
if proc is not None and proc.poll() is None:
@@ -552,3 +617,4 @@ class Runner:
with self._lock:
self._proc = None
self._pid = None
self._paused = False