refactor(fudai): 抽 run_once_basic 中"初始化预约队列"块到 initialize_reservations_queue
将 run_once_basic 步骤 0.1.1"第一次运行:清空预约队列 + 写状态文件" 独立块(约 122 行)抽到 fudai_detail_helpers.initialize_reservations_queue。 逻辑: - 第一次运行:reservations = [] + 写空队列到状态文件 + _first_run_once_basic = False - 非第一次:保留当前队列,仅记录调试日志 douyin_fudai.py: 2755 → 2638 行 (-117) fudai_detail_helpers.py: 993 → 1115 行 douyin_fudai.py 跌破 2700 行(4121 → 2638,本会话累计削减 1483 行 = -36%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3af7c6d894
commit
48f4af5848
2 files changed
+127
-122
No files matched your search
+5
-122
@@ -728,130 +728,13 @@ class fudai_analyse:
|
||||
try:
|
||||
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
|
||||
# 初始化预约队列(第一次运行时清空 + 写状态文件)
|
||||
from douyin.core.fudai_modules.fudai_detail_helpers import (
|
||||
initialize_reservations_queue,
|
||||
)
|
||||
|
||||
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
|
||||
initialize_reservations_queue(self)
|
||||
|
||||
high_value_enabled = getattr(self, "high_value_fudai_enabled", False)
|
||||
reservations_list = getattr(self, "reservations", None)
|
||||
|
||||
@@ -42,6 +42,128 @@ def log_screenshot_failed(
|
||||
pass
|
||||
|
||||
|
||||
def initialize_reservations_queue(analyser) -> None:
|
||||
"""run_once_basic 步骤 0.1.1:初始化预约队列。
|
||||
|
||||
第一次运行时:清空预约队列 + 写状态文件(reservations=[])+ 标记 _first_run_once_basic=False。
|
||||
非第一次运行:仅记录调试日志,保留当前队列。
|
||||
"""
|
||||
try:
|
||||
is_first_run = getattr(analyser, "_first_run_once_basic", True)
|
||||
|
||||
# 记录清空前的队列状态
|
||||
old_reservations_count = 0
|
||||
try:
|
||||
old_reservations_count = (
|
||||
len(analyser.reservations)
|
||||
if hasattr(analyser, "reservations") and analyser.reservations
|
||||
else 0
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if is_first_run:
|
||||
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
|
||||
else:
|
||||
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
|
||||
except Exception as exc:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 初始化预约队列时出错",
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def try_wait_for_lottery_result(analyser, start_time: float) -> bool:
|
||||
"""run_once_basic 步骤 0:如果正在等待开奖,持续 OCR 检测开奖结果。
|
||||
|
||||
|
||||
Reference in new issue
Block a user