fix(shipping-notice): 库存回调先锁 Gate 统一锁序并保证轮询链异常不断裂
- 库存 RPA 回调写候选数据前先锁活跃 Execution 的全部 Gate 行,与 external-wait 轮询统一加锁顺序,消除双方反序抢 Gate/候选行形成的死锁环 - external-wait 轮询异常时回滚并按 Gate 现状分流:仍在等待外部则以 waiting_external_requeued 延迟重排保持轮询链不断;已被回调收口则安静结束, 轮询 Job 不再终态失败 - 错误文本留在 result_json.poll_error 供排查
This commit is contained in:
1 parent
556d3fd3db
commit
f9f5053f0b
4 files changed
+200
-1
No files matched your search
@@ -8,6 +8,7 @@ backend/app/jobs/handlers/shipping_process_gate.py
|
||||
次数耗尽时 fail_job 并 complete_gate_attempt(blocked_retryable),保留人工恢复入口;
|
||||
不修改 input_snapshot_json,以保持 input_snapshot_hash 与正文一致;
|
||||
waiting_external 先结束当前 Job 再延迟入队新 Job;
|
||||
external-wait 轮询异常时回滚并按 Gate 现状重排/安静结束,保证轮询链不因瞬时冲突断裂;
|
||||
healthcheck_probe 载荷用于部署探针,不依赖真实 Attempt。
|
||||
"""
|
||||
|
||||
@@ -88,7 +89,37 @@ def run_shipping_process_gate(ctx: JobContext, payload: dict) -> None:
|
||||
if external_wait_poll_gate_id and job is not None:
|
||||
from app.services.shipping_notice.process_gate_job_service import run_inventory_external_wait_poll
|
||||
|
||||
result = run_inventory_external_wait_poll(gate_id=external_wait_poll_gate_id)
|
||||
try:
|
||||
result = run_inventory_external_wait_poll(gate_id=external_wait_poll_gate_id)
|
||||
except Exception as exc:
|
||||
# [STEP] 轮询与库存回调可能以相反顺序争抢 Gate 行与候选行(2026-09-23 test job 8818,
|
||||
# 事务被毒化后以 InFailedSqlTransaction 收口,轮询链断裂导致 Gate 永久等待)。
|
||||
# 回滚后按 Gate 现状分流:仍在等待外部 → 结束本 Job 并延迟重排,保持轮询链不断;
|
||||
# Gate 已被回调等其他路径收口 → 安静结束。错误文本留在 result_json 供排查。
|
||||
db.session.rollback()
|
||||
gate = db.session.get(ShippingProcessGate, external_wait_poll_gate_id)
|
||||
gate_status = str(getattr(gate, 'status', '') or '') if gate is not None else None
|
||||
if gate_status == 'waiting_external':
|
||||
poll = max(int(current_app.config.get('SHIPPING_PROCESS_GATE_EXTERNAL_POLL_SECONDS', 30) or 30), 5)
|
||||
finish_job(
|
||||
job.id,
|
||||
result={
|
||||
'status': 'waiting_external_requeued',
|
||||
'poll_error': sanitize_gate_error_text(exc, max_len=300),
|
||||
},
|
||||
owned_attempt=int(job.attempts or 0),
|
||||
)
|
||||
if gate is not None and gate.latest_attempt_id:
|
||||
schedule_waiting_external_poll(attempt_id=int(gate.latest_attempt_id))
|
||||
db.session.commit()
|
||||
return
|
||||
finish_job(
|
||||
job.id,
|
||||
result={'status': 'poll_superseded', 'gate_status': gate_status},
|
||||
owned_attempt=int(job.attempts or 0),
|
||||
)
|
||||
db.session.commit()
|
||||
return
|
||||
finish_job(job.id, result=result, owned_attempt=int(job.attempts or 0))
|
||||
if str(result.get('gate_status') or '') == 'waiting_external':
|
||||
gate = db.session.get(ShippingProcessGate, external_wait_poll_gate_id)
|
||||
|
||||
@@ -792,6 +792,24 @@ def _validate_rpa_callback_round(task: ShippingNotice, payload: dict[str, Any])
|
||||
return None
|
||||
|
||||
|
||||
def _lock_process_gates_for_task(task: ShippingNotice) -> None:
|
||||
"""
|
||||
[锁序约定] 回调写库存候选数据前,先锁该任务活跃 Execution 的全部 Gate 行。
|
||||
|
||||
外部等待轮询(run_inventory_external_wait_poll)的顺序是先锁 Gate 再读候选行;
|
||||
本侧若先写候选、后经 Gate 同步更新 Gate 行,会以相反顺序与轮询形成死锁环,
|
||||
事务被毒化后以 InFailedSqlTransaction 收口且轮询链断裂
|
||||
(2026-09-23 test job 8818:库存 RPA 回调 × external-wait 轮询竞跑)。
|
||||
统一为两侧都先取 Gate 锁:后到方短暂阻塞后串行执行,不再产生错误。
|
||||
"""
|
||||
from app.models import ShippingProcessGate
|
||||
|
||||
execution_id = int(getattr(task, 'active_process_execution_id', 0) or 0)
|
||||
if not execution_id:
|
||||
return
|
||||
ShippingProcessGate.query.filter_by(execution_id=execution_id).with_for_update().all()
|
||||
|
||||
|
||||
def _validate_rpa_callback_gate(task: ShippingNotice) -> dict[str, Any] | None:
|
||||
"""
|
||||
复核 RPA 触发后可能漂移的订单与路由门禁(skipped/failed/success 共用)。
|
||||
@@ -1813,6 +1831,8 @@ def rpa_report_inventory(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
if gate_resp is not None:
|
||||
return gate_resp
|
||||
|
||||
# [锁序约定] 写入前先锁 Gate 行,与 external-wait 轮询串行(见 _lock_process_gates_for_task)。
|
||||
_lock_process_gates_for_task(task)
|
||||
runtime = _save_last_inventory_rpa_runtime(task, payload)
|
||||
callback_status = _normalize_rpa_inventory_result_status(payload)
|
||||
|
||||
@@ -2486,6 +2506,8 @@ def process_rpa_report_inventory_batch(payload: dict[str, Any]) -> dict[str, Any
|
||||
"error": gate_resp.get("message") or "inventory_source_gate_blocked",
|
||||
})
|
||||
continue
|
||||
# [锁序约定] 写入前先锁 Gate 行,与 external-wait 轮询串行(见 _lock_process_gates_for_task)。
|
||||
_lock_process_gates_for_task(task)
|
||||
round_resp = _validate_rpa_callback_round(task, merged_item)
|
||||
if round_resp is not None:
|
||||
round_data = round_resp.get("data") if isinstance(round_resp.get("data"), dict) else {}
|
||||
|
||||
Reference in new issue
Block a user