From 41fc4ecbe0e4c8ed5d4594f1a413835b77becb95 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:53:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=E9=A6=96?= =?UTF-8?q?=E6=AC=A1=E8=BF=90=E8=A1=8C=E6=B8=85=E7=A9=BA=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=9D=97=EF=BC=882783=E2=86=922661=EF=BC=8C-?= =?UTF-8?q?122=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 中"首次运行清空预约队列与写空状态文件"的 130 行块抽到 fudai_run_blocks.maybe_clear_reservation_queue_on_first_run: - 首次运行:清空 reservations / 写空状态文件 / 标记 _first_run_once_basic=False - 非首次:仅记录日志保持当前队列 新增 4 个单元测试覆盖:首次清空 / 非首次保持 / 无 state_path 兜底 / 日志异常吞噬。 1123 → 1127 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 124 +----------------- douyin/core/fudai_modules/fudai_run_blocks.py | 119 +++++++++++++++++ tests/test_fudai_run_blocks.py | 42 ++++++ 3 files changed, 162 insertions(+), 123 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 558749e..ac0423d 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1217,129 +1217,7 @@ class fudai_analyse: reservation_check_start = time.time() self._log_kv("预约", "开始预约检查", timestamp=reservation_check_start) # 每次开始运行时丢弃旧的预约队列,不加载状态文件 - try: - - # 如果是第一次运行,清空预约队列,不加载状态文件 - is_first_run = getattr(self, "_first_run_once_basic", True) - # 记录清空前的队列状态(用于调试) - old_reservations_count = 0 - try: - old_reservations_count = ( - len(self.reservations) - if hasattr(self, "reservations") and self.reservations - else 0 - ) - except Exception: - pass - - if is_first_run: - try: - self._log_kv( - "预约", - "[预约] 第一次运行:丢弃旧的预约队列,不加载状态文件", - old_reservations_count=old_reservations_count, - ) - except Exception: - pass - # 清空预约队列(强制清空,确保每次启动时队列为空) - try: - # 先记录清空前的状态 - before_clear = ( - len(self.reservations) - if hasattr(self, "reservations") and self.reservations - else 0 - ) - self.reservations = [] - after_clear = ( - len(self.reservations) - if hasattr(self, "reservations") - else 0 - ) - - # 同时清空状态文件,确保外部查看时也是空的 - try: - - state_path = getattr(self, "_reservations_state_path", None) - if state_path: - # 写入空队列到状态文件 - from douyin.reservation.douyin_reservation import ( - write_reservations_state, - ) - - write_reservations_state( - state_path=state_path, - reservations=[], - room_pos=0, - current_room_title="", - current_room_title_raw="", - reason="startup_clear", - ) - try: - self._log_kv( - "预约", - "[预约] 已清空预约队列并更新状态文件", - before_clear=before_clear, - after_clear=after_clear, - ) - except Exception: - pass - except Exception as exc: - try: - self._log_kv( - "预约", - "[预约] 清空状态文件时出错", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass - except Exception as exc: - # 如果清空失败,尝试直接设置 - try: - if hasattr(self, "reservations"): - self.reservations = [] - self._log_kv( - "预约", - "[预约] 清空预约队列时出错,已尝试直接设置", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass - # 标记已运行过 - try: - self._first_run_once_basic = False - except Exception: - pass - else: - # 如果不是第一次运行,也记录日志(用于调试) - try: - current_count = ( - len(self.reservations) - if hasattr(self, "reservations") and self.reservations - else 0 - ) - self._log_kv( - "预约", - "[预约] 非第一次运行,保持当前预约队列", - reservations_count=current_count, - is_first_run=False, - ) - except Exception: - pass - - # 不再从状态文件加载预约数据,每次启动时都从空队列开始 - # 已移除从状态文件加载预约数据的逻辑,每次启动时预约队列为空 - except Exception as exc: - try: - self._log_kv( - "预约", - "[预约] 初始化预约队列时出错", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass + fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(self) high_value_enabled = getattr(self, "high_value_fudai_enabled", False) reservations_list = getattr(self, "reservations", None) diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 67f5faa..6435123 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -655,6 +655,125 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: ) +def maybe_clear_reservation_queue_on_first_run(analyser: Any) -> None: + """如果是首次运行,清空预约队列并把状态文件也写空。 + + 后续运行只记录日志、保持当前队列。本函数捕获所有异常,仅记录到日志, + 不影响主流程。 + """ + try: + is_first_run = getattr(analyser, "_first_run_once_basic", True) + try: + old_reservations_count = ( + len(analyser.reservations) + if hasattr(analyser, "reservations") and analyser.reservations + else 0 + ) + except Exception: + old_reservations_count = 0 + + if not is_first_run: + try: + current_count = ( + len(analyser.reservations) + if hasattr(analyser, "reservations") and analyser.reservations + else 0 + ) + analyser._log_kv( + "预约", + "[预约] 非第一次运行,保持当前预约队列", + reservations_count=current_count, + is_first_run=False, + ) + except Exception: + pass + return + + try: + analyser._log_kv( + "预约", + "[预约] 第一次运行:丢弃旧的预约队列,不加载状态文件", + old_reservations_count=old_reservations_count, + ) + except Exception: + pass + + try: + before_clear = ( + len(analyser.reservations) + if hasattr(analyser, "reservations") and analyser.reservations + else 0 + ) + analyser.reservations = [] + after_clear = ( + len(analyser.reservations) + if hasattr(analyser, "reservations") + else 0 + ) + + try: + state_path = getattr(analyser, "_reservations_state_path", None) + if state_path: + from douyin.reservation.douyin_reservation import ( + write_reservations_state, + ) + + write_reservations_state( + state_path=state_path, + reservations=[], + room_pos=0, + current_room_title="", + current_room_title_raw="", + reason="startup_clear", + ) + try: + analyser._log_kv( + "预约", + "[预约] 已清空预约队列并更新状态文件", + before_clear=before_clear, + after_clear=after_clear, + ) + except Exception: + pass + except Exception as exc: + try: + analyser._log_kv( + "预约", + "[预约] 清空状态文件时出错", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + except Exception as exc: + try: + if hasattr(analyser, "reservations"): + analyser.reservations = [] + analyser._log_kv( + "预约", + "[预约] 清空预约队列时出错,已尝试直接设置", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + + try: + analyser._first_run_once_basic = False + except Exception: + pass + except Exception as exc: + try: + analyser._log_kv( + "预约", + "[预约] 初始化预约队列时出错", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + + def take_screenshot_with_retries( analyser: Any, start_time: float, max_retries: int = 3 ) -> tuple[bool, float]: diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index b02ed58..94de1fc 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -423,3 +423,45 @@ def test_screenshot_get_screenshot_exception_treated_as_false(): 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 + + +# ---- maybe_clear_reservation_queue_on_first_run ---- # + +def test_clear_queue_first_run_clears_reservations(): + a = MagicMock() + a._first_run_once_basic = True + a.reservations = [{"future_ts": 100}, {"future_ts": 200}] + a._reservations_state_path = None # 跳过 write 状态文件 + fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a) + assert a.reservations == [] + assert a._first_run_once_basic is False + + +def test_clear_queue_non_first_run_keeps_reservations(): + a = MagicMock() + a._first_run_once_basic = False + original = [{"future_ts": 100}] + a.reservations = original + fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a) + # 队列保持 + assert a.reservations is original + + +def test_clear_queue_first_run_no_state_path_still_clears(): + a = MagicMock() + a._first_run_once_basic = True + a.reservations = [{"future_ts": 100}] + a._reservations_state_path = None + fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a) + assert a.reservations == [] + + +def test_clear_queue_swallows_log_exceptions(): + a = MagicMock() + a._first_run_once_basic = True + a.reservations = [{"future_ts": 100}] + a._log_kv.side_effect = RuntimeError("boom") + a._reservations_state_path = None + # 应不抛错 + fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a) + assert a.reservations == []