- 实测订单接口平均 300ms,2.5 次/秒连续 20 次无限流,故营业时段可压到 2 秒 - 商家后台 JS 无 WebSocket/SSE/socket.io/MQTT(0 命中)→ 平台不推,只能轮询 - 出单后 60 秒内自动加密到 1.5 秒(出单常成串),窗口结束回落 - 新增 last_detect_lag_sec:以订单 payTime 计算「支付→检出」真实延迟,写入 state 并暴露在 /healthz - 新增 6 项节奏测试(共 30 项全绿) - 修复 login_url 折行产生空格、上传内网绕过系统代理、新增 --upload-only 补传
109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
"""运行状态持久化。
|
|
|
|
解决原项目两个真实问题:
|
|
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
|
|
last_detect_lag_sec: Optional[float] = None # 最近一单:支付→检出 的端到端延迟
|
|
# 运行时字段,不落盘
|
|
_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)
|
|
lag = data.get("last_detect_lag_sec")
|
|
state.last_detect_lag_sec = float(lag) if lag is not None else None
|
|
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,
|
|
"last_detect_lag_sec": self.last_detect_lag_sec,
|
|
}
|
|
|
|
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)
|