feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务

- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
This commit is contained in:
hugo committed 2026-10-19 18:45:16 +08:00
1 parent 39fe3c05f2
commit 911afa0f73
152 files changed
+9861 -2984

No files matched your search

+5 -1
View File
@@ -4,7 +4,7 @@ scripts/freight_rpa/common/rpa_callbacks.py
用途:货代 RPA Worker → 后端回调 URL 的单一真相(/api/freight-rpa/* 前缀,批次 R 起
取代旧 shipping-notice rpa 回调前缀,无双路由、无兼容层)与统一 POST 实现。
依赖:rpa_common.env、urllib、json(Worker 独立运行,禁止 import 后端 app)。
逻辑:onhand/inbound Worker 与 rpa_heartbeat 一律从本模块常量取回调路径,禁止再硬编码;
逻辑:onhand/inbound/outbound Worker 与 rpa_heartbeat 一律从本模块常量取回调路径,禁止再硬编码;
post_callback 复用统一鉴权(双 Header 集成密钥)、脱敏请求日志与 HTTP 错误处理。
路径清单与后端 backend/app/api/freight_rpa.py 蓝图路由一一对应,改动须双侧同步。
"""
@@ -38,6 +38,10 @@ ONHAND_REPAIR = f"{_CALLBACK_BASE}/onhand-repair"
INBOUND_BATCH_READY = f"{_CALLBACK_BASE}/inbound-batch-ready"
INBOUND_UNIT_PROGRESS = f"{_CALLBACK_BASE}/inbound-unit-progress"
INBOUND_BATCH_PROGRESS = f"{_CALLBACK_BASE}/inbound-batch-progress"
# outbound(出站)Worker 回调。
OUTBOUND_BATCH_READY = f"{_CALLBACK_BASE}/outbound-batch-ready"
OUTBOUND_UNIT_PROGRESS = f"{_CALLBACK_BASE}/outbound-unit-progress"
OUTBOUND_BATCH_PROGRESS = f"{_CALLBACK_BASE}/outbound-batch-progress"
# 货代上传结果(URL 词根不变,仅前缀换为 /api/freight-rpa)。
UPLOAD_COMPLETE = f"{_CALLBACK_BASE}/upload-complete"
+13 -6
View File
@@ -3,8 +3,8 @@ scripts/freight_rpa/common/rpa_heartbeat.py
用途:货代 RPA Worker 向后端上报心跳(POST /api/freight-rpa/heartbeat(路径常量见 common/rpa_callbacks.HEARTBEAT))。
依赖:rpa_common.env、urllib、socket。
逻辑:由 onhand/inbound Worker 主循环或守护线程按 FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS 周期调用;
onhand 声明 onhand_export,inbound(原收料)声明 inbound_export;onhand 侧可写共享目录哨兵。
逻辑:由 onhand/inbound/outbound Worker 主循环或守护线程按 FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS 周期调用;
onhand 声明 onhand_export,inbound 声明 inbound_export,outbound 声明 outbound_export;可写共享目录哨兵。
"""
from __future__ import annotations
@@ -138,14 +138,21 @@ def build_heartbeat_payload(
"""
组装心跳 payload。
核心逻辑:onhand Worker 声明 onhand_export;inbound Worker(worker_id 以 -inbound
结尾,或 trigger_dir 路径含 /inbound/)声明 inbound_export;开启上传时附加 freight_rpa_upload。
核心逻辑:outbound Worker(worker_id 以 -outbound 结尾,或 trigger_dir 含 /outbound/)
声明 outbound_export;inbound 同理 inbound_export;其余 onhand_export。开启上传时附加 freight_rpa_upload。
"""
hostname = socket.gethostname()
worker_id = _resolve_worker_id(hostname)
trigger_norm = str(trigger_dir or "").replace("\\", "/").lower()
is_inbound = str(worker_id).lower().endswith("-inbound") or "/inbound/" in f"/{trigger_norm}/"
capabilities = ["inbound_export" if is_inbound else "onhand_export"]
worker_id_l = str(worker_id).lower()
is_outbound = worker_id_l.endswith("-outbound") or "/outbound/" in f"/{trigger_norm}/"
is_inbound = worker_id_l.endswith("-inbound") or "/inbound/" in f"/{trigger_norm}/"
if is_outbound:
capabilities = ["outbound_export"]
elif is_inbound:
capabilities = ["inbound_export"]
else:
capabilities = ["onhand_export"]
upload_enabled = str(_env("FREIGHT_RPA_WORKER_UPLOAD_ENABLED", "false") or "false").strip().lower() in {
"1", "true", "yes", "on",
}
@@ -3,7 +3,7 @@ scripts/freight_rpa/common/worker_disk_cleanup.py
用途:RPA Worker 本机磁盘保留清理,防止 logs / 归档 trigger / drop 归档 / 孤儿临时 Profile 长期堆积撑爆盘符。
依赖:rpa_common.env、pathlib、shutil、time。
逻辑:由 onhand(及可复用的 inbound)Worker 主循环周期调用;只删超过保留期的归档与临时物,
逻辑:由 onhand / inbound / outbound Worker 主循环周期调用;只删超过保留期的归档与临时物,
绝不删除进行中的 trigger 根目录 JSON、可复用会话 Profile(*_session)、以及活跃 cleanup 队列。
"""
@@ -48,9 +48,9 @@ _PROTECTED_DROP_DIRNAMES = frozenset(
)
_DROP_ROOT_SUFFIXES = frozenset({".xlsx", ".xls", ".csv", ".hxops_consumed"})
_TRIGGER_ARCHIVE_DIRNAMES = ("processed", "failed")
# 一次性运行 Profile 前缀:onhand Worker 用 run_*,inbound Worker 用 inbound_*,
# 两者均为浏览器崩溃/异常中断后可能残留的临时目录,超期统一清理。
_RUNTIME_PROFILE_TEMP_PREFIXES = ("run_", "inbound_")
# 一次性运行 Profile 前缀:onhand 用 run_*,inbound 用 inbound_*,outbound 用 outbound_*,
# 均为浏览器崩溃/异常中断后可能残留的临时目录,超期统一清理。
_RUNTIME_PROFILE_TEMP_PREFIXES = ("run_", "inbound_", "outbound_")
_DEFAULT_RETENTION_DAYS = 7
_DEFAULT_INTERVAL_SECONDS = 3600
_DEFAULT_ORPHAN_PROFILE_HOURS = 24
@@ -111,7 +111,7 @@ def cleanup_interval_seconds() -> int:
def orphan_profile_max_age_seconds() -> int:
"""
读取孤儿临时 Profile(run_* / inbound_*)最大存活秒数。
读取孤儿临时 Profile(run_* / inbound_* / outbound_*)最大存活秒数。
核心逻辑:`FREIGHT_RPA_WORKER_ORPHAN_PROFILE_MAX_AGE_HOURS`,默认 24;
`<=0` 禁用孤儿 Profile 清理。会话复用目录(*_session)永不删除。
@@ -335,11 +335,11 @@ def cleanup_orphan_runtime_profiles(
stats: DiskCleanupStats,
) -> None:
"""
清理浏览器 Profile 根下过期的一次性 `run_*` / `inbound_*` 目录。
清理浏览器 Profile 根下过期的一次性 `run_*` / `inbound_*` / `outbound_*` 目录。
核心逻辑:会话复用目录名以 `_session` 结尾或历史 `dsv360_session`,一律保留;
仅删除 `run_*` / `inbound_*` 临时目录,避免异常中断后残留撑盘。
inbound Worker 一次性 Profile 以 inbound_ 前缀创建,同属清理对象。
仅删除 `run_*` / `inbound_*` / `outbound_*` 临时目录,避免异常中断后残留撑盘。
inbound/outbound Worker 一次性 Profile 以前缀创建,同属清理对象。
"""
if profile_root is None or not profile_root.is_dir():
return
@@ -379,7 +379,7 @@ def run_worker_disk_cleanup(
【AI 触发时机】:Worker 启动后及主循环按 `DISK_CLEANUP_INTERVAL_SECONDS` 周期调用。
核心逻辑:按 `DISK_RETENTION_DAYS` 清理归档/日志备份/队列残留;按
`ORPHAN_PROFILE_MAX_AGE_HOURS` 清理孤儿 `run_*` / `inbound_*` Profile。全部 best-effort。
`ORPHAN_PROFILE_MAX_AGE_HOURS` 清理孤儿 `run_*` / `inbound_*` / `outbound_*` Profile。全部 best-effort。
Args:
onhand_trigger_dir: onhand trigger 根目录。