feat(in-transit-transfer): inbound/outbound 幂等重命名与解析用途分层
- receipt→inbound 三件套改名(回调桥/文件门禁/触发服务),回调链幂等去重,迁移 tt14 同步台账键与测试基建 - 库存解析用途分层 onhand/inbound/outbound(迁移 tt15):解析档案、文件存储与下载名按用途隔离,管理端表单同步 - 「成熟」概念退役(迁移 tt16):数据无成熟属性,日期边界由调用方入参严格判定 - backlog 新增重解析服务,在途重识别投影 processing,EML 父项不再停留处理中 - 收料 RPA 回调/归档/排程链路同步改名挂点,运维脚本复位假完成状态 - 文档随批:ADR 0003 模块边界决策、10-15/10-16 计划与设计、调拨文档族与 demand/tec/funclist - 同步 chat/errors 文案四语言、调拨前端卡片与 e2e、测试全家
This commit is contained in:
1 parent
43dceaf027
commit
8102a1fe44
151 files changed
+8057
-2382
No files matched your search
@@ -1,9 +1,11 @@
|
||||
"""
|
||||
backend/app/services/schedule/freight_receipt_rpa_finalize.py
|
||||
|
||||
用途:独立收料 RPA 排程等待态收口——进度回写、全单元终态后写计数、预生成 ZIP、发企微。
|
||||
用途:独立收料 RPA 排程等待态收口——进度回写、全单元终态后写计数、预生成 ZIP、发企微;
|
||||
另含超时对账:排程 run 级(每轮 tick)与按需触发孤儿单元级(schedule_run_id=NULL)。
|
||||
依赖:ShippingReceiptRpaUnitRun、shipping_receipt_rpa_archive、scheduler_service 企微通知。
|
||||
逻辑:每笔回调 → refresh 进度计数 → 全终态时 persist ZIP → _notify_schedule_run_wecom。
|
||||
逻辑:每笔回调 → refresh 进度计数 → 全终态时 persist ZIP → _notify_schedule_run_wecom;
|
||||
超时对账只终态化失败,绝不自动重试或重发(恢复走人工显式重试入口)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -12,6 +14,7 @@ from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import func
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask
|
||||
@@ -394,6 +397,38 @@ def _receipt_last_activity(run: ScheduleRun, units: list[ShippingReceiptRpaUnitR
|
||||
return max(stamps)
|
||||
|
||||
|
||||
def _fail_receipt_unit_with_timeout(
|
||||
unit: ShippingReceiptRpaUnitRun, *, now: datetime, timeout_seconds: int,
|
||||
run_no: str | None = None,
|
||||
) -> None:
|
||||
"""单个收料单元回调超时收口为失败并写审计(run 级与孤儿级回收共用的唯一实现)。
|
||||
|
||||
核心逻辑:错误码/错误文案/审计事件三者的单一事实来源——run 级扫描与孤儿
|
||||
单元扫描共用本函数收口单元,防止两条路径语义漂移。只改单元不 commit,
|
||||
事务归属调用方。
|
||||
|
||||
Args:
|
||||
unit: 待收口的非终态收料单元。
|
||||
now: 收口时刻(同批单元共享同一时间戳)。
|
||||
timeout_seconds: 超时窗(写入错误文案与审计 metadata)。
|
||||
run_no: 所属排程 run 号(run 级回收传实;孤儿单元无排程,保持 None)。
|
||||
Returns:
|
||||
None。
|
||||
"""
|
||||
before = {'status': unit.status, 'error_code': unit.error_code, 'error_message': unit.error_message}
|
||||
unit.status = 'failed'
|
||||
unit.error_code = 'RPA.RECEIPT_CALLBACK_TIMEOUT'
|
||||
unit.error_message = f'Receipt RPA callback timed out after {timeout_seconds} seconds'
|
||||
unit.finished_at = now
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_receipt_rpa_callback_timeout', aggregate_type='shipping_receipt_unit',
|
||||
aggregate_id=str(unit.id), run_no=run_no, actor_type='worker', actor_id='schedule-recovery',
|
||||
unit_id=unit.id, result='failure', before=before,
|
||||
after={'status': unit.status, 'error_code': unit.error_code, 'error_message': unit.error_message},
|
||||
metadata={'timeout_seconds': timeout_seconds},
|
||||
)
|
||||
|
||||
|
||||
def _recover_stale_receipt_run(run: ScheduleRun, *, cutoff: datetime, timeout_seconds: int) -> bool:
|
||||
"""将无回调的独立收料运行收口为失败,保留其单元与触发审计事实。
|
||||
|
||||
@@ -412,18 +447,7 @@ def _recover_stale_receipt_run(run: ScheduleRun, *, cutoff: datetime, timeout_se
|
||||
now = datetime.utcnow()
|
||||
pending = [unit for unit in units if unit.status in {'queued', 'awaiting_rpa', 'processing'}]
|
||||
for unit in pending:
|
||||
before = {'status': unit.status, 'error_code': unit.error_code, 'error_message': unit.error_message}
|
||||
unit.status = 'failed'
|
||||
unit.error_code = 'RPA.RECEIPT_CALLBACK_TIMEOUT'
|
||||
unit.error_message = f'Receipt RPA callback timed out after {timeout_seconds} seconds'
|
||||
unit.finished_at = now
|
||||
append_shipping_audit_event(
|
||||
event_type='shipping_receipt_rpa_callback_timeout', aggregate_type='shipping_receipt_unit',
|
||||
aggregate_id=str(unit.id), run_no=run.run_no, actor_type='worker', actor_id='schedule-recovery',
|
||||
unit_id=unit.id, result='failure', before=before,
|
||||
after={'status': unit.status, 'error_code': unit.error_code, 'error_message': unit.error_message},
|
||||
metadata={'timeout_seconds': timeout_seconds},
|
||||
)
|
||||
_fail_receipt_unit_with_timeout(unit, now=now, timeout_seconds=timeout_seconds, run_no=run.run_no)
|
||||
run.status = 'failed'
|
||||
run.finished_at = now
|
||||
run.duration_ms = int((now - run.started_at).total_seconds() * 1000)
|
||||
@@ -463,3 +487,46 @@ def recover_all_stale_receipt_rpa_schedule_runs() -> int:
|
||||
if recovered:
|
||||
db.session.commit()
|
||||
return recovered
|
||||
|
||||
|
||||
def recover_all_stale_orphan_receipt_units() -> int:
|
||||
"""回收按需触发(无排程绑定)的超时收料单元,解除来源投影永久 fetching。
|
||||
|
||||
【AI 触发时机】仅由排程队列每轮领取维护(queue_service)调用。调拨工作台
|
||||
重试/自动调拨补抓创建的单元 schedule_run_id 为 NULL(见 inbound_trigger_service),
|
||||
不在 run 级扫描(recover_all_stale_receipt_rpa_schedule_runs 按 run 遍历)范围内;
|
||||
Worker 网页异常关闭后回调永不到达,若不回收则来源投影(source_file_status)
|
||||
永久 fetching、前端重试按钮永久锁死(2026-10-16 事故根因)。
|
||||
|
||||
核心逻辑:[STEP 1] 扫 schedule_run_id IS NULL、状态非终态、活动锚
|
||||
(coalesce(started_at, created_at):与 run 级同为活动锚思想,单元未被领取
|
||||
无 started_at 时以 created_at 兜底;注意 run 级 _receipt_last_activity 是
|
||||
笔间静默口径、刻意不含 created_at,两者语义不同)超过
|
||||
FREIGHT_RPA_RECEIPT_TIMEOUT_SECONDS 的单元;admin 单元重试(retry_receipt_rpa_unit)
|
||||
原样复制 schedule_run_id,其 retry 单元同为孤儿,一并覆盖。
|
||||
[STEP 2] 逐单元经 _fail_receipt_unit_with_timeout 收口(failed +
|
||||
RPA.RECEIPT_CALLBACK_TIMEOUT + 审计,与 run 级扫描同一实现零漂移);
|
||||
绝不自动重试或重发——恢复走调拨工作台显式「重试 RPA」
|
||||
(与 recover_stale_waiting_source_jobs 口径一致)。
|
||||
|
||||
Returns:
|
||||
本次回收的单元数。
|
||||
"""
|
||||
timeout_seconds = _receipt_timeout_seconds()
|
||||
cutoff = datetime.utcnow() - timedelta(seconds=timeout_seconds)
|
||||
orphans = (
|
||||
ShippingReceiptRpaUnitRun.query
|
||||
.filter(
|
||||
ShippingReceiptRpaUnitRun.schedule_run_id.is_(None),
|
||||
ShippingReceiptRpaUnitRun.status.in_(('queued', 'awaiting_rpa', 'processing')),
|
||||
func.coalesce(ShippingReceiptRpaUnitRun.started_at, ShippingReceiptRpaUnitRun.created_at) < cutoff,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
if not orphans:
|
||||
return 0
|
||||
now = datetime.utcnow()
|
||||
for unit in orphans:
|
||||
_fail_receipt_unit_with_timeout(unit, now=now, timeout_seconds=timeout_seconds)
|
||||
db.session.commit()
|
||||
return len(orphans)
|
||||
@@ -1,15 +1,16 @@
|
||||
"""自动调拨排程。
|
||||
|
||||
用途:自动处理当日新成熟可调拨项与历史可恢复异常;执行结果通知统一走
|
||||
用途:自动处理可调拨异动与历史可恢复异常;执行结果通知统一走
|
||||
排程「启用通知」通用企微通知,本任务不自建领域通知。
|
||||
依赖:auto_transfer、transfer deps、job_registry.ScheduleExecuteResult、
|
||||
receipt_trigger_service.list_receipt_capable_source_codes(缺省来源口径)、
|
||||
inbound_trigger_service.list_inbound_capable_source_codes(缺省来源口径)、
|
||||
date_policy(业务日与日期解析)。
|
||||
逻辑:文件就绪保障内建于自动调拨匹配管道(就绪复用,缺失自动触发收料 RPA),
|
||||
不依赖其他排程成功;来源留空兜底为全部启用收料来源;可选收料日期范围
|
||||
以「T−N 偏移天数」配置,执行时按当日业务日换算为具体日期透传匹配层;
|
||||
空数据日(无 run_no 且无 errors)置 domain_notification_managed 保持
|
||||
静默(C5),有 Run 或硬失败交由框架完成卡按排程「启用通知」发送。
|
||||
逻辑:文件就绪保障内建于自动调拨匹配管道(就绪复用,缺失自动触发入站 RPA),
|
||||
不依赖其他排程成功;来源留空兜底为全部启用入站来源;可选异动日期范围
|
||||
以「T−N 偏移天数」配置,执行时按当日业务日换算为具体日期透传匹配层——
|
||||
该偏移参数是自动处理面的唯一日期边界(成熟规则已退役 2026-10-16,
|
||||
未配置即处理全部圈中数据);空数据日(无 run_no 且无 errors)置
|
||||
domain_notification_managed 保持静默(C5),有 Run 或硬失败交由框架完成卡按排程「启用通知」发送。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -21,7 +22,7 @@ from app.models import ScheduleRun, ScheduleTask
|
||||
from app.services.in_transit_transfer import deps as transfer_deps
|
||||
from app.services.in_transit_transfer.auto_transfer import AutoTransferOutcome, run_auto_transfer
|
||||
from app.services.in_transit_transfer.date_policy import business_date_now, parse_date
|
||||
from app.services.in_transit_transfer.receipt_trigger_service import list_receipt_capable_source_codes
|
||||
from app.services.in_transit_transfer.inbound_trigger_service import list_inbound_capable_source_codes
|
||||
from app.services.schedule.job_registry import ScheduleExecuteResult
|
||||
|
||||
|
||||
@@ -31,12 +32,12 @@ def _task_params(task: ScheduleTask) -> dict[str, Any]:
|
||||
|
||||
|
||||
def _wanted_source_codes(task: ScheduleTask) -> list[str]:
|
||||
"""提取结构化 source_codes;留空回落为全部启用且已配置收料报表的来源。
|
||||
"""提取结构化 source_codes;留空回落为全部启用且已配置收料报表(入站证据)的来源。
|
||||
|
||||
Args:
|
||||
task: 排程任务(job_params.source_codes 可为列表/字符串/缺省)。
|
||||
Returns:
|
||||
去空白后的来源编码列表;留空时为 list_receipt_capable_source_codes() 结果。
|
||||
去空白后的来源编码列表;留空时为 list_inbound_capable_source_codes() 结果。
|
||||
"""
|
||||
params = _task_params(task)
|
||||
values = params.get("source_codes") or []
|
||||
@@ -44,12 +45,12 @@ def _wanted_source_codes(task: ScheduleTask) -> list[str]:
|
||||
values = [values]
|
||||
codes = [str(item).strip() for item in values if str(item).strip()]
|
||||
if not codes:
|
||||
codes = list_receipt_capable_source_codes()
|
||||
codes = list_inbound_capable_source_codes()
|
||||
return codes
|
||||
|
||||
|
||||
def _offset_to_date(business_date: str, offset_days: Any, label: str, errors: list[str]) -> str | None:
|
||||
"""把「T−N 偏移天数」参数换算为执行日口径的具体收料日期(T−N)。
|
||||
"""把「T−N 偏移天数」参数换算为执行日口径的具体异动日期(T−N)。
|
||||
|
||||
Args:
|
||||
business_date: 业务日期 T(YYYY-MM-DD)。
|
||||
@@ -79,10 +80,10 @@ def run_in_transit_transfer_schedule(task: ScheduleTask, run: ScheduleRun) -> Sc
|
||||
底部「启用通知」配置);仅在空数据日(无 run_no 且无 errors)置
|
||||
domain_notification_managed 抑制框架完成卡(C5 空数据日静默);
|
||||
有 run_no(成功/部分失败)或硬失败时框架卡照常发送/兜底。
|
||||
可选收料日期范围以「T−N 偏移天数」配置(receipt_from/to_offset_days),
|
||||
执行时按当日业务日换算为具体日期透传匹配层,供测试/排障缩小本轮处理面;
|
||||
偏移非法(负数/小数/非数字)快速失败不执行;范围越界由匹配层
|
||||
ValidationError 落 errors 走硬失败路径。
|
||||
可选异动日期范围以「T−N 偏移天数」配置(receipt_from/to_offset_days),
|
||||
执行时按当日业务日换算为具体日期透传匹配层,供测试/排障缩小本轮处理面
|
||||
(成熟规则已退役:未配置偏移即处理全部圈中数据);偏移非法(负数/小数/
|
||||
非数字)快速失败不执行;范围越界由匹配层 ValidationError 落 errors 走硬失败路径。
|
||||
|
||||
Args:
|
||||
task: 排程任务(job_params: source_codes/recover_history/
|
||||
@@ -99,10 +100,10 @@ def run_in_transit_transfer_schedule(task: ScheduleTask, run: ScheduleRun) -> Sc
|
||||
# [STEP] T−N 偏移换算:非法配置快速失败(不执行、交框架失败通知),
|
||||
# 防止坏参数被静默忽略后退化成全量积压处理。
|
||||
param_errors: list[str] = []
|
||||
receipt_date_from = _offset_to_date(
|
||||
business_date, params.get("receipt_from_offset_days"), "收料日期起点", param_errors)
|
||||
receipt_date_to = _offset_to_date(
|
||||
business_date, params.get("receipt_to_offset_days"), "收料日期终点", param_errors)
|
||||
movement_date_from = _offset_to_date(
|
||||
business_date, params.get("receipt_from_offset_days"), "异动日期起点", param_errors)
|
||||
movement_date_to = _offset_to_date(
|
||||
business_date, params.get("receipt_to_offset_days"), "异动日期终点", param_errors)
|
||||
if param_errors:
|
||||
return (0, 0, 0, 0, [], [f"sources={len(source_codes)}"], param_errors)
|
||||
|
||||
@@ -117,8 +118,8 @@ def run_in_transit_transfer_schedule(task: ScheduleTask, run: ScheduleRun) -> Sc
|
||||
deps=deps,
|
||||
source_codes=source_codes,
|
||||
recover_history=recover_history,
|
||||
receipt_date_from=receipt_date_from,
|
||||
receipt_date_to=receipt_date_to,
|
||||
movement_date_from=movement_date_from,
|
||||
movement_date_to=movement_date_to,
|
||||
transfer_type=transfer_type,
|
||||
)
|
||||
|
||||
|
||||
@@ -612,7 +612,7 @@ def _register_builtin_platform_jobs() -> None:
|
||||
"label": "收料日期起(T-N)",
|
||||
"label_key": "admin.schedule.form.transferReceiptDateFrom",
|
||||
"required": False,
|
||||
"help": "填天数 N,按执行日换算收料日期下限 T−N;例:4=从 T−4 起。留空=全部成熟积压(默认 ≤ T-2)。",
|
||||
"help": "填天数 N,按执行日换算异动日期下限 T−N;例:4=从 T−4 起。留空=全部积压。",
|
||||
},
|
||||
{
|
||||
"key": "receipt_to_offset_days",
|
||||
@@ -620,7 +620,7 @@ def _register_builtin_platform_jobs() -> None:
|
||||
"label": "收料日期止(T-N)",
|
||||
"label_key": "admin.schedule.form.transferReceiptDateTo",
|
||||
"required": False,
|
||||
"help": "填天数 N,按执行日换算收料日期上限 T−N;例:起4止2=只处理 T−4~T−2。不得晚于成熟上限(默认 T-2)。",
|
||||
"help": "填天数 N,按执行日换算异动日期上限 T−N;例:起4止2=只处理 T−4~T−2。不得晚于执行日业务日期。",
|
||||
},
|
||||
),
|
||||
defaults={"source_codes": [], "recover_history": True},
|
||||
|
||||
@@ -24,6 +24,7 @@ from app.services.schedule.shipping_inventory_rpa_finalize import (
|
||||
recover_stale_awaiting_rpa_schedule_runs,
|
||||
)
|
||||
from app.services.schedule.freight_receipt_rpa_finalize import (
|
||||
recover_all_stale_orphan_receipt_units,
|
||||
recover_all_stale_receipt_rpa_schedule_runs,
|
||||
recover_stale_receipt_rpa_schedule_runs,
|
||||
is_awaiting_receipt_rpa_schedule_run,
|
||||
@@ -370,7 +371,7 @@ def prepare_schedule_queue() -> int:
|
||||
|
||||
# 调拨来源等待回调 Job 的超时回收与下方 RPA 心跳告警同属每轮领取维护:
|
||||
# 不能挂在 worker 启动一次性分支里,否则进程长期驻留期间超时任务永远无法终态化。
|
||||
from app.services.in_transit_transfer.receipt_callback_bridge import recover_stale_waiting_source_jobs
|
||||
from app.services.in_transit_transfer.inbound_callback_bridge import recover_stale_waiting_source_jobs
|
||||
|
||||
transfer_stale = recover_stale_waiting_source_jobs()
|
||||
if transfer_stale:
|
||||
@@ -383,6 +384,13 @@ def prepare_schedule_queue() -> int:
|
||||
if receipt_stale:
|
||||
current_app.logger.info("[schedule-worker] recovered %d stale receipt RPA runs", receipt_stale)
|
||||
|
||||
# 按需触发(无排程)孤儿单元同口径回收(2026-10-16):调拨工作台重试/自动调拨
|
||||
# 补抓创建的单元 schedule_run_id 为 NULL,上方 run 级扫描按 run 遍历覆盖不到;
|
||||
# 网页异常关闭后回调永不到达,不回收则来源投影永久 fetching、重试按钮永久锁死。
|
||||
orphan_stale = recover_all_stale_orphan_receipt_units()
|
||||
if orphan_stale:
|
||||
current_app.logger.info("[schedule-worker] recovered %d stale orphan receipt RPA units", orphan_stale)
|
||||
|
||||
try:
|
||||
from app.services.shipping_notice.rpa_worker_heartbeat_alert import maybe_alert_rpa_worker_status
|
||||
|
||||
|
||||
Reference in new issue
Block a user