From a90a885a51e8dbad7b55ba1128df4315fcb50b34 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 11:31:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20run=5Fonce=5Fbas?= =?UTF-8?q?ic=20=E4=B8=AD"=E9=A2=84=E7=BA=A6=E7=8A=B6=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=86=99=E5=85=A5"=E9=97=AD=E5=8C=85=E5=88=B0=20write?= =?UTF-8?q?=5Freservations=5Fstate=5Fthrottled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 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) --- douyin/core/douyin_fudai.py | 90 ++----------------- .../fudai_modules/fudai_detail_helpers.py | 83 +++++++++++++++++ 2 files changed, 88 insertions(+), 85 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 0f084b6..ef004ae 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -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 = [ diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 99a6baf..70bda55 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -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:初始化预约队列。