Merge branch 'hugo-shipping-notice' into test
This commit is contained in:
commit
e35f01bbe9
163 files changed
+21008
-2545
No files matched your search
@@ -2,13 +2,17 @@
|
||||
|
||||
用途:Invoice 级执行 Job:对单个明细执行写前复查 + ERP 原子写入。
|
||||
依赖:execution_service.execute_item、transfer deps、Job 注册表。
|
||||
逻辑:payload 携带 item_id;单项结果不影响其他项。
|
||||
逻辑:payload 携带 item_id;单项结果不影响其他项。业务收尾后 handler 自行
|
||||
finish_job(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.execution_service import execute_item
|
||||
|
||||
@@ -23,7 +27,16 @@ def run_in_transit_transfer_item(_ctx, payload: dict) -> None:
|
||||
item_id = payload.get("item_id")
|
||||
if item_id is None:
|
||||
raise RuntimeError("missing_transfer_item_id")
|
||||
execute_item(
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
status = execute_item(
|
||||
item_id=int(item_id),
|
||||
deps=transfer_deps.get_transfer_dependencies(),
|
||||
)
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨单行执行完成:item_id={item_id} status={status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -3,15 +3,17 @@
|
||||
用途:异步执行在途调拨匹配(文件准备 + 只读匹配),并更新匹配状态。
|
||||
依赖:Job 注册表、matching_service、transfer 模型、transfer deps。
|
||||
逻辑:领取后按 payload.matching_id 加载匹配,运行 run_matching 并回写
|
||||
状态/计数/Hash;匹配不存在时 fail-closed 抛 RuntimeError。
|
||||
状态/计数/Hash;匹配不存在时 fail-closed 抛 RuntimeError。业务收尾后
|
||||
handler 自行 finish_job(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import TransferMatching
|
||||
from app.models import Job, TransferMatching
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.matching_service import run_matching
|
||||
|
||||
@@ -29,6 +31,9 @@ def run_in_transit_transfer_match(_ctx, payload: dict) -> None:
|
||||
matching = db.session.query(TransferMatching).filter_by(matching_no=matching_no).first()
|
||||
if matching is None:
|
||||
raise RuntimeError(f"missing_transfer_matching: {matching_no}")
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
|
||||
deps = transfer_deps.get_transfer_dependencies()
|
||||
scope = matching.scope_json if isinstance(matching.scope_json, dict) else {}
|
||||
@@ -54,3 +59,9 @@ def run_in_transit_transfer_match(_ctx, payload: dict) -> None:
|
||||
matching.estimated_external_write_count = summary.estimated_external_write_count
|
||||
matching.matching_hash = summary.matching_hash
|
||||
db.session.commit()
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨匹配完成:matching={matching_no} status={summary.status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -1,30 +1,49 @@
|
||||
"""backend/app/jobs/handlers/in_transit_transfer_run.py
|
||||
|
||||
用途:Run 级执行 Job:按匹配 scope 圈定 ready 行并驱动按行执行服务完成写入。
|
||||
依赖:execution_service、transfer deps、Job 注册表。
|
||||
逻辑:payload 携带 matching_id(仅用于 scope 圈行);三层门禁由 execute_items
|
||||
统一裁定,门禁失败即入队路径失败(fail-closed)。
|
||||
用途:Run 级执行 Job:领取 TRANSFER_RUN 任务并驱动执行服务 worker 段完成单据级写入。
|
||||
依赖:execution_service.execute_run、transfer deps、Job 注册表。
|
||||
逻辑:payload {run_id, item_ids} 为常规路径(worker 执行 Run 名下全部单);
|
||||
{run_id, doc_id} 为 retrigger 路径(D11:result_unknown 单唯一出口,仅执行
|
||||
该单且只触发末步);门禁/组复用守卫由 prepare_execution 前置完成,二次门禁
|
||||
由 execute_run 统一裁定(fail-closed)。业务收尾后 handler 须自行 finish_job
|
||||
(runner 仅兜底异常路径,正常完成不代收口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app import db
|
||||
from app.jobs.queue import finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.execution_service import execute_matching_items
|
||||
from app.services.in_transit_transfer.execution_service import execute_run
|
||||
|
||||
|
||||
@register_handler(JobType.TRANSFER_RUN)
|
||||
def run_in_transit_transfer_run(_ctx, payload: dict) -> None:
|
||||
"""执行匹配 scope 内全部可执行行(异步路径)。
|
||||
"""执行 Run 名下全部单(或 retrigger 指定单)(异步路径)。
|
||||
|
||||
【AI 触发时机】:仅由执行 Run 入队路径调用;页面直连走 API 同步路径。
|
||||
【AI 触发时机】:仅由 prepare_execution(提交建单后)与 retrigger_document
|
||||
(人工重新触发)入队路径调用;页面直连走 API 同步段,不经过本 handler。
|
||||
"""
|
||||
payload = payload if isinstance(payload, dict) else {}
|
||||
matching_id = str(payload.get("matching_id") or "").strip()
|
||||
if not matching_id:
|
||||
raise RuntimeError("missing_transfer_matching_id")
|
||||
execute_matching_items(
|
||||
matching_id=matching_id,
|
||||
run_id = str(payload.get("run_id") or "").strip()
|
||||
if not run_id:
|
||||
raise RuntimeError("missing_transfer_run_id")
|
||||
doc_id = str(payload.get("doc_id") or "").strip()
|
||||
# 领取代次快照:finish_job 条件 UPDATE 需与领取时一致,防止超时重领后错收新代次。
|
||||
job_row = db.session.get(Job, int(payload["job_id"])) if payload.get("job_id") else None
|
||||
owned_attempt = int(job_row.attempts) if job_row is not None else None
|
||||
status = execute_run(
|
||||
run_id=int(run_id),
|
||||
deps=transfer_deps.get_transfer_dependencies(),
|
||||
doc_id=int(doc_id) if doc_id else None,
|
||||
skip_write_recheck=bool(payload.get("skip_write_recheck")),
|
||||
)
|
||||
if job_row is not None:
|
||||
finish_job(
|
||||
int(job_row.id),
|
||||
message=f"调拨单执行完成:run_id={run_id} status={status}",
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -730,10 +730,25 @@ def _is_transient_lock_error(exc: BaseException) -> bool:
|
||||
|
||||
【AI 触发时机】:订单抽取 Job 异常收口时决定是否标记可重试。此类错误重试即可恢复,
|
||||
按终态失败收口会触发 run failed 并要求人工重新识别(2026-09-22 生产事故两次)。
|
||||
SQLAlchemy 将 DBAPI 错误包装为 exc.orig;非 DBAPI 异常(无 pgcode)一律返回 False。
|
||||
|
||||
需要穿透两层包装:①SQLAlchemy 将 DBAPI 错误挂在 exc.orig;②若失败后同一事务里
|
||||
还有后续语句,SQLAlchemy 会改抛 PendingRollbackError(无 .orig,原文内嵌在 message),
|
||||
或经 __cause__ 链包装。沿 orig/__cause__ 链遍历并对消息做特征匹配,确保包装后的
|
||||
死锁仍被判为瞬时(2026-09-24 test job 8840:同类死锁因分类漏判而一次判死)。
|
||||
"""
|
||||
orig = getattr(exc, 'orig', None)
|
||||
return getattr(orig, 'pgcode', None) in {'40P01', '55P03'}
|
||||
pgcode_set = {'40P01', '55P03'}
|
||||
seen: set[int] = set()
|
||||
current: BaseException | None = exc
|
||||
while current is not None and id(current) not in seen and len(seen) < 6:
|
||||
seen.add(id(current))
|
||||
orig = getattr(current, 'orig', None)
|
||||
if getattr(orig, 'pgcode', None) in pgcode_set:
|
||||
return True
|
||||
text = str(current)
|
||||
if 'DeadlockDetected' in text or 'deadlock detected' in text or '40P01' in text or '55P03' in text:
|
||||
return True
|
||||
current = getattr(current, 'orig', None) or getattr(current, '__cause__', None)
|
||||
return False
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_ORDER_EXTRACTION)
|
||||
@@ -929,15 +944,13 @@ 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 仍须独立触发下游(幂等)。
|
||||
# [交接不变量] defer_stp_enqueue:开工快照仍有待排版附件(本 Job 必交接)时
|
||||
# 推迟 STP 入队到 run 收口,确保 STP 只由最后一个抽取 Job 提交;否则本 Job
|
||||
# 提交的 STP Job 会与续抽 Job 并发执行,双方以相反顺序抢 run/intake 行锁
|
||||
# 形成结构性死锁(2026-09-22 SNER-20260922-6466EB53 生产事故)。
|
||||
# 推送失败必须冒泡(与下方新单元推送同一规则):此处是这些 Unit 候选与
|
||||
# 复核 Case 的唯一物化触发点,吞掉等于静默丢单。
|
||||
# [收口不变量] 推送只物化 Candidate/Review Case,STP 统一由 run 收口
|
||||
# (submit_order_extraction_run_result)入队——STP Job 可见时抽取 Job
|
||||
# 必已提交完毕,结构性消除抽取∥STP 并发死锁(2026-09-22/24 事故族)。
|
||||
# 推送失败必须冒泡:此处是这些 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':
|
||||
@@ -1208,11 +1221,10 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
apply_worker_cancellation_checkpoint(run=run)
|
||||
db.session.commit()
|
||||
continue
|
||||
# [交接不变量] 同 replay 路径:本 Job 仍会交接时推迟 STP 入队到 run 收口。
|
||||
# [收口不变量] 同 replay 路径:推送只物化候选与复核 Case,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,
|
||||
@@ -1226,6 +1238,10 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
job_id=job.id,
|
||||
)
|
||||
except Exception as unit_exc:
|
||||
# [STEP] 先回滚再记录:推送失败时事务可能已中止(如死锁),直接写进度会抛
|
||||
# PendingRollbackError 顶掉原始异常(2026-09-24 test job 8840)。回滚后在新
|
||||
# 事务记录单元技术失败,再抛出原始异常交外层收口(瞬时锁错由其自动重排)。
|
||||
db.session.rollback()
|
||||
record_unit_progress(
|
||||
run=run,
|
||||
unit=unit,
|
||||
|
||||
Reference in new issue
Block a user