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:
hugo committed 2026-09-23 20:51:50 +08:00
1 parent 556d3fd3db
commit f9f5053f0b
4 files changed
+200 -1

No files matched your search

@@ -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 {}