feat(fulfillment): 增加签名 Webhook 事务 Outbox
- 新增直播履约状态事务 Outbox 与签名投递 worker - 增加 order_site 状态回传、重试和低频对账兜底 - 补充 Outbox、签名校验与集成接口测试
This commit is contained in:
@@ -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",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user