feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
backend/app/jobs/handlers/shipping_document_layout.py
|
||||
|
||||
用途:独立执行单份 ShippingNoticeDocument 的 MinerU 布局解析任务。
|
||||
用途:独立执行单份 ShippingOrderDocument 的 MinerU 布局解析任务。
|
||||
依赖:Layout lifecycle/runtime、FileAsset、通用 Job queue、audit_service、workflow_ledger(尽力引用)。
|
||||
逻辑:按 job attempt 领取版本 lease,读取对象存储调用 MinerU;收口时旧 lease 无权覆盖新版本;
|
||||
起止与异常轻量写统一审计(若能定位 WorkflowItem 则带软引用)。
|
||||
@@ -144,13 +144,75 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
if job.started_at and job.created_at else 0,
|
||||
'asset_read_ms': round((perf_counter() - asset_started) * 1000),
|
||||
}
|
||||
snapshot = build_mineru_table_snapshot(
|
||||
filename=str(document.filename or asset.original_filename or 'document.pdf'),
|
||||
content=content,
|
||||
document_id=document.id,
|
||||
content_hash=document.content_hash,
|
||||
timings=timings,
|
||||
from app.services.shipping_notice.document_layout.xfa_analysis import (
|
||||
analyze_xfa_pdf,
|
||||
persist_xfa_structure_analysis,
|
||||
)
|
||||
from app.services.shipping_notice.document_layout.xfa_contract import default_xfa_resource_limits
|
||||
|
||||
xfa_started = perf_counter()
|
||||
xfa_outcome = None
|
||||
if str(document.filename or '').lower().endswith('.pdf') and content.startswith(b'%PDF'):
|
||||
_layout_job_audit(
|
||||
event_type='xfa_parse_started',
|
||||
job=job,
|
||||
version=version,
|
||||
metadata={'document_id': int(document.id)},
|
||||
)
|
||||
xfa_outcome = analyze_xfa_pdf(content, limits=default_xfa_resource_limits())
|
||||
timings['xfa_analysis_ms'] = round((perf_counter() - xfa_started) * 1000)
|
||||
|
||||
snapshot = None
|
||||
if (
|
||||
xfa_outcome is not None
|
||||
and xfa_outcome.packet_result is not None
|
||||
and xfa_outcome.packet_result.document_kind not in {'plain_pdf', 'acroform'}
|
||||
):
|
||||
snapshot = dict(xfa_outcome.snapshot)
|
||||
persist_xfa_structure_analysis(
|
||||
document_id=int(document.id),
|
||||
layout_version_id=int(version.id),
|
||||
source_content_hash=str(document.content_hash or ''),
|
||||
outcome=xfa_outcome,
|
||||
)
|
||||
if xfa_outcome.error_code == 'XFA_UNSAFE_XML':
|
||||
_layout_job_audit(
|
||||
event_type='xfa_parse_rejected_unsafe',
|
||||
job=job,
|
||||
version=version,
|
||||
result='failure',
|
||||
error_code=xfa_outcome.error_code,
|
||||
)
|
||||
elif snapshot.get('status') == 'ready':
|
||||
_layout_job_audit(
|
||||
event_type='xfa_structure_analysis_ready',
|
||||
job=job,
|
||||
version=version,
|
||||
metadata={
|
||||
'document_kind': snapshot.get('document_kind'),
|
||||
'structure_analysis_id': (
|
||||
(snapshot.get('xfa_capability') or {}).get('structure_analysis_id')
|
||||
),
|
||||
},
|
||||
)
|
||||
_layout_job_audit(event_type='xfa_parse_completed', job=job, version=version)
|
||||
else:
|
||||
_layout_job_audit(
|
||||
event_type='xfa_parse_failed',
|
||||
job=job,
|
||||
version=version,
|
||||
result='failure',
|
||||
error_code=xfa_outcome.error_code,
|
||||
metadata={'route_reason': xfa_outcome.route_reason},
|
||||
)
|
||||
if snapshot is None:
|
||||
snapshot = build_mineru_table_snapshot(
|
||||
filename=str(document.filename or asset.original_filename or 'document.pdf'),
|
||||
content=content,
|
||||
document_id=document.id,
|
||||
content_hash=document.content_hash,
|
||||
timings=timings,
|
||||
)
|
||||
timings['snapshot_build_ms'] = round((perf_counter() - total_started) * 1000)
|
||||
if snapshot is None:
|
||||
snapshot = {
|
||||
@@ -170,7 +232,8 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
)
|
||||
finish_job(
|
||||
job.id, result={
|
||||
'status': str(snapshot.get('status') or 'failed'),
|
||||
'status': final_status,
|
||||
'provider_status': str(snapshot.get('status') or 'failed'),
|
||||
'timings': {key: int(value) for key, value in timings.items()},
|
||||
},
|
||||
message=(
|
||||
@@ -197,7 +260,7 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
event_type='document_layout_job_failed',
|
||||
job=job,
|
||||
version=version,
|
||||
result='error',
|
||||
result='failure',
|
||||
error_code=LAYOUT_JOB_ERROR,
|
||||
metadata={'error': str(exc)[:200]},
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user