From e6da24c2a6bacefdcaa520eda53a6e9b45c3dfdd Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:49:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=E6=88=AA?= =?UTF-8?q?=E5=9B=BE=E9=87=8D=E8=AF=95=E5=9D=97=EF=BC=882822=E2=86=922783?= =?UTF-8?q?=EF=BC=8C-39=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 run_once_basic 中 '截图 + 重试 + 失败诊断日志' 的 45 行块抽到 fudai_run_blocks.take_screenshot_with_retries: - 失败时自动写入完整诊断日志(含 reservation flags / room_pos 等) - 返回 (ok, elapsed);ok=False 时主流程直接 return 新增 4 个单元测试覆盖:首次成功 / 二次成功 / 全失败写诊断 / get_screenshot 抛错。 1119 → 1123 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 45 +------------- douyin/core/fudai_modules/fudai_run_blocks.py | 62 +++++++++++++++++++ tests/test_fudai_run_blocks.py | 39 ++++++++++++ 3 files changed, 104 insertions(+), 42 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 409d637..558749e 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1826,49 +1826,10 @@ class fudai_analyse: return # 1. 截图 + 阶段诊断 - screenshot_start = time.time() - max_screenshot_retries = 3 - ok = False - - for retry in range(max_screenshot_retries): - ok = self.operation.get_screenshot(self.device_id, use_service=False) - if ok: - break - - if retry < max_screenshot_retries - 1: - self._log_kv( - "截图", - f"截图失败,重试中 ({retry + 1}/{max_screenshot_retries})", - retry=retry + 1, - max_retries=max_screenshot_retries, - ) - try: - self.operation.delay(1.0) # 等待1秒后重试 - except Exception: - time.sleep(1.0) - - screenshot_elapsed = time.time() - screenshot_start + ok, screenshot_elapsed = fudai_run_blocks.take_screenshot_with_retries( + self, start_time, max_retries=3 + ) if not ok: - elapsed = time.time() - start_time - self._log_kv( - "截图", - "获取截图失败(多次重试后仍失败)", - ok=0, - retries=max_screenshot_retries, - elapsed_seconds=round(elapsed, 2), - screenshot_elapsed_seconds=round(screenshot_elapsed, 2), - reservation_after_action_pending=bool( - getattr(self, "reservation_after_action_pending", False) - ), - reservation_focus_until_ts=float( - getattr(self, "reservation_focus_until_ts", 0.0) or 0.0 - ), - waiting_for_lottery=bool( - getattr(self, "_waiting_for_lottery_result", False) - ), - room_pos=int(getattr(self, "room_pos", 0) or 0), - suggestion="检查设备连接状态和ADB服务", - ) return # 结构化预日志,记录缓存的阶段诊断结果(若存在),便于排查为何后续没复用首次诊断结果 diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 4ad6c37..67f5faa 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -655,6 +655,68 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: ) +def take_screenshot_with_retries( + analyser: Any, start_time: float, max_retries: int = 3 +) -> tuple[bool, float]: + """带重试的截图。 + + 返回 (ok, screenshot_elapsed_seconds):ok=False 时主流程应记录失败日志后 return。 + 若 ok=False 已自动写入"获取截图失败"的诊断日志。 + """ + screenshot_start = time.time() + ok = False + + for retry in range(max_retries): + try: + ok = analyser.operation.get_screenshot(analyser.device_id, use_service=False) + except Exception: + ok = False + if ok: + break + + if retry < max_retries - 1: + try: + analyser._log_kv( + "截图", + f"截图失败,重试中 ({retry + 1}/{max_retries})", + retry=retry + 1, + max_retries=max_retries, + ) + except Exception: + pass + try: + analyser.operation.delay(1.0) + except Exception: + time.sleep(1.0) + + screenshot_elapsed = time.time() - screenshot_start + if not ok: + try: + elapsed = time.time() - start_time + analyser._log_kv( + "截图", + "获取截图失败(多次重试后仍失败)", + ok=0, + retries=max_retries, + elapsed_seconds=round(elapsed, 2), + screenshot_elapsed_seconds=round(screenshot_elapsed, 2), + reservation_after_action_pending=bool( + getattr(analyser, "reservation_after_action_pending", False) + ), + reservation_focus_until_ts=float( + getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0 + ), + waiting_for_lottery=bool( + getattr(analyser, "_waiting_for_lottery_result", False) + ), + room_pos=int(getattr(analyser, "room_pos", 0) or 0), + suggestion="检查设备连接状态和ADB服务", + ) + except Exception: + pass + return ok, screenshot_elapsed + + def handle_recommend_page_stage(analyser: Any, stage: str, seen: Any) -> None: """stage == "RECOMMEND_PAGE":点击进入直播页 → 进入直播间(连点两次)。""" analyser._log_kv( diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 102ab7c..b02ed58 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -384,3 +384,42 @@ def test_in_live_room_logs_completion(): if len(c.args) >= 2 and "run_once_basic完成(处理直播间)" in c.args[1] ] assert len(completion) == 1 + + +# ---- take_screenshot_with_retries ---- # + +def test_screenshot_success_first_try(): + a = _make_analyser() + a.operation.get_screenshot.return_value = True + ok, elapsed = fudai_run_blocks.take_screenshot_with_retries(a, time.time()) + assert ok is True + assert elapsed >= 0 + + +def test_screenshot_success_second_try(): + a = _make_analyser() + a.operation.get_screenshot.side_effect = [False, True] + ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=3) + assert ok is True + # 重试一次,所以 delay 被调用一次 + assert a.operation.delay.call_count == 1 + + +def test_screenshot_all_retries_fail_logs_error(): + a = _make_analyser() + a.operation.get_screenshot.return_value = False + ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=3) + assert ok is False + # 应记录"获取截图失败(多次重试后仍失败)" + failure_logs = [ + c for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "获取截图失败" in c.args[1] + ] + assert len(failure_logs) == 1 + + +def test_screenshot_get_screenshot_exception_treated_as_false(): + a = _make_analyser() + a.operation.get_screenshot.side_effect = RuntimeError("adb fail") + ok, _ = fudai_run_blocks.take_screenshot_with_retries(a, time.time(), max_retries=2) + assert ok is False