perf(fulfillment): 修复 Outbox 状态版本去重、跨进程实时推送与数据库默认配置

This commit is contained in:
yml
2026-11-28 18:52:42 +08:00
parent f23ef42e67
commit d03eeaee18
22 changed files with 751 additions and 72 deletions
+48 -3
View File
@@ -22,7 +22,11 @@ from ..models import (
)
from .account_capability_service import AVAILABILITY_AVAILABLE, AVAILABILITY_LIMITED
from .account_protection_service import evaluate_protection_targets
from .account_supply_service import load_supply_snapshot_inventories
from .account_supply_service import (
load_supply_snapshot_inventories,
supply_snapshot_max_age_seconds,
)
from .external_order_source import get_cross_db_lookup_counts
TREND_DAYS = 7
@@ -268,7 +272,11 @@ def _supply_snapshot(
db: Session,
moment: datetime,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
inventories = load_supply_snapshot_inventories(db, now=moment)
inventories = load_supply_snapshot_inventories(
db,
now=moment,
allow_stale_snapshots=True,
)
supply_targets: list[dict[str, Any]] = []
alerts: list[dict[str, Any]] = []
for item in inventories:
@@ -294,6 +302,8 @@ def _supply_snapshot(
"available_shortage": item.available_shortage,
"allocatable_shortage": item.allocatable_shortage,
"needs_replenishment": item.needs_replenishment,
"checked_at": item.checked_at,
"is_stale": item.is_stale,
}
)
if not item.needs_replenishment:
@@ -347,6 +357,29 @@ def _supply_snapshot(
),
}
)
stale_checked_ats = [
item.checked_at
for item in inventories
if item.is_stale and item.checked_at is not None
]
if stale_checked_ats:
oldest_checked_at = min(stale_checked_ats)
age_minutes = max(
1, int((moment - oldest_checked_at).total_seconds() // 60)
)
max_age_minutes = max(1, supply_snapshot_max_age_seconds() // 60)
alerts.append(
{
"code": "supply_snapshot_stale",
"severity": "warning",
"stale_count": len(stale_checked_ats),
"oldest_checked_at": oldest_checked_at,
"message": (
f"账号供给快照已超过 {age_minutes} 分钟未刷新"
f"(阈值 {max_age_minutes} 分钟),当前展示缓存库存"
),
}
)
return supply_targets, alerts
@@ -366,10 +399,19 @@ def account_supply_snapshot(
)
.filter(AccountSupplyTarget.enabled.is_(True))
.scalar()
or moment
)
if checked_at is None:
target_checked = [
item["checked_at"]
for item in supply_targets
if item.get("checked_at") is not None
]
checked_at = max(target_checked) if target_checked else None
is_stale = any(bool(item.get("is_stale")) for item in supply_targets)
return {
"checked_at": checked_at,
"is_stale": is_stale,
"max_age_seconds": supply_snapshot_max_age_seconds(),
"supply_targets": supply_targets,
"alerts": alerts,
}
@@ -653,6 +695,9 @@ def account_platform_status(
"supply_targets": supply_targets,
"alerts": alerts,
"trends": trends,
"cross_db_lookups": (
get_cross_db_lookup_counts() if include_diagnostics else None
),
}