test(shipping): 更新订单复核库存与通知流程测试
This commit is contained in:
1 parent
8ec028e6a3
commit
265a6e179a
168 files changed
+9420
-4613
No files matched your search
@@ -16,9 +16,9 @@ from datetime import datetime
|
||||
from sqlalchemy import and_, or_, update
|
||||
|
||||
from app import db
|
||||
from app.core.errors import AppError, NotFoundError
|
||||
from app.core.errors import NotFoundError
|
||||
from app.jobs.context import JobContext
|
||||
from app.jobs.queue import fail_job, finish_job
|
||||
from app.jobs.queue import _is_retryable_exc, fail_job, finish_job
|
||||
from app.jobs.registry import register_handler
|
||||
from app.jobs.types import JobType
|
||||
from app.models import FileAsset, Job, ShippingOrderIntake
|
||||
@@ -120,15 +120,6 @@ def _claim_intake_for_parsing(
|
||||
return intake, lease
|
||||
|
||||
|
||||
def _is_retryable_exc(exc: BaseException) -> bool:
|
||||
"""业务 NotFound / AppError.retryable=False 视为不可再排队。"""
|
||||
if isinstance(exc, NotFoundError):
|
||||
return False
|
||||
if isinstance(exc, AppError):
|
||||
return bool(getattr(exc, 'retryable', False))
|
||||
return True
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_NOTICE_PARSE)
|
||||
def run_shipping_notice_parse(ctx: JobContext, payload: dict) -> None:
|
||||
"""
|
||||
@@ -264,7 +255,9 @@ def run_shipping_notice_parse(ctx: JobContext, payload: dict) -> None:
|
||||
# 租约已失效:旧 Worker 不得触碰 Job(条件 UPDATE 亦会 no-op,此处直接返回)
|
||||
if str(fail_result.get('reason') or '') == 'stale_parse_lease':
|
||||
return
|
||||
retryable = False
|
||||
# 失败收口按异常类型分级:业务失败(NotFound/AppError.retryable=False)停排,
|
||||
# 瞬时基础设施异常才允许重试;不按业务域一刀切。
|
||||
retryable = _is_retryable_exc(exc)
|
||||
if job_id is not None:
|
||||
fail_job(
|
||||
job_id,
|
||||
@@ -272,3 +265,7 @@ def run_shipping_notice_parse(ctx: JobContext, payload: dict) -> None:
|
||||
retryable=retryable,
|
||||
owned_attempt=owned_attempt,
|
||||
)
|
||||
# 重试类异常交由外层运行时统一收口(fail_job 已 requeue);非重试业务失败已直接
|
||||
# fail_job 为 failed,不再重抛,避免向上滚动。
|
||||
if retryable:
|
||||
raise
|
||||
@@ -10,7 +10,8 @@ backend/app/jobs/handlers/shipping_stp_auto_create.py
|
||||
逻辑:由 shipping worker 领取;幂等键保证同一 Case/draft 不重复建单;
|
||||
成功 finish,附加 task_no / idempotent_replay;
|
||||
失败统一走 _classify_stp_result:
|
||||
- 所有失败 → retryable=false(blocked_manual + 注入 next_action + 写阻断审计)
|
||||
- 业务阻断 → retryable=false(blocked_manual + 注入 next_action + 写阻断审计)
|
||||
- 临时基础设施异常 → retryable=true(保留普通重试入口)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -51,11 +52,10 @@ def _classify_stp_job_result(
|
||||
【AI 触发时机】:Handler 的三条 fail 路径(业务拒绝 ok=False、AppError 兜底、
|
||||
非预期 Exception 兜底)统一调用,确保 Job.result_json schema 一致。
|
||||
|
||||
核心逻辑(fail-closed):
|
||||
[STEP 1] build_stp_job_blocked_manual_result:
|
||||
derive_blocking_next_actions 取第一个动作作 next_action,默认 open_order_review;
|
||||
details 合并至 blocking_reason_details。
|
||||
[STEP 2] 返回三元组(False, normalized_result_dict, next_action)。
|
||||
核心逻辑:
|
||||
[STEP 1] temporary_storage_error 走技术失败契约,允许普通重试;
|
||||
[STEP 2] 其他错误走 build_stp_job_blocked_manual_result,要求人工裁决;
|
||||
[STEP 3] 返回三元组(retryable, normalized_result_dict, next_action)。
|
||||
|
||||
Args:
|
||||
error_code: 业务错误码(SHIPPING.* 或顶层 reason_code)。
|
||||
@@ -71,6 +71,23 @@ def _classify_stp_job_result(
|
||||
# 当前执行已停止并应在当前 Gate 选择动作,原文仅保留在 Worker 日志。
|
||||
if str(error_code or '').strip() == 'SHIPPING.STP_AUTO_CREATE_UNEXPECTED':
|
||||
message = '自动建单失败,已停止,等待用户选择下一步动作'
|
||||
if category_hint == 'temporary_storage_error':
|
||||
next_action = 'retry_stp_auto_create'
|
||||
return True, {
|
||||
'status': 'failed',
|
||||
'retryable': True,
|
||||
'error_code': str(error_code or '').strip(),
|
||||
'message': str(message or ''),
|
||||
'next_action': next_action,
|
||||
'contract_version': 'shipping-recovery-v1-phase1',
|
||||
'decision_trace': {
|
||||
'contract_version': 'shipping-recovery-v1-phase1',
|
||||
'stage': 'stp_classify_error',
|
||||
'retryable': True,
|
||||
'matched_rule': 'temporary_storage_error',
|
||||
'category_hint': category_hint,
|
||||
},
|
||||
}, next_action
|
||||
actions = derive_blocking_next_actions(blocking_reason=error_code)
|
||||
next_action = actions[0] if actions else 'open_order_review'
|
||||
blocked: dict[str, Any] = build_stp_job_blocked_manual_result(
|
||||
|
||||
Reference in new issue
Block a user