Prevent stale run from restoring room map
This commit is contained in:
1 parent
f129206df7
commit
0f1baac92f
2 files changed
+92
No files matched your search
@@ -48,6 +48,8 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
|
||||
os.makedirs(state_dir, exist_ok=True)
|
||||
final_path = os.path.join(state_dir, "room_pos_map.json")
|
||||
tmp_path = final_path + ".tmp"
|
||||
if _is_stale_run_overwriting_startup_clear(analyser, final_path):
|
||||
return
|
||||
|
||||
# 先按直播间标题归一化做一次合并(防止重复直播间散落在不同位置)
|
||||
# 保留原则:优先保留 room_pos 较小(更靠前)的一条;如果其后有 has_fudai=True 的记录则补齐信息。
|
||||
@@ -275,3 +277,49 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
|
||||
analyser._log_kv("预约", "[预约] 序列化映射表到文件失败")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _is_stale_run_overwriting_startup_clear(analyser: Any, final_path: str) -> bool:
|
||||
"""新运行已清空映射表后,旧运行实例不得再把旧内存映射写回。"""
|
||||
try:
|
||||
if not os.path.exists(final_path):
|
||||
return False
|
||||
with open(final_path, "r", encoding="utf-8") as f:
|
||||
existing = json.load(f)
|
||||
if not isinstance(existing, dict):
|
||||
return False
|
||||
meta = existing.get("_meta")
|
||||
if not isinstance(meta, dict):
|
||||
return False
|
||||
if str(meta.get("reason") or "") != "startup_clear":
|
||||
return False
|
||||
existing_run_id = str(meta.get("run_id") or "")
|
||||
current_run_id = str(getattr(analyser, "_run_id", "") or "")
|
||||
if not existing_run_id or not current_run_id or existing_run_id == current_run_id:
|
||||
return False
|
||||
try:
|
||||
generated_ts = float(meta.get("generated_ts") or 0.0)
|
||||
except Exception:
|
||||
generated_ts = 0.0
|
||||
try:
|
||||
run_start_ts = float(getattr(analyser, "_run_start_ts", 0.0) or 0.0)
|
||||
except Exception:
|
||||
run_start_ts = 0.0
|
||||
if run_start_ts > 0 and generated_ts >= run_start_ts:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"映射表",
|
||||
"跳过旧运行映射表写入,避免覆盖新启动清空状态",
|
||||
current_run_id=current_run_id,
|
||||
existing_run_id=existing_run_id,
|
||||
generated_ts=int(generated_ts),
|
||||
run_start_ts=int(run_start_ts),
|
||||
decision="skip_stale_room_pos_map_dump",
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
return False
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
@@ -74,6 +75,49 @@ def test_merge_room_info_keeps_explicit_no_fudai_state():
|
||||
assert merged["fudai_detail"] is None
|
||||
|
||||
|
||||
def test_old_run_does_not_overwrite_startup_cleared_room_pos_map(tmp_path, monkeypatch):
|
||||
from douyin.core import path_config
|
||||
from douyin.reservation.douyin_reservation_handler_pkg.dump_state import (
|
||||
_dump_room_pos_map_to_state,
|
||||
)
|
||||
|
||||
state_dir = tmp_path / "douyin" / "core" / "state"
|
||||
state_dir.mkdir(parents=True)
|
||||
room_map_path = state_dir / "room_pos_map.json"
|
||||
room_map_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"_meta": {
|
||||
"reason": "startup_clear",
|
||||
"run_id": "new_run",
|
||||
"generated_ts": 200,
|
||||
}
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setattr(path_config, "PROJECT_ROOT", str(tmp_path))
|
||||
|
||||
analyser = _mk_analyser()
|
||||
analyser._run_id = "old_run"
|
||||
analyser._run_start_ts = 100.0
|
||||
analyser._room_pos_to_title_map = {
|
||||
1: {"title": "旧直播间", "title_raw": "旧直播间", "has_fudai": True}
|
||||
}
|
||||
|
||||
_dump_room_pos_map_to_state(analyser)
|
||||
|
||||
room_map = json.loads(room_map_path.read_text(encoding="utf-8"))
|
||||
assert room_map == {
|
||||
"_meta": {
|
||||
"reason": "startup_clear",
|
||||
"run_id": "new_run",
|
||||
"generated_ts": 200,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_same_room_streak_triggers_reset_and_consumes_swipe():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
|
||||
|
||||
|
||||
Reference in new issue
Block a user