- douyin/stage/douyin_stage.py:阶段诊断起始处的 [DEBUG] print 改为 _LOGGER.debug,原本是诊断日志,走 logging 更合适 - douyin/reservation/douyin_reservation.py:当 _RESERVATION_ERROR_UTILS 不可用时的 print 兜底改为 _LOGGER.error,避免日志静默丢失 - 两处都新增模块级 logger 回归:140 .py 编译通过;test_filter_logging / check_web_gui_config_sync 通过。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
268 lines
8.9 KiB
Python
268 lines
8.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
import time
|
|
import threading
|
|
from datetime import datetime
|
|
from typing import Any, Optional
|
|
|
|
# 模块级 logger:原代码 _RESERVATION_ERROR_UTILS 不可用时用 print 兜底,
|
|
# 改走 logging,避免日志静默丢失。
|
|
_LOGGER = logging.getLogger("douyin.reservation.douyin_reservation")
|
|
|
|
# 文件级锁字典,用于保护 JSON 文件的并发读写
|
|
_json_file_locks: dict[str, threading.Lock] = {}
|
|
_json_locks_lock = threading.Lock()
|
|
|
|
|
|
def _get_file_lock(path: str) -> threading.Lock:
|
|
"""获取文件级别的锁"""
|
|
with _json_locks_lock:
|
|
if path not in _json_file_locks:
|
|
_json_file_locks[path] = threading.Lock()
|
|
return _json_file_locks[path]
|
|
|
|
|
|
# 导入预约错误处理工具
|
|
try:
|
|
from douyin.reservation.core.reservation_error_utils import (
|
|
log_reservation_error,
|
|
format_reservation_context,
|
|
)
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE = True
|
|
except ImportError:
|
|
# 如果导入失败,提供占位符函数
|
|
def log_reservation_error(*args, **kwargs):
|
|
pass
|
|
|
|
def format_reservation_context(*args, **kwargs):
|
|
return {}
|
|
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE = False
|
|
|
|
|
|
def _write_json_atomic(path: str, data: object, *, indent: int = 2, analyser: Optional[Any] = None) -> None:
|
|
"""原子写入 JSON 文件,带详细的错误处理和线程锁保护。
|
|
|
|
参数:
|
|
path: 文件路径
|
|
data: 要写入的数据
|
|
indent: JSON 缩进
|
|
analyser: 分析器对象(可选,用于日志记录)
|
|
"""
|
|
lock = _get_file_lock(path)
|
|
with lock:
|
|
try:
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
except PermissionError as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="创建状态文件目录(权限错误)",
|
|
)
|
|
raise
|
|
except OSError as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="创建状态文件目录(系统错误)",
|
|
)
|
|
raise
|
|
|
|
tmp = path + ".tmp"
|
|
try:
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=indent)
|
|
f.write("\n")
|
|
except (ValueError, TypeError) as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="序列化状态数据",
|
|
)
|
|
raise
|
|
except PermissionError as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="写入状态文件(权限错误)",
|
|
)
|
|
raise
|
|
except OSError as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="写入状态文件(系统错误)",
|
|
)
|
|
raise
|
|
|
|
try:
|
|
os.replace(tmp, path)
|
|
except OSError as e:
|
|
# 清理临时文件
|
|
try:
|
|
if os.path.exists(tmp):
|
|
os.remove(tmp)
|
|
except Exception:
|
|
pass
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(path=path),
|
|
level="ERROR",
|
|
stage="原子替换状态文件",
|
|
)
|
|
raise
|
|
|
|
|
|
def write_reservations_state(
|
|
*,
|
|
state_path: str,
|
|
reservations: list[dict],
|
|
room_pos: int,
|
|
current_room_title: str,
|
|
current_room_title_raw: str,
|
|
reason: str = "",
|
|
analyser: Optional[Any] = None,
|
|
) -> None:
|
|
"""寫入預約隊列狀態(跨進程可讀)。
|
|
|
|
参数:
|
|
state_path: 状态文件路径
|
|
reservations: 预约队列列表
|
|
room_pos: 当前房间位置
|
|
current_room_title: 当前房间标题
|
|
current_room_title_raw: 当前房间原始标题
|
|
reason: 写入原因
|
|
analyser: 分析器对象(可选,用于日志记录)
|
|
"""
|
|
try:
|
|
items: list[dict] = []
|
|
for r in reservations or []:
|
|
try:
|
|
ft = float(r.get("future_ts") or 0)
|
|
except Exception:
|
|
ft = 0.0
|
|
try:
|
|
ft_str = datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") if ft else ""
|
|
except Exception:
|
|
ft_str = ""
|
|
items.append(
|
|
{
|
|
"prize_value_yuan": float(r.get("prize_value_yuan") or 0),
|
|
"content": str(r.get("content") or ""),
|
|
"future_ts": ft,
|
|
"future_str": ft_str,
|
|
"pos_seen": int(r.get("pos_seen") or 0),
|
|
"room_title": str(r.get("room_title") or ""),
|
|
"room_title_raw": str(r.get("room_title_raw") or ""),
|
|
"task_count": int(r.get("task_count") or 2),
|
|
"time_text": str(r.get("time_text") or ""),
|
|
"participants": int(r.get("participants") or 0),
|
|
"copies": int(r.get("copies") or 0),
|
|
}
|
|
)
|
|
|
|
data = {
|
|
"updated_ts": time.time(),
|
|
"reason": str(reason or ""),
|
|
"room_pos": int(room_pos),
|
|
"current_room_title": str(current_room_title or ""),
|
|
"current_room_title_raw": str(current_room_title_raw or ""),
|
|
"reservations": items,
|
|
}
|
|
_write_json_atomic(state_path, data, indent=2, analyser=analyser)
|
|
except Exception as e:
|
|
if _RESERVATION_ERROR_UTILS_AVAILABLE and analyser:
|
|
log_reservation_error(
|
|
analyser,
|
|
e,
|
|
format_reservation_context(
|
|
reservations=reservations,
|
|
state_path=state_path,
|
|
reason=reason,
|
|
),
|
|
level="ERROR",
|
|
stage="写入预约队列状态",
|
|
)
|
|
else:
|
|
try:
|
|
_LOGGER.error(
|
|
"[预约队列] 写入状态文件失败: %s: %s",
|
|
type(e).__name__, e,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def load_high_value_rooms_map(path: str) -> dict[str, dict]:
|
|
"""讀取高價值直播間映射(跨運行持久化),带线程锁保护。"""
|
|
lock = _get_file_lock(path)
|
|
with lock:
|
|
try:
|
|
if not os.path.exists(path):
|
|
return {}
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
# 兼容兩種格式:{"rooms": [...]} 或 直接是 [...]
|
|
rooms = None
|
|
if isinstance(data, dict):
|
|
rooms = data.get("rooms")
|
|
elif isinstance(data, list):
|
|
rooms = data
|
|
|
|
out: dict[str, dict] = {}
|
|
if isinstance(rooms, list):
|
|
for it in rooms:
|
|
if not isinstance(it, dict):
|
|
continue
|
|
key = str(it.get("room_title") or "").strip()
|
|
if not key:
|
|
continue
|
|
out[key] = dict(it)
|
|
return out
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def write_high_value_rooms_state(path: str, rooms_map: dict, *, reason: str = "") -> None:
|
|
"""寫入高價值直播間映射(跨運行持久化)。"""
|
|
try:
|
|
rooms: list[dict] = []
|
|
for k, v in (rooms_map or {}).items():
|
|
if not k:
|
|
continue
|
|
item = dict(v or {})
|
|
item["room_title"] = str(k)
|
|
rooms.append(item)
|
|
|
|
rooms.sort(key=lambda x: float(x.get("last_seen_ts") or 0.0), reverse=True)
|
|
data = {
|
|
"updated_ts": time.time(),
|
|
"reason": str(reason or ""),
|
|
"total": int(len(rooms)),
|
|
"rooms": rooms,
|
|
}
|
|
_write_json_atomic(path, data, indent=2)
|
|
except Exception:
|
|
pass
|