refactor: 重构为可部署到服务器的模块化结构(v5.0.0)

- 拆包 fengxiang/:config/client/store/notify/monitor/admin/tts,统一入口 order_monitor.py
- 服务器形态:Docker 常驻,Cookie 由本机登录后 POST 上传并热加载,不再依赖桌面弹窗
- 修复:首次启动重推历史订单、speak() 阻塞主循环、夜间 sleep 10h 不可中断
- 修复:test_push.py 残留 miservice 依赖(小爱已移除)、run_monitor.bat 写死盘符
- 配置化:webhook/shopId 移出源码(环境变量 > config.yaml > 默认值)
- 可观测:滚动文件日志 + /healthz + Docker HEALTHCHECK + 告警冷却
- 飞书通道保留配置位,默认关闭(原 token 已失效 code=19001)
- 新增 22 项单元测试(不联网)
This commit is contained in:
houhuan committed 2026-11-16 12:45:23 +08:00
1 parent ab6a2bb7ea
commit 4ee30399bf
30 files changed
+2410 -825

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
"""运行状态持久化。
解决原项目两个真实问题:
1. 进程重启后 last_order_id 丢失 → 靠「取当前最新」兜底,但拉单失败时会重推历史订单
2. 一次拉取超过 page_size 时,仅靠 orderId 断点会漏单
因此这里同时保存 last_order_id 和最近推送过的订单号集合(去重兜底)。
写入采用 tmp + os.replace 原子替换,避免断电/被杀留下半个 JSON。
"""
from __future__ import annotations
import json
import logging
import os
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional
log = logging.getLogger("fengxiang.store")
MAX_PUSHED_IDS = 500
@dataclass
class State:
last_order_id: Optional[str] = None
pushed_ids: List[str] = field(default_factory=list)
last_poll_ts: float = 0.0
cookie_updated_ts: float = 0.0
cookie_valid: bool = False
gap_since_ts: float = 0.0 # 最近一次进入「会话失效」的时刻
total_pushed: int = 0
# 运行时字段,不落盘
_path: Optional[Path] = field(default=None, repr=False, compare=False)
# ---------------- 持久化 ----------------
@classmethod
def load(cls, path: Path) -> "State":
state = cls(_path=Path(path))
if Path(path).exists():
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
state.last_order_id = str(data["last_order_id"]) if data.get("last_order_id") else None
state.pushed_ids = [str(i) for i in data.get("pushed_ids", [])]
state.last_poll_ts = float(data.get("last_poll_ts") or 0)
state.cookie_updated_ts = float(data.get("cookie_updated_ts") or 0)
state.cookie_valid = bool(data.get("cookie_valid"))
state.gap_since_ts = float(data.get("gap_since_ts") or 0)
state.total_pushed = int(data.get("total_pushed") or 0)
log.info("已加载状态: last_order_id=%s 去重缓存=%d 条",
state.last_order_id, len(state.pushed_ids))
except (OSError, ValueError, KeyError, TypeError) as exc:
log.warning("状态文件损坏,按全新状态启动: %s", exc)
return state
def to_dict(self) -> Dict[str, Any]:
return {
"last_order_id": self.last_order_id,
"pushed_ids": self.pushed_ids[-MAX_PUSHED_IDS:],
"last_poll_ts": self.last_poll_ts,
"cookie_updated_ts": self.cookie_updated_ts,
"cookie_valid": self.cookie_valid,
"gap_since_ts": self.gap_since_ts,
"total_pushed": self.total_pushed,
}
def save(self, path: Optional[Path] = None) -> None:
target = Path(path or self._path or "state.json")
target.parent.mkdir(parents=True, exist_ok=True)
tmp = target.with_suffix(target.suffix + ".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(self.to_dict(), f, ensure_ascii=False, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, target)
# ---------------- 业务方法 ----------------
def has_pushed(self, order_id: str) -> bool:
return str(order_id) in self.pushed_ids
def mark_pushed(self, order_ids: Iterable[str]) -> None:
ids = [str(i) for i in order_ids]
self.pushed_ids.extend(ids)
self.total_pushed += len(ids)
if len(self.pushed_ids) > MAX_PUSHED_IDS:
self.pushed_ids = self.pushed_ids[-MAX_PUSHED_IDS:]
def touch_poll(self) -> None:
self.last_poll_ts = time.time()
def atomic_write_json(path: Path, data: Any) -> None:
"""供管理接口写 cookies 复用。"""
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, path)