Merge branch 'hugo-shipping-notice' into test

This commit is contained in:
hugo committed 2026-09-24 17:06:33 +08:00
commit 387b3157fe
113 files changed
+6036 -1759

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ from app.jobs.handlers import ( # noqa: F401
contract_payment,
file_check_run,
in_transit_transfer_item,
in_transit_transfer_precheck,
in_transit_transfer_match,
in_transit_transfer_run,
kb_clean,
kb_dify_sync,
@@ -0,0 +1,56 @@
"""backend/app/jobs/handlers/in_transit_transfer_match.py
用途:异步执行在途调拨匹配(文件准备 + 只读匹配),并更新匹配状态。
依赖:Job 注册表、matching_service、transfer 模型、transfer deps。
逻辑:领取后按 payload.matching_id 加载匹配,运行 run_matching 并回写
状态/计数/Hash;匹配不存在时 fail-closed 抛 RuntimeError。
"""
from __future__ import annotations
from app import db
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.models import TransferMatching
from app.services.in_transit_transfer import deps as transfer_deps
from app.services.in_transit_transfer.matching_service import run_matching
@register_handler(JobType.TRANSFER_MATCH)
def run_in_transit_transfer_match(_ctx, payload: dict) -> None:
"""执行一次匹配 Job 并更新匹配状态。
【AI 触发时机】:仅由匹配创建异步路径入队;页面直连走 API 同步路径。
"""
payload = payload if isinstance(payload, dict) else {}
matching_no = str(payload.get("matching_id") or "").strip()
if not matching_no:
raise RuntimeError("missing_transfer_matching_id")
matching = db.session.query(TransferMatching).filter_by(matching_no=matching_no).first()
if matching is None:
raise RuntimeError(f"missing_transfer_matching: {matching_no}")
deps = transfer_deps.get_transfer_dependencies()
scope = matching.scope_json if isinstance(matching.scope_json, dict) else {}
source_codes = list(scope.get("source_codes") or [])
result = run_matching(
source_codes=source_codes,
business_date=matching.business_date,
deps=deps,
run_no=matching.run.run_no if matching.run else "RUN-PENDING",
# 类型范围随匹配落库回读:异步重跑与创建时口径一致(缺省回落类型1)。
transfer_type=str(scope.get("transfer_type")) if scope.get("transfer_type") else None,
)
summary = result.summary
matching.status = summary.status
matching.counts_json = {
"file_invoice_count": summary.counts.file_invoice_count,
"not_matured_count": summary.counts.not_matured_count,
"matched_count": summary.counts.matched_count,
"executable_count": summary.counts.executable_count,
"blocked_count": summary.counts.blocked_count,
}
matching.estimated_transfer_quantity = summary.estimated_transfer_quantity
matching.estimated_external_write_count = summary.estimated_external_write_count
matching.matching_hash = summary.matching_hash
db.session.commit()
@@ -1,56 +0,0 @@
"""backend/app/jobs/handlers/in_transit_transfer_precheck.py
用途:异步执行在途调拨预检(文件准备 + 只读预检),并更新预检状态。
依赖:Job 注册表、precheck_service、transfer 模型、transfer deps。
逻辑:领取后按 payload.precheck_id 加载预检,运行 run_precheck 并回写
状态/计数/Hash;预检不存在时 fail-closed 抛 RuntimeError。
"""
from __future__ import annotations
from app import db
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.models import TransferPrecheck
from app.services.in_transit_transfer import deps as transfer_deps
from app.services.in_transit_transfer.precheck_service import run_precheck
@register_handler(JobType.TRANSFER_PRECHECK)
def run_in_transit_transfer_precheck(_ctx, payload: dict) -> None:
"""执行一次预检 Job 并更新预检状态。
【AI 触发时机】:仅由预检创建异步路径入队;页面直连走 API 同步路径。
"""
payload = payload if isinstance(payload, dict) else {}
precheck_no = str(payload.get("precheck_id") or "").strip()
if not precheck_no:
raise RuntimeError("missing_transfer_precheck_id")
precheck = db.session.query(TransferPrecheck).filter_by(precheck_no=precheck_no).first()
if precheck is None:
raise RuntimeError(f"missing_transfer_precheck: {precheck_no}")
deps = transfer_deps.get_transfer_dependencies()
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
precheck.counts_json = {
"file_invoice_count": summary.counts.file_invoice_count,
"not_matured_count": summary.counts.not_matured_count,
"prechecked_count": summary.counts.prechecked_count,
"executable_count": summary.counts.executable_count,
"blocked_count": summary.counts.blocked_count,
}
precheck.estimated_transfer_quantity = summary.estimated_transfer_quantity
precheck.estimated_external_write_count = summary.estimated_external_write_count
precheck.precheck_hash = summary.precheck_hash
db.session.commit()
@@ -1,8 +1,9 @@
"""backend/app/jobs/handlers/in_transit_transfer_run.py
用途:Run 级执行 Job:加载预检并驱动执行服务完成全部可执行项写入。
用途:Run 级执行 Job:按匹配 scope 圈定 ready 行并驱动按行执行服务完成写入。
依赖:execution_service、transfer deps、Job 注册表。
逻辑:payload 携带 precheck_id;执行后预检与 Run 落库终态。
逻辑:payload 携带 matching_id(仅用于 scope 圈行);三层门禁由 execute_items
统一裁定,门禁失败即入队路径失败(fail-closed)。
"""
from __future__ import annotations
@@ -10,20 +11,20 @@ from __future__ import annotations
from app.jobs.registry import register_handler
from app.jobs.types import JobType
from app.services.in_transit_transfer import deps as transfer_deps
from app.services.in_transit_transfer.execution_service import execute_precheck
from app.services.in_transit_transfer.execution_service import execute_matching_items
@register_handler(JobType.TRANSFER_RUN)
def run_in_transit_transfer_run(_ctx, payload: dict) -> None:
"""执行一个 Run(异步路径)。
"""执行匹配 scope 内全部可执行行(异步路径)。
【AI 触发时机】:仅由执行 Run 入队路径调用;页面直连走 API 同步路径。
"""
payload = payload if isinstance(payload, dict) else {}
precheck_id = str(payload.get("precheck_id") or "").strip()
if not precheck_id:
raise RuntimeError("missing_transfer_precheck_id")
execute_precheck(
precheck_id=precheck_id,
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,
deps=transfer_deps.get_transfer_dependencies(),
)
@@ -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)
+3 -3
View File
@@ -39,8 +39,8 @@ class JobType(StrEnum):
CONTRACT_PAYMENT_SYNC = 'contract_payment_sync'
CONTRACT_PAYMENT_ALERT = 'contract_payment_alert'
CONTRACT_PAYMENT_ALERT_WEEKLY = 'contract_payment_alert_weekly'
# 在途仓自动调拨:文件准备 + 只读预检(Phase 1);执行 Run/Item 在 Phase 2 注册。
TRANSFER_PRECHECK = 'in_transit_transfer_precheck'
# 在途仓自动调拨:文件准备 + 只读匹配(Phase 1);执行 Run/Item 在 Phase 2 注册。
TRANSFER_MATCH = 'in_transit_transfer_match'
TRANSFER_RUN = 'in_transit_transfer_run'
TRANSFER_ITEM = 'in_transit_transfer_item'
@@ -83,7 +83,7 @@ QUEUE_KB_JOB_TYPES: dict[str, frozenset[str]] = {
'contract': frozenset({'procurement_contract_review', 'contract_payment_sync', 'contract_payment_alert', 'contract_payment_alert_weekly'}),
'schedule': frozenset({'schedule_run', 'file_check_run', 'wecom_directory_sync'}),
'transfer': frozenset({
'in_transit_transfer_precheck', 'in_transit_transfer_run', 'in_transit_transfer_item',
'in_transit_transfer_match', 'in_transit_transfer_run', 'in_transit_transfer_item',
}),
}