perf(fulfillment): 修复 Outbox 状态版本去重、跨进程实时推送与数据库默认配置
This commit is contained in:
@@ -78,6 +78,8 @@ class SupplyInventory:
|
||||
covered_shortage: int
|
||||
available_shortage: int
|
||||
allocatable_shortage: int
|
||||
checked_at: datetime | None = None
|
||||
is_stale: bool = False
|
||||
|
||||
@property
|
||||
def needs_replenishment(self) -> bool:
|
||||
@@ -271,6 +273,8 @@ def _inventory_from_counts(
|
||||
covered: int,
|
||||
available: int,
|
||||
allocatable: int,
|
||||
checked_at: datetime | None = None,
|
||||
is_stale: bool = False,
|
||||
) -> SupplyInventory:
|
||||
return SupplyInventory(
|
||||
target_id=int(target.id),
|
||||
@@ -300,6 +304,8 @@ def _inventory_from_counts(
|
||||
+ int(target.safety_buffer or 0)
|
||||
- allocatable,
|
||||
),
|
||||
checked_at=checked_at,
|
||||
is_stale=is_stale,
|
||||
)
|
||||
|
||||
|
||||
@@ -461,6 +467,7 @@ def load_supply_snapshot_inventories(
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
refresh_if_stale: bool = True,
|
||||
allow_stale_snapshots: bool = False,
|
||||
) -> list[SupplyInventory]:
|
||||
"""优先读快照;缺失或超过有效期时回源一次并刷新快照。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
@@ -478,20 +485,33 @@ def load_supply_snapshot_inventories(
|
||||
.all()
|
||||
}
|
||||
fresh_after = moment - timedelta(seconds=supply_snapshot_max_age_seconds())
|
||||
if all(
|
||||
int(target.id) in snapshots
|
||||
and snapshots[int(target.id)].checked_at >= fresh_after
|
||||
for target in targets
|
||||
):
|
||||
return [
|
||||
_inventory_from_counts(
|
||||
target,
|
||||
covered=int(snapshots[int(target.id)].covered or 0),
|
||||
available=int(snapshots[int(target.id)].available or 0),
|
||||
allocatable=int(snapshots[int(target.id)].allocatable or 0),
|
||||
)
|
||||
all_present = all(int(target.id) in snapshots for target in targets)
|
||||
if all_present and (
|
||||
allow_stale_snapshots
|
||||
or all(
|
||||
snapshots[int(target.id)].checked_at >= fresh_after
|
||||
for target in targets
|
||||
]
|
||||
)
|
||||
):
|
||||
result: list[SupplyInventory] = []
|
||||
for target in targets:
|
||||
snap = snapshots[int(target.id)]
|
||||
snap_checked_at = (
|
||||
snap.checked_at
|
||||
if snap.checked_at.tzinfo is not None
|
||||
else snap.checked_at.replace(tzinfo=UTC)
|
||||
)
|
||||
result.append(
|
||||
_inventory_from_counts(
|
||||
target,
|
||||
covered=int(snap.covered or 0),
|
||||
available=int(snap.available or 0),
|
||||
allocatable=int(snap.allocatable or 0),
|
||||
checked_at=snap_checked_at,
|
||||
is_stale=snap_checked_at < fresh_after,
|
||||
)
|
||||
)
|
||||
return result
|
||||
if refresh_if_stale:
|
||||
return refresh_supply_snapshots(db, now=moment)
|
||||
return evaluate_supply_targets(db, now=moment)
|
||||
|
||||
Reference in New Issue
Block a user