From 6aec4ae31406599ac7a0c4521c74e1e687db3aa5 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 12:53:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20process=5F?= =?UTF-8?q?reservation=5Fattend=5Fwindow=20=E6=9C=AB=E5=B0=BE"=E5=9B=9E?= =?UTF-8?q?=E9=80=80=E5=A4=B1=E8=B4=A5"=E5=9D=97=E5=88=B0=20log=5Fseek=5Ff?= =?UTF-8?q?ailure=5Fand=5Ffinish?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将函数末尾"回退失败:记录日志,等待后续重试或清理"块(约 36 行)抽到 helper: - 写"回退失败"日志 - 调用 write_state_callback - 写"未触发回退"完成日志 attend_window.py: 1059 → 1030 行 (-29) attend_window_helpers.py: 594 → 645 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 37 ++------------ .../attend_window_helpers.py | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index f19c242..5ee2f3a 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -1019,40 +1019,11 @@ def process_reservation_attend_window( return True # 回退失败:记录日志,等待后续重试或清理 - try: - analyser._log_kv( - "预约", - "[预约] 回退到预约直播间失败,保留队列以便后续重试或清理", - future_ts=int(ft), - now_ts=int(now_ts), - remain_seconds_to_open=int(ft - now_ts), - target_pos=int(target.get("pos_seen", 0)), - current_room_pos=int(room_pos), - ) - except Exception: - pass + from .attend_window_helpers import log_seek_failure_and_finish - try: - write_state_callback() - except Exception: - pass - - # 添加函数完成日志 - try: - from datetime import datetime - - elapsed = time.time() - start_time - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window 返回结果", - result="未触发回退", - future_ts=int(ft) if ft > 0 else 0, - now_ts=int(now_ts), - remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, - elapsed_seconds=round(elapsed, 3), - ) - except Exception: - pass + log_seek_failure_and_finish( + analyser, target, ft, now_ts, room_pos, start_time, write_state_callback + ) return False diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 4f5d472..37946a5 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -105,6 +105,57 @@ def log_attend_window_detail_check( pass +def log_seek_failure_and_finish( + analyser: Any, + target: ReservationItem, + ft: float, + now_ts: float, + room_pos: int, + start_time: float, + write_state_callback: Callable[[], None], +) -> None: + """process_reservation_attend_window 末尾"回退失败"块。 + + 流程: + 1. 写"回退到预约直播间失败"日志 + 2. 调用 write_state_callback + 3. 写"未触发回退"完成日志 + """ + import time as _time + + try: + analyser._log_kv( + "预约", + "[预约] 回退到预约直播间失败,保留队列以便后续重试或清理", + future_ts=int(ft), + now_ts=int(now_ts), + remain_seconds_to_open=int(ft - now_ts), + target_pos=int(target.get("pos_seen", 0)), + current_room_pos=int(room_pos), + ) + except Exception: + pass + + try: + write_state_callback() + except Exception: + pass + + try: + elapsed = _time.time() - start_time + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window 返回结果", + result="未触发回退", + future_ts=int(ft) if ft > 0 else 0, + now_ts=int(now_ts), + remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, + elapsed_seconds=round(elapsed, 3), + ) + except Exception: + pass + + def execute_seek_reservation_with_logging( analyser: Any, target: ReservationItem,