feat(fulfillment): 增加签名 Webhook 事务 Outbox

- 新增直播履约状态事务 Outbox 与签名投递 worker
- 增加 order_site 状态回传、重试和低频对账兜底
- 补充 Outbox、签名校验与集成接口测试
This commit is contained in:
yml
2026-11-18 10:59:20 +08:00
parent 00c4a268c2
commit bba3d9dac9
17 changed files with 967 additions and 114 deletions
@@ -1,86 +1,53 @@
"""把直播账号履约状态变化主动推送给 order_site。"""
"""把直播账号履约状态变化写入 order_site 的事务 Outbox。"""
from __future__ import annotations
import hashlib
import json
import os
import time
import uuid
from datetime import UTC, datetime
from threading import Thread
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from ...database import SessionLocal
from ..integration.outbox import enqueue_integration_event
PUBLISH_ATTEMPTS = 3
PUBLISH_TIMEOUT_SECONDS = 5
EVENT_TYPE = "platform_fulfillment.binding_state_changed"
def notify_order_site_binding_state_async(task_id: int) -> None:
"""状态提交后异步推送;失败由工作台低频对账补齐。"""
Thread(
target=_publish_binding_state,
args=(int(task_id),),
name=f"order-site-fulfillment-{task_id}",
daemon=True,
).start()
def _state_dedupe_key(payload: dict[str, Any]) -> str:
state = {
"sessionId": payload.get("sessionId"),
"executionStatus": payload.get("executionStatus"),
"bindingStatus": payload.get("bindingStatus"),
"exchangeStatus": payload.get("exchangeStatus"),
"issueCode": payload.get("issueCode"),
"accountReady": payload.get("accountReady"),
"bindTaskStatus": payload.get("bindTaskStatus"),
"bindPhase": payload.get("bindPhase"),
"qrUrl": payload.get("qrUrl"),
"roleName": payload.get("roleName"),
"areaName": payload.get("areaName"),
"platName": payload.get("platName"),
"bindReadyForConfirm": payload.get("bindReadyForConfirm"),
"bindConfirmed": payload.get("bindConfirmed"),
}
digest = hashlib.sha256(
json.dumps(state, ensure_ascii=False, sort_keys=True).encode("utf-8")
).hexdigest()[:32]
return f"binding-state:{payload.get('sessionId')}:{digest}"
def _publish_binding_state(task_id: int) -> None:
base_url = os.getenv("ORDER_SITE_PLATFORM_FULFILLMENT_URL", "").strip().rstrip("/")
integration_key = os.getenv("PLATFORM_FULFILLMENT_INTEGRATION_KEY", "").strip()
if not base_url or not integration_key:
return
db = SessionLocal()
try:
from . import service
payload = service.customer_binding_out(db, task_id)
except Exception as exc: # noqa: BLE001
logger.warning("构造 order_site 履约状态事件失败 task_id={}: {}", task_id, exc)
return
finally:
db.close()
body = json.dumps(
{
"eventId": str(uuid.uuid4()),
"occurredAt": datetime.now(UTC).isoformat(),
"items": [payload],
},
ensure_ascii=False,
).encode("utf-8")
endpoint = f"{base_url}/api/integration/platform-fulfillment/binding-state"
for attempt in range(1, PUBLISH_ATTEMPTS + 1):
request = Request(
endpoint,
data=body,
method="POST",
headers={
"Content-Type": "application/json",
"X-Integration-Key": integration_key,
},
)
try:
with urlopen(request, timeout=PUBLISH_TIMEOUT_SECONDS) as response:
if 200 <= response.status < 300:
return
raise RuntimeError(f"order_site 返回 HTTP {response.status}")
except (HTTPError, URLError, OSError, RuntimeError) as exc:
if attempt >= PUBLISH_ATTEMPTS:
logger.warning(
"推送 order_site 履约状态失败 task_id={} attempts={}: {}",
task_id,
attempt,
exc,
)
return
time.sleep(float(attempt))
def enqueue_order_site_binding_state(
db: Session,
payload: dict[str, Any],
) -> bool:
"""必须在任务状态变更的同一事务内调用。"""
return enqueue_integration_event(
db,
destination="order_site",
event_type=EVENT_TYPE,
payload={"items": [payload]},
dedupe_key=_state_dedupe_key(payload),
)
__all__ = ["notify_order_site_binding_state_async"]
__all__ = ["enqueue_order_site_binding_state"]
@@ -0,0 +1,72 @@
"""低频订单对账兜底;正常同步由 order_site 签名 Webhook 驱动。"""
from __future__ import annotations
import os
import threading
from loguru import logger
from ...database import SessionLocal
_stop_event = threading.Event()
_thread: threading.Thread | None = None
_lock = threading.Lock()
def _interval_seconds() -> int:
try:
value = int(os.getenv("PLATFORM_FULFILLMENT_RECONCILE_SECONDS", "900"))
except ValueError:
return 900
return max(60, value)
def _run() -> None:
from . import service
while not _stop_event.is_set():
# 不抢占启动时的 Webhook;首轮延后一分钟。
if _stop_event.wait(min(60, _interval_seconds())):
break
db = SessionLocal()
try:
result = service.sync_pending_tasks(db, limit=500)
queued = service.reconcile_binding_state_outbox(db, limit=500)
logger.info(
"直播账号履约低频对账完成: order_sync={} outbox_requeued={}",
result,
queued,
)
except Exception as exc: # noqa: BLE001 - 对账失败等待下一轮
db.rollback()
logger.warning("直播账号履约低频对账失败: {}", exc)
finally:
db.close()
if _stop_event.wait(_interval_seconds()):
break
def start_platform_fulfillment_reconciler() -> bool:
global _thread
with _lock:
if _thread is not None and _thread.is_alive():
return False
_stop_event.clear()
_thread = threading.Thread(
target=_run,
name="platform-fulfillment-reconciler",
daemon=True,
)
_thread.start()
return True
def stop_platform_fulfillment_reconciler() -> None:
global _thread
_stop_event.set()
with _lock:
thread = _thread
_thread = None
if thread is not None and thread.is_alive():
thread.join(timeout=3)
@@ -40,7 +40,7 @@ from .account_selector import (
release_lease,
renew_lease,
)
from .order_site_notifier import notify_order_site_binding_state_async
from .order_site_notifier import enqueue_order_site_binding_state
TARGET_PLATFORM = "douyu"
SCOPE_ELITE = "elite"
@@ -55,7 +55,7 @@ ACTIVE_STATUSES = {
}
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
AUTO_SYNC_INTERVAL_SECONDS = 60.0
AUTO_SYNC_INTERVAL_SECONDS = 900.0
_SYNC_LOCK = Lock()
_AUTO_SYNC_LOCK = Lock()
@@ -228,6 +228,14 @@ def _append_event(
)
def _emit_order_site_binding_state(db: Session, task_id: int) -> bool:
"""当前事务内写 Outbox;HTTP 投递由独立 worker 完成。"""
return enqueue_order_site_binding_state(
db,
customer_binding_out(db, task_id),
)
def _load_task(db: Session, task_id: int) -> PlatformFulfillmentTask:
task = (
db.query(PlatformFulfillmentTask)
@@ -259,7 +267,12 @@ def _apply_mapping(
task.last_error = ""
def _sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
def _sync_pending_tasks(
db: Session,
*,
limit: int = 100,
commit: bool = True,
) -> dict[str, Any]:
"""从 order_site 拉取人工待履约订单项并幂等生成本地作业。"""
try:
rows = list_pending_manual_fulfillments(limit=limit)
@@ -320,15 +333,22 @@ def _sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
task.updated_at = _now()
if task.execution_status in {"pending_mapping", "ready"}:
_apply_mapping(task, _mapping_for_sku(db, task.internal_sku_key))
_emit_order_site_binding_state(db, task.id)
db.commit()
if commit:
db.commit()
return {"fetched": len(rows), "created": created, "updated": updated}
def sync_pending_tasks(db: Session, *, limit: int = 100) -> dict[str, Any]:
def sync_pending_tasks(
db: Session,
*,
limit: int = 100,
commit: bool = True,
) -> dict[str, Any]:
"""串行同步订单,避免自动同步与人工同步并发创建重复任务。"""
with _SYNC_LOCK:
return _sync_pending_tasks(db, limit=limit)
return _sync_pending_tasks(db, limit=limit, commit=commit)
def sync_pending_tasks_if_due(
@@ -539,7 +559,7 @@ def _customer_task_message(task: PlatformFulfillmentTask) -> str:
"elite_not_opened": "账号正在准备精英宝典,请稍后重试",
"points_short": "账号正在补充积分,请稍后重试",
"commodity_limit_reached": "当前商品暂时无法兑换,请联系客服",
"binding_failed": "绑定失败,请刷新二维码后重试",
"binding_failed": "暂未识别到绑定角色,请继续使用当前二维码完成扫码",
"eligibility_check_failed": "状态查询异常,请稍后刷新",
"manual_required": "当前需要人工处理,请联系客服",
}
@@ -892,8 +912,8 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
"diagnostics": diagnostics,
},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
task.account_id = selected.account_id
@@ -936,8 +956,8 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
"exchangeStatus": task.exchange_status,
},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -972,8 +992,8 @@ def release_task_account(
actor_id=current.id,
detail={"reason": reason},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -1044,8 +1064,8 @@ async def start_binding(
actor_id=current.id,
detail={"bindTaskId": bind_task.id},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
start_douyu_batch(
batch_id=batch_id,
task_type="get_bind_qr",
@@ -1090,8 +1110,6 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
task.execution_status = "waiting_bind"
task.issue_code = ""
task.last_error = ""
task.updated_at = _now()
db.commit()
after = (
task.binding_status,
task.execution_status,
@@ -1100,10 +1118,50 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
task.last_error,
)
if after != before:
notify_order_site_binding_state_async(task.id)
task.updated_at = _now()
_emit_order_site_binding_state(db, task.id)
db.commit()
return get_task_detail(db, task.id)
def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
"""斗鱼绑定任务落库后,把关联履约任务和 Outbox 一并刷新。"""
task_ids = [
int(row.id)
for row in (
db.query(PlatformFulfillmentTask.id)
.filter(PlatformFulfillmentTask.bind_task_id == int(douyu_task_id))
.all()
)
]
for task_id in task_ids:
refresh_binding(db, task_id=task_id)
return len(task_ids)
def reconcile_binding_state_outbox(db: Session, *, limit: int = 500) -> int:
"""低频补齐 active 任务当前快照;只会在缺失或 dead 时重新入队。"""
task_ids = [
int(row.id)
for row in (
db.query(PlatformFulfillmentTask.id)
.filter(
PlatformFulfillmentTask.execution_status.notin_(
["completed", "cancelled"]
)
)
.order_by(PlatformFulfillmentTask.updated_at.asc())
.limit(max(1, min(limit, 500)))
.all()
)
]
queued = sum(
1 for task_id in task_ids if _emit_order_site_binding_state(db, task_id)
)
db.commit()
return queued
async def confirm_binding(
db: Session, *, task_id: int, current: User
) -> dict[str, Any]:
@@ -1145,8 +1203,8 @@ async def confirm_binding(
actor_id=current.id,
detail={"confirmTaskId": confirm_task.id},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
start_douyu_batch(
batch_id=batch_id,
task_type="confirm_bind",
@@ -1192,8 +1250,8 @@ def refresh_exchange_eligibility(
actor_id=current.id,
detail={"error": str(exc)},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
if task.commodity_id in eligibility.limited_ids:
@@ -1228,8 +1286,8 @@ def refresh_exchange_eligibility(
"limitedCount": len(eligibility.limited_ids),
},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -1269,8 +1327,8 @@ def complete_manual_exchange(
actor_id=current.id,
detail={"note": note[:1000]},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -1295,8 +1353,8 @@ def mark_manual_required(
actor_id=current.id,
detail={"reason": task.last_error},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -1326,8 +1384,8 @@ def cancel_task(
actor_id=current.id,
detail={"reason": task.last_error},
)
_emit_order_site_binding_state(db, task.id)
db.commit()
notify_order_site_binding_state_async(task.id)
return get_task_detail(db, task.id)
@@ -1341,11 +1399,13 @@ __all__ = [
"list_mappings",
"list_tasks",
"mark_manual_required",
"reconcile_binding_state_outbox",
"refresh_binding",
"refresh_exchange_eligibility",
"release_task_account",
"start_binding",
"sync_pending_tasks",
"sync_pending_tasks_if_due",
"sync_task_from_binding_runner",
"upsert_mapping",
]