refactor(fudai): 抽 run_once_basic 中"预约状态文件写入"闭包到 write_reservations_state_throttled

将 run_once_basic 中的 _write_state 内联闭包(约 87 行)抽到模块级
fudai_detail_helpers.write_reservations_state_throttled。该闭包:
- 节流(1 秒内重复调用直接返回)避免高频写盘卡顿
- 格式化 reservations 为 dict 列表 + 写 JSON 状态文件

主文件中保留薄包装的 _write_state 闭包仅一行调用 helper,
保持原有签名供 process_reservation_attend_window 等回调使用。

douyin_fudai.py: 2638 → 2558 行 (-80)
fudai_detail_helpers.py: 1115 → 1198 行

douyin_fudai.py 跌破 2600 行(4121 → 2558,本会话累计削减 1563 行 = -38%)

验证:
- pytest 1013 passed
- compile OK

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 11:31:12 +08:00
1 parent 48f4af5848
commit a90a885a51
2 files changed
+88 -85

No files matched your search

+5 -85
View File
@@ -811,92 +811,12 @@ class fudai_analyse:
douyin_reservation_logic._sort_reservations_inplace(self.reservations) # type: ignore[attr-defined]
def _write_state() -> None:
# 将预约队列写入状态文件,供外部查看(格式与 douyin_fudai_core.py 一致)
try:
# 节流:避免高频写盘(某些环境下会造成明显卡顿甚至看似卡死)
now_ts = time.time()
last_ts = float(
getattr(self, "_reservations_state_last_write_ts", 0.0)
or 0.0
)
if (now_ts - last_ts) < 1.0:
return
self._reservations_state_last_write_ts = now_ts
"""将预约队列写入状态文件(节流,避免高频写盘卡顿)。"""
from douyin.core.fudai_modules.fudai_detail_helpers import (
write_reservations_state_throttled,
)
import json
os.makedirs(
os.path.dirname(self._reservations_state_path),
exist_ok=True,
)
# 格式化预约队列数据(与 douyin_reservation.py 格式一致)
items = []
for r in self.reservations or []:
try:
ft = float(r.get("future_ts") or 0)
except Exception:
ft = 0.0
try:
ft_str = (
datetime.fromtimestamp(ft).strftime(
"%Y-%m-%d %H:%M:%S"
)
if ft
else ""
)
except Exception:
ft_str = str(r.get("future_str", ""))
items.append(
{
"prize_value_yuan": float(
r.get("prize_value_yuan") or 0
),
"content": str(r.get("content") or ""),
"future_ts": ft,
"future_str": ft_str,
"pos_seen": int(r.get("pos_seen") or 0),
"room_title": str(r.get("room_title", "") or ""),
"room_title_raw": str(
r.get("room_title_raw", "") or ""
),
"created_ts": float(r.get("created_ts") or 0),
}
)
# 获取当前直播间标题(用于状态文件)
current_title = str(
getattr(self, "current_room_title", "") or ""
)
current_title_raw = str(
getattr(self, "current_room_title_raw", "") or ""
)
# 写入完整的状态数据
data = {
"updated_ts": time.time(),
"reason": "process_reservation",
"room_pos": int(getattr(self, "room_pos", 0) or 0),
"current_room_title": current_title,
"current_room_title_raw": current_title_raw,
"reservations": items,
}
with open(
self._reservations_state_path, "w", encoding="utf-8"
) as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as exc:
try:
self._log_kv(
"预约",
"写入预约队列状态文件失败",
error=str(exc),
error_type=type(exc).__name__,
)
except Exception:
pass
write_reservations_state_throttled(self)
focus_ref = [float(self.reservation_focus_until_ts or 0.0)]
after_action_ref = [
@@ -42,6 +42,89 @@ def log_screenshot_failed(
pass
def write_reservations_state_throttled(analyser) -> None:
"""run_once_basic 中的 _write_state 回调实现:将预约队列写入状态文件。
节流:1 秒内重复调用直接返回,避免高频写盘卡顿。
格式与 douyin_fudai_core.py 一致:包含 updated_ts / reason / room_pos /
current_room_title / current_room_title_raw / reservations 列表。
"""
try:
# 节流:避免高频写盘
now_ts = time.time()
last_ts = float(
getattr(analyser, "_reservations_state_last_write_ts", 0.0) or 0.0
)
if (now_ts - last_ts) < 1.0:
return
analyser._reservations_state_last_write_ts = now_ts
import json
import os
from datetime import datetime
os.makedirs(
os.path.dirname(analyser._reservations_state_path),
exist_ok=True,
)
# 格式化预约队列数据
items = []
for r in analyser.reservations or []:
try:
ft = float(r.get("future_ts") or 0)
except Exception:
ft = 0.0
try:
ft_str = (
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
if ft
else ""
)
except Exception:
ft_str = str(r.get("future_str", ""))
items.append(
{
"prize_value_yuan": float(r.get("prize_value_yuan") or 0),
"content": str(r.get("content") or ""),
"future_ts": ft,
"future_str": ft_str,
"pos_seen": int(r.get("pos_seen") or 0),
"room_title": str(r.get("room_title", "") or ""),
"room_title_raw": str(r.get("room_title_raw", "") or ""),
"created_ts": float(r.get("created_ts") or 0),
}
)
current_title = str(getattr(analyser, "current_room_title", "") or "")
current_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
)
data = {
"updated_ts": time.time(),
"reason": "process_reservation",
"room_pos": int(getattr(analyser, "room_pos", 0) or 0),
"current_room_title": current_title,
"current_room_title_raw": current_title_raw,
"reservations": items,
}
with open(analyser._reservations_state_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as exc:
try:
analyser._log_kv(
"预约",
"写入预约队列状态文件失败",
error=str(exc),
error_type=type(exc).__name__,
)
except Exception:
pass
def initialize_reservations_queue(analyser) -> None:
"""run_once_basic 步骤 0.1.1:初始化预约队列。