feat: 添加回退方式配置选项,支持滑动和按返回键回退;优化日志清理机制

This commit is contained in:
Your Name committed 2026-01-05 00:38:27 +08:00
1 parent 46733155e0
commit 982357927e
4 files changed
+125 -50

No files matched your search

+1 -1
View File
@@ -186,7 +186,7 @@
"debug_captures": {
"enabled": false,
"keep_days": 3,
"max_files": 2000,
"max_files": 200,
"auto_cleanup": true,
"async_save": false,
"log_on_complete": false
+67 -48
View File
@@ -201,64 +201,83 @@ class SwipeHandler:
if back_times <= 0:
return 0
# 向上回退:从上往下滑动(与上划切换相反方向)
try:
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
except Exception:
rx, ry = 1080, 2400
# 根据配置选择回退方式:'swipe'=滑动回退(默认),'back'=按返回键回退
mode = str(getattr(analyser, "reset_after_swipes_action", "swipe") or "swipe").lower()
if mode not in ("swipe", "back"):
mode = "swipe"
for i in range(back_times):
try:
# 向上回退:从上往下滑动(start_y < end_y)
start_x = 760 * rx // 1080
start_y = 800 * ry // 2400 # 上方起始位置
end_x = 760 * rx // 1080
end_y = 1600 * ry // 2400 # 下方结束位置
# 直接使用 ADB 内部滑动命令
# 使用配置的滑动持续时间,如果没有配置则使用300ms(比之前的200ms更快)
swipe_duration_ms = int(
getattr(analyser, "swipe_duration_ms", 300) or 300
)
if mode == "back":
# 使用返回键回退(兼容性更好,适用于无法通过滑动稳定回退的设备)
for i in range(back_times):
try:
if hasattr(analyser.operation, "_adb"):
adb_cmd = analyser.operation._adb()
else:
import os
adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb"
if not adb_cmd.startswith('"') and not adb_cmd.endswith('"'):
adb_cmd = f'"{adb_cmd}"'
cmd = (
f"{adb_cmd} -s {analyser.device_id} shell input swipe "
f"{start_x} {start_y} {end_x} {end_y} {swipe_duration_ms}"
)
subprocess.run(
cmd,
shell=True,
timeout=5.0,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
analyser.operation.click_back(analyser.device_id)
except Exception:
# 回退到 operation.swipe
analyser.operation.swipe(
analyser.device_id,
start_x,
start_y,
end_x,
end_y,
swipe_duration_ms,
)
pass
try:
analyser._log_kv( # type: ignore[attr-defined]
"流程", "回退直播间 {}/{}".format(i + 1, back_times)
"流程", "回退直播间 {}/{} (使用返回键)".format(i + 1, back_times)
)
except Exception:
pass
analyser.operation.delay(2)
else:
try:
rx = int(getattr(analyser, "resolution_ratio_x", 1080) or 1080)
ry = int(getattr(analyser, "resolution_ratio_y", 2400) or 2400)
except Exception:
pass
rx, ry = 1080, 2400
for i in range(back_times):
try:
# 向上回退:从上往下滑动(start_y < end_y)
start_x = 760 * rx // 1080
start_y = 800 * ry // 2400 # 上方起始位置
end_x = 760 * rx // 1080
end_y = 1600 * ry // 2400 # 下方结束位置
# 直接使用 ADB 内部滑动命令
# 使用配置的滑动持续时间,如果没有配置则使用300ms(比之前的200ms更快)
swipe_duration_ms = int(
getattr(analyser, "swipe_duration_ms", 300) or 300
)
try:
if hasattr(analyser.operation, "_adb"):
adb_cmd = analyser.operation._adb()
else:
import os
adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb"
if not adb_cmd.startswith('"') and not adb_cmd.endswith('"'):
adb_cmd = f'"{adb_cmd}"'
cmd = (
f"{adb_cmd} -s {analyser.device_id} shell input swipe "
f"{start_x} {start_y} {end_x} {end_y} {swipe_duration_ms}"
)
subprocess.run(
cmd,
shell=True,
timeout=5.0,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception:
# 回退到 operation.swipe
analyser.operation.swipe(
analyser.device_id,
start_x,
start_y,
end_x,
end_y,
swipe_duration_ms,
)
try:
analyser._log_kv( # type: ignore[attr-defined]
"流程", "回退直播间 {}/{}".format(i + 1, back_times)
)
except Exception:
pass
analyser.operation.delay(2)
except Exception:
pass
# 回到第一个后,计数归零
analyser.operation.delay(3)
+46 -1
View File
@@ -978,6 +978,9 @@ class LogRotationMonitor:
self._thread: Optional[threading.Thread] = None
self._last_rotation_count = 0
self._last_check_time = datetime.now()
# 清理状态,避免并发触发大量清理操作
self._cleanup_lock = threading.Lock()
self._cleanup_running = False
def start(self) -> None:
"""启动监控器"""
@@ -1034,7 +1037,11 @@ class LogRotationMonitor:
self.alert_callback("WARNING", message)
else:
print(f"[日志监控] WARNING: {message}")
# 异步尝试触发清理以释放空间(非阻塞监控线程)
try:
self._schedule_cleanup_async()
except Exception:
pass
# 检查轮转频率
current_time = datetime.now()
if (
@@ -1057,6 +1064,44 @@ class LogRotationMonitor:
if self.alert_callback:
self.alert_callback("ERROR", f"日志检查失败: {e}")
def _schedule_cleanup_async(self) -> None:
"""如果没有正在运行的清理任务,则在后台线程中启动一次清理。"""
# 快速检查,避免频繁加锁
if self._cleanup_running:
return
def _worker():
try:
with self._cleanup_lock:
self._cleanup_running = True
# 使用模块内的 cleanup_old_logs 执行清理,采用默认策略(保留30天、10GB上限)
try:
cleanup_old_logs(
logs_dir=self.logs_dir,
base_name=self.base_name,
keep_days=30,
max_total_size_gb=10.0,
compress_after_days=7,
compress=True,
)
except Exception as e:
try:
if self.alert_callback:
self.alert_callback("ERROR", f"日志自动清理失败: {e}")
else:
print(f"[日志监控] 日志自动清理失败: {e}")
except Exception:
pass
finally:
try:
with self._cleanup_lock:
self._cleanup_running = False
except Exception:
self._cleanup_running = False
t = threading.Thread(target=_worker, daemon=True)
t.start()
def start_log_monitor(
logs_dir: str,
+11
View File
@@ -182,6 +182,12 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
action="store_true",
help="关闭切换次数阈值回退到第一个直播间",
)
p.add_argument(
"--reset-after-swipes-action",
choices=["swipe", "back"],
default=None,
help="回退方式:swipe=滑动回退(默认),back=按返回键回退。",
)
p.add_argument(
"--swipe-down-back-count",
@@ -471,6 +477,11 @@ def main(argv: list[str]) -> int:
analyser.reset_after_swipes_enabled = True
if args.reset_after_swipes_disabled:
analyser.reset_after_swipes_enabled = False
if getattr(args, "reset_after_swipes_action", None) is not None:
try:
analyser.reset_after_swipes_action = str(args.reset_after_swipes_action or "swipe")
except Exception:
pass
# 下滑后返回并重新进入直播间的命令行覆盖(优先级高于 config.json)
if getattr(args, "swipe_down_back_count", None) is not None:
try: