diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index ecde5eb..621fd79 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -149,19 +149,10 @@ def process_reservation_attend_window( pass # 超时检查 - try: - if (time.time() - start_time) > total_timeout_s: - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:超时退出(排序前)", - total_timeout_seconds=round(total_timeout_s, 3), - ) - except Exception: - pass - return False - except Exception: - pass + from .attend_window_helpers import check_total_timeout + + if check_total_timeout(analyser, start_time, total_timeout_s, "排序前"): + return False try: _sort_reservations_inplace(reservations) except Exception as e: @@ -249,19 +240,8 @@ def process_reservation_attend_window( pass # 超时检查 - try: - if (time.time() - start_time) > total_timeout_s: - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:超时退出(排序后)", - total_timeout_seconds=round(total_timeout_s, 3), - ) - except Exception: - pass - return False - except Exception: - pass + if check_total_timeout(analyser, start_time, total_timeout_s, "排序后"): + return False # 取队头(最早需要处理的一条;若在锁定期内已将锁定目标移动到队头,则这里即为锁定目标) target = reservations[0] diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 2d68b89..7306032 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -1,11 +1,13 @@ """attend_window 内部辅助函数。 从 process_reservation_attend_window 中抽出的相对独立的早返回块。 +- check_total_timeout:检查总超时是否达到,达到则记日志返回 True - handle_already_participated:检测到本预约 future_ts 已被参与,移除队头并写状态 - handle_invalid_future_ts:future_ts 无效,丢弃队头并写状态 """ from __future__ import annotations +import time from typing import Any, Callable, List from ._common import ( @@ -16,6 +18,31 @@ from ._common import ( ) +def check_total_timeout( + analyser: Any, start_time: float, total_timeout_s: float, stage_label: str +) -> bool: + """检查从 start_time 到现在是否超过 total_timeout_s。 + + 超时则写入日志并返回 True;否则返回 False。 + 任何内部异常都被吞噬(与原代码一致),返回 False。 + """ + try: + if (time.time() - start_time) > total_timeout_s: + try: + analyser._log_kv( + "预约", + f"[预约] process_reservation_attend_window:超时退出({stage_label})", + total_timeout_seconds=round(total_timeout_s, 3), + ) + except Exception: + pass + return True + except Exception: + pass + return False + + + def _write_state_unified( write_state_callback: Callable[[], None], analyser: Any,