将 process_reservation_attend_window 中两个纯日志块抽到 helper: - log_time_window_not_yet:时间窗口未到,跳过回退 - log_missed_window:已明显错过开奖时间,记录 missed_seconds attend_window.py: 1276 → 1247 行 (-29) attend_window_helpers.py: 276 → 329 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
330 lines
10 KiB
Python
330 lines
10 KiB
Python
"""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 (
|
||
ReservationItem,
|
||
_RESERVATION_ERROR_UTILS_AVAILABLE,
|
||
format_reservation_context,
|
||
safe_write_state,
|
||
)
|
||
|
||
|
||
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 log_attend_window_detail_check(
|
||
analyser: Any,
|
||
now_ts: float,
|
||
focus_until: float,
|
||
ft: float,
|
||
dynamic_ahead: int,
|
||
join_ahead: float,
|
||
poll_before: float,
|
||
est_back_times: int,
|
||
) -> None:
|
||
"""记录"详细检查"日志(锁定期状态 + 时间窗口)。纯日志,无副作用。"""
|
||
try:
|
||
from datetime import datetime
|
||
|
||
now_str = datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
time_window_start_ts = ft - dynamic_ahead
|
||
time_window_start_str = (
|
||
datetime.fromtimestamp(time_window_start_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
if time_window_start_ts > 0
|
||
else "无效"
|
||
)
|
||
time_window_end_ts = ft + max(0.0, poll_before)
|
||
time_window_end_str = (
|
||
datetime.fromtimestamp(time_window_end_ts).strftime("%Y-%m-%d %H:%M:%S")
|
||
if time_window_end_ts > 0
|
||
else "无效"
|
||
)
|
||
ft_str = (
|
||
datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S")
|
||
if ft > 0
|
||
else "无效"
|
||
)
|
||
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window 详细检查",
|
||
now_ts=int(now_ts),
|
||
now_str=now_str,
|
||
focus_until=int(focus_until) if focus_until > 0 else 0,
|
||
target_future_ts=int(ft),
|
||
target_future_str=ft_str,
|
||
dynamic_ahead=dynamic_ahead,
|
||
join_ahead=join_ahead,
|
||
poll_before=poll_before,
|
||
est_back_times=est_back_times,
|
||
time_window_start=int(time_window_start_ts),
|
||
time_window_start_str=time_window_start_str,
|
||
time_window_end=int(time_window_end_ts),
|
||
time_window_end_str=time_window_end_str,
|
||
remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0,
|
||
remain_seconds_to_window=(
|
||
int(time_window_start_ts - now_ts)
|
||
if time_window_start_ts > now_ts
|
||
else 0
|
||
),
|
||
in_lock_period=bool(focus_until > 0 and now_ts < focus_until),
|
||
in_time_window=bool(now_ts >= time_window_start_ts),
|
||
passed_time_window=bool(now_ts > time_window_end_ts),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def log_time_window_not_yet(
|
||
analyser: Any,
|
||
now_ts: float,
|
||
ft: float,
|
||
dynamic_ahead: int,
|
||
) -> None:
|
||
"""记录"时间窗口未到,跳过回退"日志。纯日志,无副作用。"""
|
||
try:
|
||
from datetime import datetime
|
||
|
||
time_window_start_ts = ft - dynamic_ahead
|
||
time_window_start_str = (
|
||
datetime.fromtimestamp(time_window_start_ts).strftime(
|
||
"%Y-%m-%d %H:%M:%S"
|
||
)
|
||
if time_window_start_ts > 0
|
||
else "无效"
|
||
)
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 时间窗口未到,跳过回退",
|
||
now_ts=int(now_ts),
|
||
now_str=datetime.fromtimestamp(now_ts).strftime("%Y-%m-%d %H:%M:%S"),
|
||
time_window_start=int(time_window_start_ts),
|
||
time_window_start_str=time_window_start_str,
|
||
remain_seconds=int((ft - dynamic_ahead) - now_ts),
|
||
target_future_ts=int(ft),
|
||
dynamic_ahead=dynamic_ahead,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def log_missed_window(
|
||
analyser: Any,
|
||
now_ts: float,
|
||
ft: float,
|
||
poll_before: float,
|
||
) -> None:
|
||
"""记录"已明显错过开奖时间"日志。纯日志,无副作用。"""
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 已明显错过开奖时间,清理过期预约",
|
||
future_ts=int(ft),
|
||
now_ts=int(now_ts),
|
||
poll_before=poll_before,
|
||
missed_seconds=int(now_ts - (ft + max(0.0, poll_before))),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
|
||
def _write_state_unified(
|
||
write_state_callback: Callable[[], None],
|
||
analyser: Any,
|
||
target: ReservationItem,
|
||
reservations: List[ReservationItem],
|
||
reason: str,
|
||
) -> None:
|
||
"""统一的"写状态"封装:优先走 safe_write_state,失败回退到裸 callback。"""
|
||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||
safe_write_state(
|
||
write_state_callback,
|
||
analyser,
|
||
format_reservation_context(
|
||
item=target,
|
||
reservations=reservations,
|
||
reason=reason,
|
||
),
|
||
)
|
||
else:
|
||
try:
|
||
write_state_callback()
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def handle_already_participated(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
target: ReservationItem,
|
||
ft: float,
|
||
write_state_callback: Callable[[], None],
|
||
) -> bool:
|
||
"""检查 ft 是否已经参与过;若已参与则移除队头并写状态,返回 True。
|
||
|
||
判定规则:_participated_fudai_future_ts 中存在与 ft 误差 ≤ 5 秒的时间戳。
|
||
"""
|
||
try:
|
||
participated_fudai_ts = getattr(
|
||
analyser, "_participated_fudai_future_ts", set()
|
||
)
|
||
if not isinstance(participated_fudai_ts, set):
|
||
participated_fudai_ts = set()
|
||
|
||
already_participated = any(
|
||
abs(ft - participated_ts) <= 5.0
|
||
for participated_ts in participated_fudai_ts
|
||
)
|
||
|
||
if already_participated:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:检测到已参与过该预约,从队列中移除",
|
||
target_future_ts=int(ft) if ft > 0 else 0,
|
||
)
|
||
except Exception:
|
||
pass
|
||
reservations.pop(0)
|
||
_write_state_unified(
|
||
write_state_callback, analyser, target, reservations, "已参与过"
|
||
)
|
||
return True
|
||
except Exception as e:
|
||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||
from ._common import log_reservation_error
|
||
|
||
log_reservation_error(
|
||
analyser,
|
||
e,
|
||
format_reservation_context(item=target, reservations=reservations),
|
||
level="WARNING",
|
||
stage="检查是否已参与",
|
||
)
|
||
|
||
return False
|
||
|
||
|
||
def handle_invalid_future_ts(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
target: ReservationItem,
|
||
ft: float,
|
||
write_state_callback: Callable[[], None],
|
||
) -> None:
|
||
"""future_ts 无效(≤0),丢弃队头并写状态。调用方调用后应直接 return False。"""
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] process_reservation_attend_window:预约 future_ts 无效,丢弃",
|
||
future_ts=ft,
|
||
target=target,
|
||
)
|
||
except Exception:
|
||
pass
|
||
reservations.pop(0)
|
||
_write_state_unified(
|
||
write_state_callback, analyser, target, reservations, "future_ts 无效"
|
||
)
|
||
|
||
|
||
def process_lock_period(
|
||
analyser: Any,
|
||
reservations: List[ReservationItem],
|
||
focus_until: float,
|
||
now_ts: float,
|
||
poll_before: float,
|
||
reservation_focus_until_ts_ref: List[float],
|
||
) -> float:
|
||
"""锁定期内:优先处理"锁定的预约",避免被队头其它预约打断。
|
||
|
||
- 找到与 focus_until 对应的预约(误差 ≤ 5 秒);
|
||
- 找不到 → 视为锁定失效,清除 focus_until_ts_ref;
|
||
- 找到但不在队头 → 把该项移到队头;
|
||
- 已在队头 → 不动。
|
||
|
||
返回(可能更新过的)focus_until。
|
||
"""
|
||
locked_idx = None
|
||
try:
|
||
for i, item in enumerate(reservations):
|
||
try:
|
||
item_ft = float(item.get("future_ts") or 0.0)
|
||
except Exception:
|
||
continue
|
||
if item_ft <= 0:
|
||
continue
|
||
expected_focus_until = item_ft + max(0.0, poll_before)
|
||
if abs(focus_until - expected_focus_until) <= 5.0:
|
||
locked_idx = i
|
||
break
|
||
except Exception:
|
||
locked_idx = None
|
||
|
||
if locked_idx is None:
|
||
try:
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 检测到锁定期已失效(锁定预约不在队列),清除锁定期",
|
||
focus_until=int(focus_until),
|
||
lock_period_remain_seconds=int(focus_until - now_ts),
|
||
reservations_count=len(reservations),
|
||
)
|
||
except Exception:
|
||
pass
|
||
try:
|
||
if reservation_focus_until_ts_ref:
|
||
reservation_focus_until_ts_ref[0] = 0.0
|
||
else:
|
||
reservation_focus_until_ts_ref.append(0.0)
|
||
except Exception:
|
||
pass
|
||
return 0.0
|
||
|
||
if locked_idx != 0:
|
||
try:
|
||
locked_item = reservations.pop(locked_idx)
|
||
reservations.insert(0, locked_item)
|
||
analyser._log_kv(
|
||
"预约",
|
||
"[预约] 锁定期内固定处理锁定预约(忽略队头其他预约)",
|
||
focus_until=int(focus_until),
|
||
locked_future_ts=int(float(locked_item.get("future_ts") or 0.0)),
|
||
moved_from_index=int(locked_idx),
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
return focus_until
|