Merge branch 'hugo-shipping-notice' into dev

# Conflicts:
#	backend/app/services/shipping_notice/receipt_report_notification.py
This commit is contained in:
hugo committed 2026-09-23 01:09:28 +08:00
commit 876a7ade7d
212 files changed
+12056 -2889

No files matched your search

@@ -31,14 +31,15 @@ def run_in_transit_transfer_precheck(_ctx, payload: dict) -> None:
raise RuntimeError(f"missing_transfer_precheck: {precheck_no}")
deps = transfer_deps.get_transfer_dependencies()
source_codes = list(precheck.scope_json.get("source_codes") or []) if isinstance(
precheck.scope_json, dict
) else []
scope = precheck.scope_json if isinstance(precheck.scope_json, dict) else {}
source_codes = list(scope.get("source_codes") or [])
result = run_precheck(
source_codes=source_codes,
business_date=precheck.business_date,
deps=deps,
run_no=precheck.run.run_no if precheck.run else "RUN-PENDING",
# 类型范围随预检落库回读:异步重跑与创建时口径一致(缺省回落类型1)。
transfer_type=str(scope.get("transfer_type")) if scope.get("transfer_type") else None,
)
summary = result.summary
precheck.status = summary.status
@@ -50,6 +51,6 @@ def run_in_transit_transfer_precheck(_ctx, payload: dict) -> None:
"blocked_count": summary.counts.blocked_count,
}
precheck.estimated_transfer_quantity = summary.estimated_transfer_quantity
precheck.estimated_erp_write_count = summary.estimated_erp_write_count
precheck.estimated_external_write_count = summary.estimated_external_write_count
precheck.precheck_hash = summary.precheck_hash
db.session.commit()
@@ -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'}: