优化(直播履约): 即时选号并收敛订单快照同步
This commit is contained in:
@@ -257,43 +257,98 @@ def list_pending_manual_fulfillments(
|
||||
)
|
||||
if not sku_keys:
|
||||
return []
|
||||
result: list[dict[str, Any]] = []
|
||||
for row in _fetch_pending_manual_fulfillments(
|
||||
limit=limit,
|
||||
internal_sku_keys=sku_keys,
|
||||
paid_at_from=paid_at_from,
|
||||
):
|
||||
internal_sku_key = str(
|
||||
row.get("manual_sku_key")
|
||||
or row.get("sku_name")
|
||||
or row.get("sku_code")
|
||||
or ""
|
||||
).strip()
|
||||
result.append(
|
||||
{
|
||||
"source_system": PLATFORM_FULFILLMENT_SOURCE,
|
||||
"source_order_id": str(row.get("source_order_id") or ""),
|
||||
"source_order_item_id": str(row.get("source_order_item_id") or ""),
|
||||
"source_fulfillment_id": str(row.get("source_fulfillment_id") or ""),
|
||||
"platform_order_id": str(row.get("platform_order_id") or ""),
|
||||
"provider": str(row.get("provider") or ""),
|
||||
"platform": str(row.get("platform") or ""),
|
||||
"shop_id": str(row.get("shop_id") or ""),
|
||||
"shop_name": str(row.get("shop_name") or ""),
|
||||
"order_status": str(row.get("order_status") or ""),
|
||||
"pay_status": str(row.get("pay_status") or ""),
|
||||
"total_amount": int(row.get("total_amount") or 0),
|
||||
"revenue_amount_fen": int(row.get("revenue_amount_fen") or 0),
|
||||
"paid_at": row.get("paid_at"),
|
||||
"sku_code": str(row.get("sku_code") or ""),
|
||||
"sku_name": str(row.get("sku_name") or ""),
|
||||
"internal_sku_key": internal_sku_key,
|
||||
"quantity": int(row.get("quantity") or 1),
|
||||
"spec_json": row.get("spec_json"),
|
||||
"item_updated_at": row.get("item_updated_at"),
|
||||
}
|
||||
return [
|
||||
_normalize_pending_row(row)
|
||||
for row in _fetch_pending_manual_fulfillments(
|
||||
limit=limit,
|
||||
internal_sku_keys=sku_keys,
|
||||
paid_at_from=paid_at_from,
|
||||
)
|
||||
return result
|
||||
]
|
||||
|
||||
|
||||
def _normalize_pending_row(row: dict[str, Any]) -> dict[str, Any]:
|
||||
internal_sku_key = str(
|
||||
row.get("manual_sku_key") or row.get("sku_name") or row.get("sku_code") or ""
|
||||
).strip()
|
||||
return {
|
||||
"source_system": PLATFORM_FULFILLMENT_SOURCE,
|
||||
"source_order_id": str(row.get("source_order_id") or ""),
|
||||
"source_order_item_id": str(row.get("source_order_item_id") or ""),
|
||||
"source_fulfillment_id": str(row.get("source_fulfillment_id") or ""),
|
||||
"platform_order_id": str(row.get("platform_order_id") or ""),
|
||||
"provider": str(row.get("provider") or ""),
|
||||
"platform": str(row.get("platform") or ""),
|
||||
"shop_id": str(row.get("shop_id") or ""),
|
||||
"shop_name": str(row.get("shop_name") or ""),
|
||||
"order_status": str(row.get("order_status") or ""),
|
||||
"pay_status": str(row.get("pay_status") or ""),
|
||||
"total_amount": int(row.get("total_amount") or 0),
|
||||
"revenue_amount_fen": int(row.get("revenue_amount_fen") or 0),
|
||||
"paid_at": row.get("paid_at"),
|
||||
"sku_code": str(row.get("sku_code") or ""),
|
||||
"sku_name": str(row.get("sku_name") or ""),
|
||||
"internal_sku_key": internal_sku_key,
|
||||
"quantity": int(row.get("quantity") or 1),
|
||||
"spec_json": row.get("spec_json"),
|
||||
"item_updated_at": row.get("item_updated_at"),
|
||||
}
|
||||
|
||||
|
||||
def lookup_pending_manual_fulfillment(
|
||||
source_order_item_id: str,
|
||||
) -> dict[str, Any] | None:
|
||||
"""按订单项定向读取旧版事件缺失的快照,不再拉取最近 500 条。"""
|
||||
item_id = str(source_order_item_id or "").strip()
|
||||
if not item_id:
|
||||
return None
|
||||
if not _enabled():
|
||||
raise OrderLookupUnavailable("外部订单查询未启用")
|
||||
|
||||
sql = text(
|
||||
"""
|
||||
SELECT o.id AS source_order_id,
|
||||
o.platform_order_id,
|
||||
o.provider,
|
||||
o.platform,
|
||||
o.shop_id,
|
||||
o.shop_name,
|
||||
o.order_status,
|
||||
o.pay_status,
|
||||
o.total_amount,
|
||||
o.paid_at,
|
||||
o.updated_at AS order_updated_at,
|
||||
oi.id AS source_order_item_id,
|
||||
oi.sku_code,
|
||||
oi.sku_name,
|
||||
oi.quantity,
|
||||
oi.revenue_amount_fen,
|
||||
oi.manual_sku_key,
|
||||
oi.spec_json,
|
||||
oi.updated_at AS item_updated_at,
|
||||
f.id AS source_fulfillment_id,
|
||||
f.channel AS fulfillment_channel,
|
||||
f.status AS fulfillment_status,
|
||||
f.issue_code AS fulfillment_issue_code
|
||||
FROM orders o
|
||||
JOIN order_items oi ON oi.order_id = o.id
|
||||
JOIN fulfillments f ON f.order_item_id = oi.id
|
||||
WHERE oi.id = :source_order_item_id
|
||||
AND f.channel = 'manual'
|
||||
LIMIT 1
|
||||
"""
|
||||
)
|
||||
try:
|
||||
with _get_engine().connect() as conn:
|
||||
row = (
|
||||
conn.execute(sql, {"source_order_item_id": int(item_id)})
|
||||
.mappings()
|
||||
.first()
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning(f"定向查询 order_site 订单项失败({item_id}):{exc}")
|
||||
raise
|
||||
return _normalize_pending_row(dict(row)) if row is not None else None
|
||||
|
||||
|
||||
def _route_of(rows: list[dict[str, Any]]) -> str:
|
||||
|
||||
@@ -4,10 +4,12 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import threading
|
||||
from collections.abc import Iterable
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...database import SessionLocal
|
||||
@@ -21,6 +23,9 @@ DEFAULT_RETRY_SECONDS = 45
|
||||
_stop_event = threading.Event()
|
||||
_thread: threading.Thread | None = None
|
||||
_lock = threading.Lock()
|
||||
_pending_lock = threading.Lock()
|
||||
_pending_task_ids: set[int] = set()
|
||||
_wake_event = threading.Event()
|
||||
|
||||
|
||||
def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
||||
@@ -31,13 +36,50 @@ def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
||||
return max(minimum, min(maximum, value))
|
||||
|
||||
|
||||
def wake_auto_allocator(task_ids: Iterable[int] | None = None) -> None:
|
||||
"""立即唤醒选号 worker;传入任务 ID 时优先处理这些新任务。"""
|
||||
normalized = {int(task_id) for task_id in (task_ids or []) if int(task_id) > 0}
|
||||
if normalized:
|
||||
with _pending_lock:
|
||||
_pending_task_ids.update(normalized)
|
||||
_wake_event.set()
|
||||
|
||||
|
||||
def _take_pending_task_ids(limit: int) -> list[int]:
|
||||
with _pending_lock:
|
||||
selected = sorted(_pending_task_ids)[:limit]
|
||||
for task_id in selected:
|
||||
_pending_task_ids.discard(task_id)
|
||||
return selected
|
||||
|
||||
|
||||
def _has_pending_task_ids() -> bool:
|
||||
with _pending_lock:
|
||||
return bool(_pending_task_ids)
|
||||
|
||||
|
||||
def _schedule_retry(
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
moment: datetime,
|
||||
retry_seconds: int,
|
||||
) -> None:
|
||||
task = db.get(PlatformFulfillmentTask, int(task_id))
|
||||
if task is None or task.execution_status in {"completed", "cancelled"}:
|
||||
return
|
||||
task.next_allocate_at = moment + timedelta(seconds=retry_seconds)
|
||||
db.commit()
|
||||
|
||||
|
||||
def run_auto_allocation_once(
|
||||
db: Session,
|
||||
*,
|
||||
limit: int | None = None,
|
||||
now: datetime | None = None,
|
||||
task_ids: list[int] | None = None,
|
||||
) -> dict[str, int]:
|
||||
"""扫描 ready 且尚未分配账号的任务,逐笔调用统一选号逻辑。"""
|
||||
"""领取已到期的 ready 任务;新任务不再等待固定 45 秒。"""
|
||||
moment = now or datetime.now(UTC)
|
||||
batch_size = limit or _env_int(
|
||||
"PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH",
|
||||
@@ -51,18 +93,34 @@ def run_auto_allocation_once(
|
||||
10,
|
||||
3600,
|
||||
)
|
||||
# MySQL DATETIME 默认只保存到秒,写入时会按微秒四舍五入;
|
||||
# 给 1 秒容差,避免刚写入的 next_allocate_at 被 worker 当成“未来任务”。
|
||||
due_cutoff = moment + timedelta(seconds=1)
|
||||
query = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.execution_status == "ready",
|
||||
PlatformFulfillmentTask.account_id.is_(None),
|
||||
PlatformFulfillmentTask.commodity_id != "",
|
||||
PlatformFulfillmentTask.updated_at <= moment - timedelta(seconds=retry_seconds),
|
||||
or_(
|
||||
PlatformFulfillmentTask.next_allocate_at.is_(None),
|
||||
PlatformFulfillmentTask.next_allocate_at <= due_cutoff,
|
||||
),
|
||||
)
|
||||
normalized_ids = [
|
||||
int(task_id) for task_id in dict.fromkeys(task_ids or []) if int(task_id) > 0
|
||||
]
|
||||
if normalized_ids:
|
||||
query = query.filter(PlatformFulfillmentTask.id.in_(normalized_ids))
|
||||
query = service._apply_order_match_cutoff(
|
||||
query,
|
||||
service._order_match_started_at(db),
|
||||
)
|
||||
candidates = (
|
||||
query.order_by(PlatformFulfillmentTask.updated_at.asc()).limit(batch_size).all()
|
||||
query.order_by(
|
||||
PlatformFulfillmentTask.next_allocate_at.asc(),
|
||||
PlatformFulfillmentTask.id.asc(),
|
||||
)
|
||||
.limit(batch_size)
|
||||
.all()
|
||||
)
|
||||
if not candidates:
|
||||
return {"checked": 0, "allocated": 0, "failed": 0}
|
||||
@@ -71,20 +129,41 @@ def run_auto_allocation_once(
|
||||
allocated = 0
|
||||
failed = 0
|
||||
for task in candidates:
|
||||
candidate_task_id = int(task.id)
|
||||
try:
|
||||
detail: dict[str, Any] = service.allocate_task(
|
||||
db,
|
||||
task_id=int(task.id),
|
||||
task_id=candidate_task_id,
|
||||
current=actor,
|
||||
)
|
||||
if detail.get("task", {}).get("account"):
|
||||
allocated += 1
|
||||
allocated_task = db.get(PlatformFulfillmentTask, candidate_task_id)
|
||||
if allocated_task is not None:
|
||||
allocated_task.next_allocate_at = None
|
||||
db.commit()
|
||||
else:
|
||||
failed += 1
|
||||
_schedule_retry(
|
||||
db,
|
||||
task_id=candidate_task_id,
|
||||
moment=moment,
|
||||
retry_seconds=retry_seconds,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 单个任务异常不能中断整批
|
||||
db.rollback()
|
||||
failed += 1
|
||||
logger.warning("直播账号履约自动选号失败 task_id={}: {}", task.id, exc)
|
||||
_schedule_retry(
|
||||
db,
|
||||
task_id=candidate_task_id,
|
||||
moment=moment,
|
||||
retry_seconds=retry_seconds,
|
||||
)
|
||||
logger.warning(
|
||||
"直播账号履约自动选号失败 task_id={}: {}",
|
||||
candidate_task_id,
|
||||
exc,
|
||||
)
|
||||
return {
|
||||
"checked": len(candidates),
|
||||
"allocated": allocated,
|
||||
@@ -99,10 +178,18 @@ def _run() -> None:
|
||||
5,
|
||||
3600,
|
||||
)
|
||||
while not _stop_event.wait(interval):
|
||||
while not _stop_event.is_set():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
result = run_auto_allocation_once(db)
|
||||
pending_ids = _take_pending_task_ids(
|
||||
_env_int(
|
||||
"PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH",
|
||||
DEFAULT_BATCH_SIZE,
|
||||
1,
|
||||
20,
|
||||
)
|
||||
)
|
||||
result = run_auto_allocation_once(db, task_ids=pending_ids or None)
|
||||
if result["checked"]:
|
||||
logger.info("直播账号履约自动选号: {}", result)
|
||||
except Exception as exc: # noqa: BLE001 - worker 不能因单轮异常退出
|
||||
@@ -110,6 +197,12 @@ def _run() -> None:
|
||||
logger.warning("直播账号履约自动选号轮次失败: {}", exc)
|
||||
finally:
|
||||
db.close()
|
||||
if _stop_event.is_set():
|
||||
break
|
||||
if _has_pending_task_ids():
|
||||
continue
|
||||
_wake_event.wait(interval)
|
||||
_wake_event.clear()
|
||||
|
||||
|
||||
def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
@@ -118,6 +211,9 @@ def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
if _thread is not None and _thread.is_alive():
|
||||
return False
|
||||
_stop_event.clear()
|
||||
_wake_event.clear()
|
||||
with _pending_lock:
|
||||
_pending_task_ids.clear()
|
||||
_thread = threading.Thread(
|
||||
target=_run,
|
||||
name="platform-fulfillment-auto-allocator",
|
||||
@@ -130,6 +226,7 @@ def start_platform_fulfillment_auto_allocator() -> bool:
|
||||
def stop_platform_fulfillment_auto_allocator() -> None:
|
||||
global _thread
|
||||
_stop_event.set()
|
||||
_wake_event.set()
|
||||
with _lock:
|
||||
thread = _thread
|
||||
_thread = None
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""低频订单对账兜底;正常同步由 order_site 签名 Webhook 驱动。"""
|
||||
"""低频状态对账兜底;正常订单同步完全由 order_site 事务 Outbox 驱动。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -12,6 +12,7 @@ from ...database import SessionLocal
|
||||
_stop_event = threading.Event()
|
||||
_thread: threading.Thread | None = None
|
||||
_lock = threading.Lock()
|
||||
_LAST_TASK_ID = 0
|
||||
|
||||
|
||||
def _interval_seconds() -> int:
|
||||
@@ -23,6 +24,7 @@ def _interval_seconds() -> int:
|
||||
|
||||
|
||||
def _run() -> None:
|
||||
global _LAST_TASK_ID
|
||||
from . import service
|
||||
|
||||
while not _stop_event.is_set():
|
||||
@@ -31,11 +33,20 @@ def _run() -> None:
|
||||
break
|
||||
db = SessionLocal()
|
||||
try:
|
||||
result = service.sync_pending_tasks(db, limit=500)
|
||||
queued = service.reconcile_binding_state_outbox(db, limit=500)
|
||||
queued = 0
|
||||
for _page in range(20):
|
||||
result = service.reconcile_binding_state_outbox(
|
||||
db,
|
||||
limit=500,
|
||||
after_task_id=_LAST_TASK_ID,
|
||||
)
|
||||
queued += int(result["queued"])
|
||||
_LAST_TASK_ID = int(result["nextTaskId"])
|
||||
if not result["hasMore"]:
|
||||
_LAST_TASK_ID = 0
|
||||
break
|
||||
logger.info(
|
||||
"直播账号履约低频对账完成: order_sync={} outbox_requeued={}",
|
||||
result,
|
||||
"直播账号履约低频对账完成: outbox_requeued={}",
|
||||
queued,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 对账失败等待下一轮
|
||||
|
||||
@@ -35,6 +35,7 @@ from ..douyu_service import douyu_config_value, ensure_douyu_config
|
||||
from ..external_order_source import (
|
||||
OrderLookupUnavailable,
|
||||
list_pending_manual_fulfillments,
|
||||
lookup_pending_manual_fulfillment,
|
||||
)
|
||||
from .account_selector import (
|
||||
AccountCandidate,
|
||||
@@ -97,6 +98,360 @@ def _mapping_for_sku(
|
||||
)
|
||||
|
||||
|
||||
def _parse_snapshot_datetime(value: Any) -> datetime | None:
|
||||
if isinstance(value, datetime):
|
||||
return value if value.tzinfo is not None else value.replace(tzinfo=UTC)
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
parsed = datetime.fromisoformat(text)
|
||||
except ValueError:
|
||||
return None
|
||||
return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=UTC)
|
||||
|
||||
|
||||
def _snapshot_value(data: dict[str, Any], *names: str, default: Any = "") -> Any:
|
||||
for name in names:
|
||||
if name in data and data[name] is not None:
|
||||
return data[name]
|
||||
return default
|
||||
|
||||
|
||||
def _order_snapshot_from_event(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
data = payload.get("data")
|
||||
return data if isinstance(data, dict) else payload
|
||||
|
||||
|
||||
def _upsert_order_snapshot(
|
||||
db: Session,
|
||||
snapshot: dict[str, Any],
|
||||
*,
|
||||
mapping: PlatformCommodityMapping | None,
|
||||
) -> tuple[PlatformFulfillmentTask | None, bool, bool]:
|
||||
"""应用一条订单项快照,返回 (task, created, ignored)。"""
|
||||
item_id = str(
|
||||
_snapshot_value(snapshot, "sourceOrderItemId", "source_order_item_id", default="")
|
||||
).strip()
|
||||
if not item_id:
|
||||
return None, False, True
|
||||
|
||||
paid_at = _parse_snapshot_datetime(
|
||||
_snapshot_value(snapshot, "paidAt", "paid_at", default=None)
|
||||
)
|
||||
order_created_at = _parse_snapshot_datetime(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"orderCreatedAt",
|
||||
"order_created_at",
|
||||
default=None,
|
||||
)
|
||||
)
|
||||
order_status = str(
|
||||
_snapshot_value(snapshot, "orderStatus", "order_status", default="")
|
||||
)
|
||||
pay_status = str(
|
||||
_snapshot_value(snapshot, "payStatus", "pay_status", default="paid")
|
||||
)
|
||||
terminal_order = order_status in {"refunded", "cancelled", "closed"}
|
||||
if not terminal_order and pay_status != "paid":
|
||||
return None, False, True
|
||||
cutoff = _order_match_started_at(db)
|
||||
match_basis_at = paid_at or order_created_at
|
||||
if (
|
||||
match_basis_at is not None
|
||||
and cutoff is not None
|
||||
and match_basis_at < cutoff
|
||||
):
|
||||
return None, False, True
|
||||
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.source_system == "order_site",
|
||||
PlatformFulfillmentTask.source_order_item_id == item_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
created = task is None
|
||||
if task is None:
|
||||
task = PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id=str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceOrderId",
|
||||
"source_order_id",
|
||||
"orderId",
|
||||
default="",
|
||||
)
|
||||
),
|
||||
source_order_item_id=item_id,
|
||||
source_fulfillment_id=str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceFulfillmentId",
|
||||
"source_fulfillment_id",
|
||||
"fulfillmentId",
|
||||
default="",
|
||||
)
|
||||
),
|
||||
target_platform=TARGET_PLATFORM,
|
||||
scope=SCOPE_ELITE,
|
||||
created_at=_now(),
|
||||
updated_at=_now(),
|
||||
)
|
||||
db.add(task)
|
||||
db.flush()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"task_created",
|
||||
detail={
|
||||
"platformOrderId": _snapshot_value(
|
||||
snapshot, "platformOrderId", "platform_order_id", default=""
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
before = (
|
||||
task.platform_order_id,
|
||||
task.source_order_id,
|
||||
task.source_fulfillment_id,
|
||||
task.provider,
|
||||
task.shop_id,
|
||||
task.shop_name,
|
||||
task.sku_code,
|
||||
task.sku_name,
|
||||
task.internal_sku_key,
|
||||
task.quantity,
|
||||
task.order_amount_fen,
|
||||
task.paid_at,
|
||||
task.execution_status,
|
||||
task.commodity_mapping_id,
|
||||
task.commodity_id,
|
||||
task.required_points,
|
||||
task.issue_code,
|
||||
)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
task.source_snapshot_json = _jsonable(snapshot)
|
||||
return task, created, False
|
||||
|
||||
terminal = (
|
||||
terminal_order
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"fulfillmentStatus",
|
||||
"fulfillment_status",
|
||||
default="",
|
||||
)
|
||||
== "archived"
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"configEnabled",
|
||||
"config_enabled",
|
||||
default=True,
|
||||
)
|
||||
is False
|
||||
or _snapshot_value(
|
||||
snapshot,
|
||||
"douyuFulfillmentEnabled",
|
||||
"douyu_fulfillment_enabled",
|
||||
default=True,
|
||||
)
|
||||
is False
|
||||
)
|
||||
|
||||
task.platform_order_id = str(
|
||||
_snapshot_value(snapshot, "platformOrderId", "platform_order_id", default="")
|
||||
)
|
||||
task.source_order_id = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceOrderId",
|
||||
"source_order_id",
|
||||
"orderId",
|
||||
default=task.source_order_id,
|
||||
)
|
||||
)
|
||||
task.source_fulfillment_id = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"sourceFulfillmentId",
|
||||
"source_fulfillment_id",
|
||||
"fulfillmentId",
|
||||
default=task.source_fulfillment_id,
|
||||
)
|
||||
)
|
||||
task.provider = str(
|
||||
_snapshot_value(snapshot, "provider", default=task.provider)
|
||||
)
|
||||
task.shop_id = str(
|
||||
_snapshot_value(snapshot, "shopId", "shop_id", default="")
|
||||
)
|
||||
task.shop_name = str(
|
||||
_snapshot_value(snapshot, "shopName", "shop_name", default="")
|
||||
)
|
||||
task.sku_code = str(
|
||||
_snapshot_value(snapshot, "skuCode", "sku_code", default="")
|
||||
)
|
||||
task.sku_name = str(
|
||||
_snapshot_value(snapshot, "skuName", "sku_name", default="")
|
||||
)
|
||||
task.internal_sku_key = str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"internalSkuKey",
|
||||
"internal_sku_key",
|
||||
default="",
|
||||
)
|
||||
).strip()
|
||||
task.quantity = max(
|
||||
1,
|
||||
int(_snapshot_value(snapshot, "quantity", default=1) or 1),
|
||||
)
|
||||
task.order_amount_fen = int(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"revenueAmountFen",
|
||||
"revenue_amount_fen",
|
||||
"totalAmountFen",
|
||||
"total_amount",
|
||||
default=0,
|
||||
)
|
||||
or 0
|
||||
)
|
||||
task.paid_at = paid_at
|
||||
task.source_snapshot_json = _jsonable(snapshot)
|
||||
task.updated_at = _now()
|
||||
|
||||
if terminal:
|
||||
task.execution_status = "cancelled"
|
||||
task.issue_code = "source_cancelled"
|
||||
task.last_error = "订单已关闭或直播履约配置已停用"
|
||||
task.next_allocate_at = None
|
||||
else:
|
||||
_apply_mapping(task, mapping)
|
||||
if task.execution_status == "ready" and task.account_id is None:
|
||||
task.next_allocate_at = _now()
|
||||
elif task.account_id is not None:
|
||||
task.next_allocate_at = None
|
||||
|
||||
after = (
|
||||
task.platform_order_id,
|
||||
task.source_order_id,
|
||||
task.source_fulfillment_id,
|
||||
task.provider,
|
||||
task.shop_id,
|
||||
task.shop_name,
|
||||
task.sku_code,
|
||||
task.sku_name,
|
||||
task.internal_sku_key,
|
||||
task.quantity,
|
||||
task.order_amount_fen,
|
||||
task.paid_at,
|
||||
task.execution_status,
|
||||
task.commodity_mapping_id,
|
||||
task.commodity_id,
|
||||
task.required_points,
|
||||
task.issue_code,
|
||||
)
|
||||
changed = created or before != after
|
||||
if changed:
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return task, created, False
|
||||
|
||||
|
||||
def _mapping_map(db: Session) -> dict[str, PlatformCommodityMapping]:
|
||||
rows = (
|
||||
db.query(PlatformCommodityMapping)
|
||||
.filter(
|
||||
PlatformCommodityMapping.target_platform == TARGET_PLATFORM,
|
||||
PlatformCommodityMapping.enabled.is_(True),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
str(row.internal_sku_key or "").strip(): row
|
||||
for row in rows
|
||||
if str(row.internal_sku_key or "").strip()
|
||||
}
|
||||
|
||||
|
||||
def sync_order_snapshot(
|
||||
db: Session,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""消费 order_site 订单项快照;正常路径不再读取 order_site 数据库。"""
|
||||
snapshot = _order_snapshot_from_event(payload)
|
||||
mapping_by_sku = _mapping_map(db)
|
||||
mapping = mapping_by_sku.get(
|
||||
str(
|
||||
_snapshot_value(
|
||||
snapshot,
|
||||
"internalSkuKey",
|
||||
"internal_sku_key",
|
||||
default="",
|
||||
)
|
||||
).strip()
|
||||
)
|
||||
task, created, ignored = _upsert_order_snapshot(db, snapshot, mapping=mapping)
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"fetched": 1,
|
||||
"created": int(created),
|
||||
"updated": int(task is not None and not created),
|
||||
"ignored": int(ignored),
|
||||
"taskIds": [int(task.id)] if task is not None else [],
|
||||
}
|
||||
|
||||
|
||||
def sync_order_item_ids(
|
||||
db: Session,
|
||||
source_order_item_ids: list[str],
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""兼容旧版事件:只定向读取明确订单项,不再走最近 500 条窗口。"""
|
||||
mapping_by_sku = _mapping_map(db)
|
||||
created = 0
|
||||
updated = 0
|
||||
ignored = 0
|
||||
task_ids: list[int] = []
|
||||
for item_id in dict.fromkeys(str(item or "").strip() for item in source_order_item_ids):
|
||||
if not item_id:
|
||||
ignored += 1
|
||||
continue
|
||||
snapshot = lookup_pending_manual_fulfillment(item_id)
|
||||
if snapshot is None:
|
||||
ignored += 1
|
||||
continue
|
||||
mapping = mapping_by_sku.get(str(snapshot.get("internal_sku_key") or "").strip())
|
||||
task, was_created, was_ignored = _upsert_order_snapshot(
|
||||
db,
|
||||
snapshot,
|
||||
mapping=mapping,
|
||||
)
|
||||
if task is None:
|
||||
ignored += int(was_ignored)
|
||||
continue
|
||||
created += int(was_created)
|
||||
updated += int(not was_created)
|
||||
task_ids.append(int(task.id))
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"fetched": len(task_ids),
|
||||
"created": created,
|
||||
"updated": updated,
|
||||
"ignored": ignored,
|
||||
"taskIds": task_ids,
|
||||
}
|
||||
|
||||
|
||||
def get_settings(db: Session) -> dict[str, Any]:
|
||||
now = _now()
|
||||
row = (
|
||||
@@ -272,13 +627,25 @@ def _emit_order_site_binding_state(
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> bool:
|
||||
"""当前事务内写 Outbox;HTTP 投递由独立 worker 完成。"""
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||||
.with_for_update()
|
||||
.one()
|
||||
)
|
||||
next_state_version = int(task.state_version or 0) + 1
|
||||
payload = customer_binding_out(db, task_id)
|
||||
payload["stateVersion"] = next_state_version
|
||||
if extra:
|
||||
payload.update(extra)
|
||||
return enqueue_order_site_binding_state(
|
||||
enqueued = enqueue_order_site_binding_state(
|
||||
db,
|
||||
payload,
|
||||
)
|
||||
if enqueued:
|
||||
task.state_version = next_state_version
|
||||
db.flush()
|
||||
return enqueued
|
||||
|
||||
|
||||
def _load_task(db: Session, task_id: int) -> PlatformFulfillmentTask:
|
||||
@@ -355,6 +722,7 @@ def _sync_pending_tasks(
|
||||
created = 0
|
||||
updated = 0
|
||||
skipped_unmapped = 0
|
||||
task_ids: list[int] = []
|
||||
for row in rows:
|
||||
item_id = str(row.get("source_order_item_id") or "")
|
||||
if not item_id:
|
||||
@@ -414,7 +782,10 @@ def _sync_pending_tasks(
|
||||
task.updated_at = _now()
|
||||
if task.execution_status in {"pending_mapping", "ready"}:
|
||||
_apply_mapping(task, mapping)
|
||||
if task.execution_status == "ready" and task.account_id is None:
|
||||
task.next_allocate_at = _now()
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
task_ids.append(int(task.id))
|
||||
|
||||
if commit:
|
||||
db.commit()
|
||||
@@ -425,6 +796,7 @@ def _sync_pending_tasks(
|
||||
"skippedUnmapped": skipped_unmapped,
|
||||
"configuredSkuCount": len(mappings_by_sku),
|
||||
"orderMatchStartedAt": _jsonable(match_started_at),
|
||||
"taskIds": task_ids,
|
||||
}
|
||||
|
||||
|
||||
@@ -723,6 +1095,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindReadyForConfirm": (bool(pending_role) and not binding_failed),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")) and binding_confirmed,
|
||||
"stateVersion": int(task.state_version or 0),
|
||||
"completedAt": _jsonable(task.completed_at),
|
||||
"updatedAt": _jsonable(task.updated_at),
|
||||
}
|
||||
@@ -741,7 +1114,11 @@ def ensure_customer_session(
|
||||
platform_order_id=platform_order_id,
|
||||
)
|
||||
if task is None:
|
||||
sync_pending_tasks(db, limit=500)
|
||||
sync_order_item_ids(
|
||||
db,
|
||||
[source_order_item_id],
|
||||
commit=True,
|
||||
)
|
||||
task = _find_customer_task(
|
||||
db,
|
||||
source_order_item_id=source_order_item_id,
|
||||
@@ -891,9 +1268,15 @@ def upsert_mapping(
|
||||
)
|
||||
pending = _apply_order_match_cutoff(pending, _order_match_started_at(db))
|
||||
pending = pending.all()
|
||||
task_ids: list[int] = []
|
||||
for task in pending:
|
||||
_apply_mapping(task, row if row.enabled else None)
|
||||
task.updated_at = _now()
|
||||
task.next_allocate_at = (
|
||||
_now()
|
||||
if task.execution_status == "ready" and task.account_id is None
|
||||
else None
|
||||
)
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
@@ -902,7 +1285,13 @@ def upsert_mapping(
|
||||
actor_id=current.id,
|
||||
detail={"commodityId": row.commodity_id, "enabled": row.enabled},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
task_ids.append(int(task.id))
|
||||
db.commit()
|
||||
if task_ids:
|
||||
from .auto_allocator import wake_auto_allocator
|
||||
|
||||
wake_auto_allocator(task_ids)
|
||||
return next(
|
||||
item for item in list_mappings(db, keyword=sku_key) if item["id"] == row.id
|
||||
)
|
||||
@@ -1019,6 +1408,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
)
|
||||
if selected is None or selected_state is None or lease is None:
|
||||
task.execution_status = "ready"
|
||||
task.next_allocate_at = _now() + timedelta(seconds=45)
|
||||
task.issue_code = "no_eligible_account"
|
||||
if not candidates:
|
||||
task.last_error = "没有带 CK、允许自动管理且未停用的斗鱼账号"
|
||||
@@ -1049,6 +1439,7 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
task.lease_id = lease.id
|
||||
task.operator_id = current.id
|
||||
task.execution_status = "allocated"
|
||||
task.next_allocate_at = None
|
||||
task.binding_status = "not_started"
|
||||
soft_reasons = selected_state.soft_requirement_reasons(
|
||||
required_points=required_points
|
||||
@@ -1390,19 +1781,27 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
||||
return len(task_ids)
|
||||
|
||||
|
||||
def reconcile_binding_state_outbox(db: Session, *, limit: int = 500) -> int:
|
||||
"""低频补齐 active 任务当前快照;只会在缺失或 dead 时重新入队。"""
|
||||
def reconcile_binding_state_outbox(
|
||||
db: Session,
|
||||
*,
|
||||
limit: int = 500,
|
||||
after_task_id: int = 0,
|
||||
) -> dict[str, Any]:
|
||||
"""按 ID 游标补齐 active 快照;批次只限制吞吐,不形成永久扫描窗口。"""
|
||||
batch_size = max(1, min(limit, 500))
|
||||
normalized_after = max(0, int(after_task_id or 0))
|
||||
task_ids = [
|
||||
int(row.id)
|
||||
for row in (
|
||||
db.query(PlatformFulfillmentTask.id)
|
||||
.filter(
|
||||
PlatformFulfillmentTask.id > normalized_after,
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
["completed", "cancelled"]
|
||||
)
|
||||
)
|
||||
.order_by(PlatformFulfillmentTask.updated_at.asc())
|
||||
.limit(max(1, min(limit, 500)))
|
||||
.order_by(PlatformFulfillmentTask.id.asc())
|
||||
.limit(batch_size)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
@@ -1410,7 +1809,11 @@ def reconcile_binding_state_outbox(db: Session, *, limit: int = 500) -> int:
|
||||
1 for task_id in task_ids if _emit_order_site_binding_state(db, task_id)
|
||||
)
|
||||
db.commit()
|
||||
return queued
|
||||
return {
|
||||
"queued": queued,
|
||||
"nextTaskId": max(task_ids, default=normalized_after),
|
||||
"hasMore": len(task_ids) == batch_size,
|
||||
}
|
||||
|
||||
|
||||
async def confirm_binding(
|
||||
@@ -1705,6 +2108,8 @@ __all__ = [
|
||||
"release_task_account",
|
||||
"reset_task_binding",
|
||||
"start_binding",
|
||||
"sync_order_item_ids",
|
||||
"sync_order_snapshot",
|
||||
"sync_pending_tasks",
|
||||
"sync_pending_tasks_if_due",
|
||||
"sync_task_from_binding_runner",
|
||||
|
||||
Reference in New Issue
Block a user