refactor(reservation): 提取 attend_window 中 2 个早返回块到 attend_window_helpers
将 process_reservation_attend_window 中两个结构相同的早返回块抽出: - handle_already_participated:检测到 ft 已被参与,移除队头并写状态 - handle_invalid_future_ts:future_ts ≤ 0,丢弃队头并写状态 新增私有 _write_state_unified 封装"safe_write_state vs 裸 callback" 两条路径,避免主流程重复 if/else。 attend_window.py: 1437 → 1372 行 (-65) 新增 attend_window_helpers.py: 116 行 验证: - pytest 1013 passed - compile OK - stub 调用两条早返回路径均返回 False Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3b600c9f64
commit
c509bf1262
2 files changed
+128
-78
No files matched your search
@@ -271,59 +271,15 @@ def process_reservation_attend_window(
|
||||
ft = 0.0
|
||||
|
||||
# 检查是否已经参与过该预约(通过 _participated_fudai_future_ts)
|
||||
try:
|
||||
participated_fudai_ts = getattr(
|
||||
analyser, "_participated_fudai_future_ts", set()
|
||||
)
|
||||
if not isinstance(participated_fudai_ts, set):
|
||||
participated_fudai_ts = set()
|
||||
# 检查是否已经参与过(允许5秒误差)
|
||||
already_participated = False
|
||||
for participated_ts in participated_fudai_ts:
|
||||
if abs(ft - participated_ts) <= 5.0:
|
||||
already_participated = True
|
||||
break
|
||||
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)
|
||||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||||
safe_write_state(
|
||||
write_state_callback,
|
||||
analyser,
|
||||
format_reservation_context(
|
||||
item=target,
|
||||
reservations=reservations,
|
||||
reason="已参与过",
|
||||
),
|
||||
)
|
||||
else:
|
||||
try:
|
||||
write_state_callback()
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
except Exception as e:
|
||||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||||
log_reservation_error(
|
||||
analyser,
|
||||
e,
|
||||
format_reservation_context(
|
||||
item=target,
|
||||
reservations=reservations,
|
||||
),
|
||||
level="WARNING",
|
||||
stage="检查是否已参与",
|
||||
)
|
||||
else:
|
||||
pass
|
||||
from .attend_window_helpers import (
|
||||
handle_already_participated,
|
||||
handle_invalid_future_ts,
|
||||
)
|
||||
|
||||
if handle_already_participated(
|
||||
analyser, reservations, target, ft, write_state_callback
|
||||
):
|
||||
return False
|
||||
|
||||
# 添加目标预约的详细信息日志
|
||||
try:
|
||||
@@ -348,31 +304,9 @@ def process_reservation_attend_window(
|
||||
|
||||
# 没有合法 future_ts,则直接丢弃
|
||||
if ft <= 0:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] process_reservation_attend_window:预约 future_ts 无效,丢弃",
|
||||
future_ts=ft,
|
||||
target=target,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
reservations.pop(0)
|
||||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||||
safe_write_state(
|
||||
write_state_callback,
|
||||
analyser,
|
||||
format_reservation_context(
|
||||
item=target,
|
||||
reservations=reservations,
|
||||
reason="future_ts 无效",
|
||||
),
|
||||
)
|
||||
else:
|
||||
try:
|
||||
write_state_callback()
|
||||
except Exception:
|
||||
pass
|
||||
handle_invalid_future_ts(
|
||||
analyser, reservations, target, ft, write_state_callback
|
||||
)
|
||||
return False
|
||||
|
||||
# 仅在"即将开奖窗口"(包括已过一小段时间)内才触发回退
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
"""attend_window 内部辅助函数。
|
||||
|
||||
从 process_reservation_attend_window 中抽出的相对独立的早返回块。
|
||||
- handle_already_participated:检测到本预约 future_ts 已被参与,移除队头并写状态
|
||||
- handle_invalid_future_ts:future_ts 无效,丢弃队头并写状态
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, List
|
||||
|
||||
from ._common import (
|
||||
ReservationItem,
|
||||
_RESERVATION_ERROR_UTILS_AVAILABLE,
|
||||
format_reservation_context,
|
||||
safe_write_state,
|
||||
)
|
||||
|
||||
|
||||
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 无效"
|
||||
)
|
||||
Reference in new issue
Block a user