refactor(reservation): 抽 process_reservation_attend_window 中"执行回退动作前的日志+seek 调用"块到 execute_seek_reservation_with_logging
将 76 行的"进入预约参与窗口"独立块抽到 helper: - 写"进入预约参与窗口"日志 - 计算 current_room_pos / target_pos / pos_diff - 写"准备执行回退到预约直播间"日志 - 调用 seek_reservation_room_callback(target) - 异常时记录错误日志(log_reservation_error 或 _log_kv) 返回值:(ok: bool, current_room_pos: int, target_pos: int, pos_diff: int) attend_window.py: 1121 → 1059 行 (-62) attend_window_helpers.py: 496 → 594 行 attend_window.py 跌破 1100 行(1437 → 1059,本会话累计削减 378 行 = -26%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f0150cb3f4
commit
4ce62a1907
2 files changed
+112
-76
No files matched your search
@@ -357,83 +357,21 @@ def process_reservation_attend_window(
|
||||
cleanup_expired_reservations(analyser, reservations, write_state_callback)
|
||||
return False
|
||||
|
||||
# 进入预约参与窗口:执行回退动作
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 进入预约参与窗口,准备回退到目标直播间",
|
||||
future_ts=int(ft),
|
||||
now_ts=int(now_ts),
|
||||
room_pos=int(room_pos),
|
||||
queue_len=len(reservations),
|
||||
dynamic_ahead=int(dynamic_ahead),
|
||||
est_back_times=int(est_back_times),
|
||||
remain_seconds_to_open=int(ft - now_ts),
|
||||
time_window_start=int(ft - dynamic_ahead),
|
||||
time_window_end=int(ft + max(0.0, poll_before)),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 进入预约参与窗口:执行回退动作(含日志 + seek 回调 + 异常处理)
|
||||
from .attend_window_helpers import execute_seek_reservation_with_logging
|
||||
|
||||
# 获取当前实际的 room_pos(可能已经被更新)
|
||||
try:
|
||||
current_room_pos = int(getattr(analyser, "room_pos", room_pos) or room_pos)
|
||||
except Exception:
|
||||
current_room_pos = int(room_pos)
|
||||
|
||||
# 计算位置差
|
||||
try:
|
||||
target_pos = int(target.get("pos_seen") or 0)
|
||||
except Exception:
|
||||
target_pos = 0
|
||||
pos_diff = current_room_pos - target_pos
|
||||
|
||||
# 调用上层提供的"按预约信息回退到目标直播间"的回调
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 准备执行回退到预约直播间",
|
||||
target_pos_seen=target_pos,
|
||||
current_room_pos=current_room_pos,
|
||||
pos_diff=pos_diff,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 直接调用回退回调,不需要预先下滑
|
||||
# 原因:
|
||||
# 1. 回退前已经进行了阶段诊断,处理了弹窗和福袋详情页等异常状态
|
||||
# 2. 如果已经在直播间,不需要下滑
|
||||
# 3. 预先下滑会导致 room_pos 减1,影响后续的回退计算,导致少回退一个直播间
|
||||
ok = False
|
||||
try:
|
||||
ok = bool(seek_reservation_room_callback(target))
|
||||
except Exception as e:
|
||||
if _RESERVATION_ERROR_UTILS_AVAILABLE:
|
||||
log_reservation_error(
|
||||
analyser,
|
||||
e,
|
||||
format_reservation_context(
|
||||
item=target,
|
||||
reservations=reservations,
|
||||
current_room_pos=current_room_pos,
|
||||
target_pos=target_pos,
|
||||
pos_diff=pos_diff,
|
||||
),
|
||||
level="ERROR",
|
||||
stage="执行回退操作",
|
||||
)
|
||||
else:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 执行回退时发生异常",
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
ok = False
|
||||
ok, current_room_pos, target_pos, pos_diff = execute_seek_reservation_with_logging(
|
||||
analyser,
|
||||
target,
|
||||
reservations,
|
||||
room_pos,
|
||||
ft,
|
||||
now_ts,
|
||||
dynamic_ahead,
|
||||
est_back_times,
|
||||
poll_before,
|
||||
seek_reservation_room_callback,
|
||||
)
|
||||
|
||||
if ok:
|
||||
# 获取目标直播间名字和福袋详情(用于日志和校验)
|
||||
|
||||
@@ -105,6 +105,104 @@ def log_attend_window_detail_check(
|
||||
pass
|
||||
|
||||
|
||||
def execute_seek_reservation_with_logging(
|
||||
analyser: Any,
|
||||
target: ReservationItem,
|
||||
reservations: List[ReservationItem],
|
||||
room_pos: int,
|
||||
ft: float,
|
||||
now_ts: float,
|
||||
dynamic_ahead: int,
|
||||
est_back_times: int,
|
||||
poll_before: float,
|
||||
seek_reservation_room_callback: Callable[[ReservationItem], bool],
|
||||
):
|
||||
"""执行回退动作前的日志 + 调用 seek_reservation_room_callback。
|
||||
|
||||
流程:
|
||||
1. 写"进入预约参与窗口"日志
|
||||
2. 计算 current_room_pos / target_pos / pos_diff
|
||||
3. 写"准备执行回退到预约直播间"日志
|
||||
4. 调用 seek_reservation_room_callback(target)
|
||||
5. 异常时记录错误日志(log_reservation_error 或 _log_kv)
|
||||
|
||||
返回:
|
||||
(ok: bool, current_room_pos: int, target_pos: int, pos_diff: int)
|
||||
"""
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 进入预约参与窗口,准备回退到目标直播间",
|
||||
future_ts=int(ft),
|
||||
now_ts=int(now_ts),
|
||||
room_pos=int(room_pos),
|
||||
queue_len=len(reservations),
|
||||
dynamic_ahead=int(dynamic_ahead),
|
||||
est_back_times=int(est_back_times),
|
||||
remain_seconds_to_open=int(ft - now_ts),
|
||||
time_window_start=int(ft - dynamic_ahead),
|
||||
time_window_end=int(ft + max(0.0, poll_before)),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
current_room_pos = int(getattr(analyser, "room_pos", room_pos) or room_pos)
|
||||
except Exception:
|
||||
current_room_pos = int(room_pos)
|
||||
|
||||
try:
|
||||
target_pos = int(target.get("pos_seen") or 0)
|
||||
except Exception:
|
||||
target_pos = 0
|
||||
pos_diff = current_room_pos - target_pos
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 准备执行回退到预约直播间",
|
||||
target_pos_seen=target_pos,
|
||||
current_room_pos=current_room_pos,
|
||||
pos_diff=pos_diff,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ok = False
|
||||
try:
|
||||
ok = bool(seek_reservation_room_callback(target))
|
||||
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,
|
||||
current_room_pos=current_room_pos,
|
||||
target_pos=target_pos,
|
||||
pos_diff=pos_diff,
|
||||
),
|
||||
level="ERROR",
|
||||
stage="执行回退操作",
|
||||
)
|
||||
else:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"预约",
|
||||
"[预约] 执行回退时发生异常",
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
ok = False
|
||||
|
||||
return ok, current_room_pos, target_pos, pos_diff
|
||||
|
||||
|
||||
def handle_lock_period_same_reservation_check(
|
||||
analyser: Any,
|
||||
target: ReservationItem,
|
||||
|
||||
Reference in new issue
Block a user