fix(shipping-notice): 修复抽取续抽与 STP 入队并发死锁并幂等草稿校验
- 抽取 Job 续抽交接时 defer_stp_enqueue,STP 入队推迟到 run 收口统一触发,消除与续抽 Job 互抢行锁的死锁(2026-09-22 生产事故) - 草稿显式 validate 在内容未变时幂等短路:不递增版本、不写事件、不重复触发下游,防连点「保存并校验」产生版本/Job 风暴(事故根因) - STP 自动建单入队前 supersede 同文档过期排队 Job,防止旧版本 Job 抢跑 - 抽取 Job 异常收口识别 PostgreSQL 瞬时锁冲突(40P01/55P03)标记可重试,不再终态失败要求人工重新识别 - 订单凭据加密抽离为 app/core/credential_crypto 通用模块,shipping_notice 委托调用并保留原错误码 - 同步复核视图与测试:新增草稿校验幂等契约测试,更新 STP 入队与 Worker 重试用例
This commit is contained in:
1 parent
13c33d063c
commit
5df204fdea
12 files changed
+865
-87
No files matched your search
@@ -724,6 +724,18 @@ def _handoff_continue_order_extraction(
|
||||
return True
|
||||
|
||||
|
||||
def _is_transient_lock_error(exc: BaseException) -> bool:
|
||||
"""
|
||||
判断异常是否为瞬时锁冲突(PostgreSQL 死锁 40P01 / 锁等待超时 55P03)。
|
||||
|
||||
【AI 触发时机】:订单抽取 Job 异常收口时决定是否标记可重试。此类错误重试即可恢复,
|
||||
按终态失败收口会触发 run failed 并要求人工重新识别(2026-09-22 生产事故两次)。
|
||||
SQLAlchemy 将 DBAPI 错误包装为 exc.orig;非 DBAPI 异常(无 pgcode)一律返回 False。
|
||||
"""
|
||||
orig = getattr(exc, 'orig', None)
|
||||
return getattr(orig, 'pgcode', None) in {'40P01', '55P03'}
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_ORDER_EXTRACTION)
|
||||
def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
"""
|
||||
@@ -917,15 +929,16 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
persisted_orders.extend(unit_orders_by_id[int(unit.id)])
|
||||
wave_orders.extend(unit_orders_by_id[int(unit.id)])
|
||||
# 重试路径:已 candidate_ready 的 Unit 仍须独立触发下游(幂等)。
|
||||
try:
|
||||
progress_ready_extraction_unit(
|
||||
run=run, unit=unit, orders=unit_orders_by_id[int(unit.id)],
|
||||
)
|
||||
except Exception:
|
||||
from flask import current_app
|
||||
current_app.logger.exception(
|
||||
'retry unit progress failed run=%s unit=%s', run.run_no, unit.id,
|
||||
)
|
||||
# [交接不变量] defer_stp_enqueue:开工快照仍有待排版附件(本 Job 必交接)时
|
||||
# 推迟 STP 入队到 run 收口,确保 STP 只由最后一个抽取 Job 提交;否则本 Job
|
||||
# 提交的 STP Job 会与续抽 Job 并发执行,双方以相反顺序抢 run/intake 行锁
|
||||
# 形成结构性死锁(2026-09-22 SNER-20260922-6466EB53 生产事故)。
|
||||
# 推送失败必须冒泡(与下方新单元推送同一规则):此处是这些 Unit 候选与
|
||||
# 复核 Case 的唯一物化触发点,吞掉等于静默丢单。
|
||||
progress_ready_extraction_unit(
|
||||
run=run, unit=unit, orders=unit_orders_by_id[int(unit.id)],
|
||||
defer_stp_enqueue=bool(pending_layout_ids),
|
||||
)
|
||||
continue
|
||||
if unit.status == 'review_required':
|
||||
continue
|
||||
@@ -1195,9 +1208,11 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
apply_worker_cancellation_checkpoint(run=run)
|
||||
db.session.commit()
|
||||
continue
|
||||
# [交接不变量] 同 replay 路径:本 Job 仍会交接时推迟 STP 入队到 run 收口。
|
||||
try:
|
||||
unit_persist = progress_ready_extraction_unit(
|
||||
run=run, unit=unit, orders=merged_unit_orders,
|
||||
defer_stp_enqueue=bool(pending_layout_ids),
|
||||
)
|
||||
record_unit_progress(
|
||||
run=run,
|
||||
@@ -1417,6 +1432,14 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
finish_job(job.id, result=summary, message='订单抽取完成', owned_attempt=owned_attempt)
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
# [STEP] 瞬时锁错(PostgreSQL 死锁 40P01 / 锁超时 55P03)不收口 run/unit 失败:
|
||||
# 失败标记会写 run=failed,重试 Job 将被"run status is not executable"门弹掉。
|
||||
# 直接可重试入队,由队列在 attempts 耗尽前自动重排;冲突窗口为秒级,重试即过
|
||||
# (2026-09-22 SNER-20260922-6466EB53 / 879785D1 生产事故)。attempts 耗尽后
|
||||
# 走下方常规失败收口,fail-closed 语义不变。
|
||||
if _is_transient_lock_error(exc):
|
||||
fail_job(job.id, error=str(exc), retryable=True, owned_attempt=owned_attempt)
|
||||
return
|
||||
run = ShippingOrderProcessingRun.query.filter_by(run_no=str(job.business_ref or '')).with_for_update().first()
|
||||
retryable = False
|
||||
if run is not None and run.status in {'queued', 'processing', 'failed'}:
|
||||
|
||||
Reference in new issue
Block a user