refactor(fudai): 拆出截图重试块(2822→2783,-39 行)
把 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) <noreply@anthropic.com>
This commit is contained in:
1 parent
6cf3ce9a72
commit
e6da24c2a6
3 files changed
+104
-42
No files matched your search
@@ -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
|
||||
|
||||
# 结构化预日志,记录缓存的阶段诊断结果(若存在),便于排查为何后续没复用首次诊断结果
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user