feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
84a881b554
commit
74270adc22
149 files changed
+20111
-3871
No files matched your search
@@ -15,5 +15,7 @@ from app.jobs.handlers import ( # noqa: F401
|
||||
shipping_notice_run,
|
||||
shipping_order_extraction,
|
||||
shipping_process_gate,
|
||||
shipping_stp_auto_create,
|
||||
shipping_xfa_ocr_automark,
|
||||
wecom_directory_sync,
|
||||
)
|
||||
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
backend/app/jobs/handlers/shipping_stp_auto_create.py
|
||||
|
||||
用途:消费 STP 自动建单 Job,执行 confirm_review_case 与确认后自动推进入队。
|
||||
依赖:JobContext、order_stp_auto_create_service.execute_stp_auto_create_job、
|
||||
jobs.queue finish/fail。
|
||||
逻辑:由 shipping worker 领取;幂等键保证同一 Case/draft 不重复建单;
|
||||
失败标记 Job failed,供 Projection 显示 retryable。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.jobs.context import JobContext
|
||||
from app.jobs.queue import fail_job, finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.services.shipping_notice.order_stp_auto_create_service import (
|
||||
execute_stp_auto_create_job,
|
||||
)
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_STP_AUTO_CREATE)
|
||||
def handle_shipping_stp_auto_create(ctx: JobContext) -> None:
|
||||
"""
|
||||
执行 STP 自动建单 Job。
|
||||
|
||||
核心逻辑:委托 execute_stp_auto_create_job;成功 finish,业务拒绝/异常 fail。
|
||||
"""
|
||||
payload = ctx.payload if isinstance(ctx.payload, dict) else {}
|
||||
try:
|
||||
result = execute_stp_auto_create_job(payload=payload, job_id=int(ctx.job_id))
|
||||
if result.get('ok'):
|
||||
finish_job(int(ctx.job_id), message=str(result.get('message') or 'STP 自动建单完成'))
|
||||
return
|
||||
fail_job(
|
||||
int(ctx.job_id),
|
||||
error_code=str(result.get('error_code') or 'STP_AUTO_CREATE_FAILED'),
|
||||
message=str(result.get('message') or 'STP 自动建单失败'),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
fail_job(
|
||||
int(ctx.job_id),
|
||||
error_code='STP_AUTO_CREATE_EXCEPTION',
|
||||
message=str(exc)[:500],
|
||||
)
|
||||
raise
|
||||
@@ -0,0 +1,82 @@
|
||||
"""
|
||||
backend/app/jobs/handlers/shipping_xfa_ocr_automark.py
|
||||
|
||||
用途:异步执行 XFA OCR 自动标记(原生 PDF/PDFium 页图),产出 suggested visual_bindings(不自动 confirmed)。
|
||||
依赖:Job queue、order_document_profile_xfa_automark_service(执行体)、JobType。
|
||||
逻辑:由 shipping worker 领取;payload 含 profile_id/document_id/preview_render_checksum;
|
||||
不再依赖 /xfa/preview synthetic;空白壳返回空建议成功;OCR 不可用时 fail_job。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.extensions import db
|
||||
from app.jobs.context import JobContext
|
||||
from app.jobs.queue import fail_job, finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import Job
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_XFA_OCR_AUTOMARK)
|
||||
def run_shipping_xfa_ocr_automark(ctx: JobContext, payload: dict) -> None:
|
||||
"""
|
||||
执行 XFA OCR 自动标记 Job。
|
||||
|
||||
核心逻辑:读取冻结 Job 载荷 → 委托 automark_service.execute_xfa_ocr_automark_job →
|
||||
finish/fail。OCR 引擎不可用时不可重试(明确业务码)。
|
||||
|
||||
Args:
|
||||
ctx: Worker 上下文(堆栈仅运维可见)。
|
||||
payload: 须含 job_id;业务字段在 Job.payload_json。
|
||||
|
||||
Returns:
|
||||
None。
|
||||
"""
|
||||
_ = ctx
|
||||
raw_job_id = payload.get('job_id')
|
||||
job = db.session.get(Job, int(raw_job_id)) if raw_job_id else None
|
||||
if job is None:
|
||||
return
|
||||
|
||||
owned_attempt = int(job.attempts or 0)
|
||||
frozen = dict(job.payload_json or {})
|
||||
frozen.setdefault('job_id', int(job.id))
|
||||
|
||||
from app.services.shipping_notice import order_document_profile_xfa_automark_service as automark_service
|
||||
|
||||
try:
|
||||
result = automark_service.execute_xfa_ocr_automark_job(frozen)
|
||||
except automark_service.XfaOcrAutomarkError as exc:
|
||||
fail_job(
|
||||
job.id,
|
||||
error=str(exc.message or exc)[:500],
|
||||
result={
|
||||
'status': 'failed',
|
||||
'error_code': exc.error_code,
|
||||
'preview_render_checksum': frozen.get('preview_render_checksum'),
|
||||
'details': dict(exc.details or {}),
|
||||
},
|
||||
retryable=bool(exc.retryable),
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001 — 未知异常走可重试失败
|
||||
fail_job(
|
||||
job.id,
|
||||
error=str(exc)[:500],
|
||||
result={
|
||||
'status': 'failed',
|
||||
'error_code': 'SHIPPING.ORDER_DOCUMENT_PROFILE_XFA_OCR_AUTOMARK_FAILED',
|
||||
'preview_render_checksum': frozen.get('preview_render_checksum'),
|
||||
},
|
||||
retryable=True,
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
return
|
||||
|
||||
finish_job(
|
||||
job.id,
|
||||
result=result,
|
||||
message='XFA OCR automark completed',
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
@@ -20,6 +20,10 @@ class JobType(StrEnum):
|
||||
SHIPPING_ORDER_EXTRACTION = 'shipping_order_extraction'
|
||||
SHIPPING_DOCUMENT_LAYOUT = 'shipping_document_layout'
|
||||
SHIPPING_PROCESS_GATE = 'shipping_process_gate'
|
||||
# XFA 合成预览就绪后的 OCR 自动标记(Job.job_type 为字符串列,无需 DB enum 迁移)
|
||||
SHIPPING_XFA_OCR_AUTOMARK = 'shipping_xfa_ocr_automark'
|
||||
# 资格达标后的 STP 自动建单入队(先 queued,再由 worker 执行 confirm)
|
||||
SHIPPING_STP_AUTO_CREATE = 'shipping_stp_auto_create'
|
||||
SCHEDULE_RUN = 'schedule_run'
|
||||
FILE_CHECK_RUN = 'file_check_run'
|
||||
WECOM_DIRECTORY_SYNC = 'wecom_directory_sync'
|
||||
@@ -55,7 +59,8 @@ QUEUE_KB_JOB_TYPES: dict[str, frozenset[str]] = {
|
||||
'kb': frozenset({'clean', 'path_upload', 'sync_dify'}),
|
||||
'shipping': frozenset({
|
||||
'shipping_notice_run', 'shipping_notice_parse', 'shipping_order_extraction',
|
||||
'shipping_document_layout', 'shipping_process_gate',
|
||||
'shipping_document_layout', 'shipping_process_gate', 'shipping_xfa_ocr_automark',
|
||||
'shipping_stp_auto_create',
|
||||
}),
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user