refactor(fudai): 拆出首次运行清空预约队列块(2783→2661,-122 行)

把 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) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 02:53:46 +08:00
1 parent e6da24c2a6
commit 41fc4ecbe0
3 files changed
+162 -123

No files matched your search

+1 -123
View File
@@ -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)
@@ -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]:
+42
View File
@@ -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 == []