把 douyin/reservation/douyin_reservation_logic.py 按职责拆成 douyin/reservation/reservation_logic/ 子包: - _common (57) 共享错误处理工具占位 + ReservationItem 类型 - helpers (130) 内部工具:标题归一化 / 相似度 / 排序 / 时间戳 - add (447) add_reservation_core_logic 主入口(添加新预约) - cleanup_validate (136) validate_back_to_reservation / cleanup_expired_reservations - queue_full (305) handle_queue_full_wait_at_earliest 队列满时等待策略 - attend_window (1437) process_reservation_attend_window 进入预约窗口(最大头) - record (328) record_high_value_reservation 高价值预约登记入口 原文件改为 53 行 re-export shim,17 个公共符号全部保留。 - process_reservation_attend_window 7 处外部引用 - handle_queue_full_wait_at_earliest 5 处外部引用 - _now_ts 5 处 / cleanup_expired_reservations 4 处 / _sort_reservations_inplace 4 处 - 其他 5 处以下 全部不需修改。 回归: - 8 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 - _now_ts / _normalize_room_title / _calculate_string_similarity 等纯函数 调用结果与拆分前一致 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
"""reservation_logic.cleanup_validate - 校验与过期清理。
|
|
|
|
- validate_back_to_reservation 回退后校验是否进入了正确的直播间
|
|
- cleanup_expired_reservations 清理已过开奖时间的预约
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
from ._common import (
|
|
ReservationItem,
|
|
format_reservation_context,
|
|
log_reservation_error,
|
|
safe_write_state,
|
|
)
|
|
from .helpers import (
|
|
_calculate_string_similarity,
|
|
_normalize_room_title_for_matching,
|
|
_now_ts,
|
|
_sort_reservations_inplace,
|
|
)
|
|
|
|
|
|
def validate_back_to_reservation(
|
|
analyser: Any,
|
|
reservations: List[ReservationItem],
|
|
now_ts: float | None = None,
|
|
) -> None:
|
|
"""回退校验:当前实现为轻量版本,仅记录日志,保留扩展点。"""
|
|
if now_ts is None:
|
|
now_ts = _now_ts()
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] validate_back_to_reservation",
|
|
log_type="OP",
|
|
count=len(reservations),
|
|
now_ts=int(now_ts),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
|
|
|
|
def cleanup_expired_reservations(
|
|
analyser: Any,
|
|
reservations: List[ReservationItem],
|
|
write_state_callback: Optional[Callable[[], None]] = None,
|
|
) -> None:
|
|
"""清理已过开奖时间的预约,并视情况写入状态文件。"""
|
|
start_time = time.time()
|
|
try:
|
|
now_ts_drop = _now_ts()
|
|
before = len(reservations)
|
|
|
|
# 记录被清理的预约详情
|
|
dropped_items_detail = []
|
|
for r in reservations:
|
|
try:
|
|
r_ft = float(r.get("future_ts") or 0.0)
|
|
if r_ft <= now_ts_drop:
|
|
dropped_items_detail.append(
|
|
{
|
|
"future_ts": int(r_ft),
|
|
"prize_value_yuan": float(r.get("prize_value_yuan", 0)),
|
|
"content": str(r.get("content", ""))[:50],
|
|
"room_title": str(r.get("room_title", ""))[:30],
|
|
}
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
reservations[:] = [
|
|
r for r in reservations if float(r.get("future_ts") or 0.0) > now_ts_drop
|
|
]
|
|
dropped = max(0, before - len(reservations))
|
|
|
|
if dropped:
|
|
try:
|
|
from datetime import datetime
|
|
|
|
now_str = datetime.fromtimestamp(now_ts_drop).strftime(
|
|
"%Y-%m-%d %H:%M:%S"
|
|
)
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] cleanup_expired_reservations:清理过期预约",
|
|
dropped_count=dropped,
|
|
remain_count=len(reservations),
|
|
now_ts=int(now_ts_drop),
|
|
now_str=now_str,
|
|
dropped_items=dropped_items_detail[:5], # 只记录前5个,避免日志过长
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
if write_state_callback is not None:
|
|
try:
|
|
write_state_callback()
|
|
except Exception:
|
|
pass
|
|
else:
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] cleanup_expired_reservations:无需清理",
|
|
queue_len=len(reservations),
|
|
now_ts=int(now_ts_drop),
|
|
)
|
|
except Exception:
|
|
pass
|
|
except Exception as e:
|
|
# Best-effort cleanup only; never crash主循环
|
|
try:
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] cleanup_expired_reservations 异常",
|
|
error=str(e),
|
|
error_type=type(e).__name__,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
try:
|
|
elapsed = time.time() - start_time
|
|
analyser._log_kv(
|
|
"预约",
|
|
"[预约] cleanup_expired_reservations 完成",
|
|
elapsed_seconds=round(elapsed, 3),
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|