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

@@ -1,13 +1,14 @@
"""
services/freight_rpa/common/worker_heartbeat.py
用途:货代 RPA Worker 心跳存储、共享目录校验、库存/上传/收料就绪检查与状态查询。
用途:货代 RPA Worker 心跳存储、共享目录校验、库存/上传/入站/出站就绪检查与状态查询。
依赖:Flask current_app、redis_client、同包 rpa_runtime(mock 模式)、
freight_rpa.onhand.trigger_io(onhand trigger 目录)、
freight_rpa.upload.trigger_io(上传目录)、
freight_rpa.inbound.trigger_io(inbound trigger 目录)。
逻辑:Windows Worker 调 POST /api/freight-rpa/heartbeat;写 onhand/upload/inbound trigger 前强制就绪检查;
管理端 /rpa-workers/status?scope=onhand|inbound 展示对应 Worker 心跳徽标。
freight_rpa.inbound.trigger_io(inbound trigger 目录)、
freight_rpa.outbound.trigger_io(outbound trigger 目录)。
逻辑:Windows Worker 调 POST /api/freight-rpa/heartbeat;写 onhand/upload/inbound/outbound trigger 前强制就绪检查;
管理端 /rpa-workers/status?scope=onhand|inbound|outbound 展示对应 Worker 心跳徽标。
"""
from __future__ import annotations
@@ -40,8 +41,11 @@ RPA_FREIGHT_UPLOAD_NOT_READY_MESSAGE = "货代上传 RPA 未就绪,请检查 W
ONHAND_EXPORT_CAPABILITY = "onhand_export"
FREIGHT_RPA_UPLOAD_CAPABILITY = "freight_rpa_upload"
INBOUND_EXPORT_CAPABILITY = "inbound_export"
OUTBOUND_EXPORT_CAPABILITY = "outbound_export"
# inbound Worker 由 start_inbound_worker.ps1 注入 FREIGHT_RPA_WORKER_ID=<主机名>-inbound。
INBOUND_WORKER_ID_SUFFIX = "-inbound"
# outbound Worker 由 start_outbound_worker.ps1 注入 FREIGHT_RPA_WORKER_ID=<主机名>-outbound。
OUTBOUND_WORKER_ID_SUFFIX = "-outbound"
_REDIS_INDEX_KEY = "shipping:rpa:heartbeat:index"
_REDIS_WORKER_KEY_PREFIX = "shipping:rpa:heartbeat:worker:"
_MEMORY_STORE: dict[str, dict[str, Any]] = {}
@@ -59,6 +63,7 @@ _REASON_USER_HINTS: dict[str, str] = {
"environment_mismatch": "Worker 环境与后端环境不一致",
"missing_onhand_export_capability": "Worker 未声明 onhand_export 能力",
"missing_inbound_export_capability": "Worker 未声明 inbound_export 能力",
"missing_outbound_export_capability": "Worker 未声明 outbound_export 能力",
"missing_freight_rpa_upload_capability": "Worker 未声明 freight_rpa_upload 能力",
"credential_key_missing": "未配置 FREIGHT_RPA_CREDENTIAL_KEY",
"upload_queue_dir_not_configured": "未配置货代上传队列目录",
@@ -369,7 +374,7 @@ def _verify_shared_trigger_mount(
trigger_dir: Path | None, *, require_sentinel: bool = True,
) -> tuple[bool, list[str]]:
"""
校验 trigger 目录可写;默认要求共享哨兵(库存与收料同口径)。
校验 trigger 目录可写;默认要求共享哨兵(库存与入站同口径)。
Args:
trigger_dir: 共享 trigger 目录。
@@ -407,7 +412,7 @@ def _verify_shared_trigger_mount(
return not reasons, reasons
def is_receipt_rpa_worker(worker: dict[str, Any]) -> bool:
def is_inbound_rpa_worker(worker: dict[str, Any]) -> bool:
"""
判断心跳是否来自 inbound(原收料)Worker。
@@ -428,6 +433,27 @@ def is_receipt_rpa_worker(worker: dict[str, Any]) -> bool:
return False
def is_outbound_rpa_worker(worker: dict[str, Any]) -> bool:
"""
判断心跳是否来自 outbound Worker。
优先认 <主机名>-outbound(ps1 注入);兼容未注入 FREIGHT_RPA_WORKER_ID、
回退为 <主机名>-<pid> 的进程——用心跳 trigger_dir 与 outbound 目录对齐判定。
"""
if str(worker.get("worker_id") or "").strip().lower().endswith(OUTBOUND_WORKER_ID_SUFFIX):
return True
from app.services.freight_rpa.outbound.trigger_io import outbound_trigger_dir
expected = outbound_trigger_dir()
actual = str(worker.get("trigger_dir") or "").strip()
if expected is None or not actual:
return False
try:
return Path(actual).resolve() == expected.resolve()
except OSError:
return False
def _worker_scope_matches(worker: dict[str, Any]) -> tuple[bool, list[str]]:
expected_scope = shared_scope_id()
if not expected_scope:
@@ -454,7 +480,7 @@ def _worker_is_eligible(
backend_env: 后端归一环境(dev/test/prod)。
ttl_seconds: 心跳 TTL。
now: 当前 epoch 秒。
required_capability: 必选能力(onhand_export / inbound_export / freight_rpa_upload)。
required_capability: 必选能力(onhand_export / inbound_export / outbound_export / freight_rpa_upload)。
Returns:
(是否合格, 不合格原因列表)。
@@ -482,7 +508,7 @@ def _worker_is_eligible(
def _missing_capability_reason(required_capability: str) -> str:
"""按必选能力生成缺失原因码(onhand_export / inbound_export / freight_rpa_upload)。"""
"""按必选能力生成缺失原因码(onhand_export / inbound_export / outbound_export / freight_rpa_upload)。"""
return f"missing_{required_capability}_capability"
@@ -521,8 +547,8 @@ def _check_rpa_worker_ready_for_capability(
trigger_dir: 待校验的共享 trigger 目录。
required_capability: 必选 Worker 能力;空字符串表示不校验能力。
extra_reasons: 调用方预检附加原因(凭据/队列等)。
worker_predicate: 可选过滤(如仅收料 Worker);不匹配的心跳不进入列表。
require_sentinel: 是否要求共享哨兵(库存/收料同口径均 True)。
worker_predicate: 可选过滤(如仅入站 Worker);不匹配的心跳不进入列表。
require_sentinel: 是否要求共享哨兵(库存/入站同口径均 True)。
Returns:
RpaWorkerReadiness。
@@ -637,9 +663,9 @@ def check_inventory_rpa_worker_ready() -> RpaWorkerReadiness:
)
def check_receipt_rpa_worker_ready() -> RpaWorkerReadiness:
def check_inbound_rpa_worker_ready() -> RpaWorkerReadiness:
"""
检查收料 RPA Worker 是否在线(管理端徽标 + 写 trigger 前 fail-fast)。
检查入站 RPA Worker 是否在线(管理端徽标 + 写 trigger 前 fail-fast)。
核心逻辑:仅看 inbound Worker 心跳(*-inbound 或 trigger_dir 对齐);要求
inbound_export 能力;校验 inbound trigger 目录可写;与 onhand 同口径要求共享哨兵。
@@ -649,24 +675,24 @@ def check_receipt_rpa_worker_ready() -> RpaWorkerReadiness:
return _check_rpa_worker_ready_for_capability(
trigger_dir=inbound_trigger_dir(),
required_capability=INBOUND_EXPORT_CAPABILITY,
worker_predicate=is_receipt_rpa_worker,
worker_predicate=is_inbound_rpa_worker,
require_sentinel=True,
)
def assert_receipt_rpa_worker_ready() -> None:
def assert_inbound_rpa_worker_ready() -> None:
"""
写收料 trigger 前的强制就绪检查。
写入站 trigger 前的强制就绪检查。
核心逻辑:镜像 assert_inventory_rpa_worker_ready;离线则抛 ServiceUnavailableError,
排程不得进入 awaiting_receipt_rpa。
排程不得进入 awaiting_inbound_rpa。
Raises:
ServiceUnavailableError: worker 未就绪、共享目录无效或心跳存储不可用。
"""
from app.services.freight_rpa.inbound.trigger_io import inbound_trigger_dir
result = check_receipt_rpa_worker_ready()
result = check_inbound_rpa_worker_ready()
if result.ready:
return
code = 'RPA.WORKER_OFFLINE'
@@ -691,6 +717,60 @@ def assert_receipt_rpa_worker_ready() -> None:
)
def check_outbound_rpa_worker_ready() -> RpaWorkerReadiness:
"""
检查出站 RPA Worker 是否在线(写 trigger 前 fail-fast)。
核心逻辑:仅看 outbound Worker 心跳(*-outbound 或 trigger_dir 对齐);要求
outbound_export 能力;校验 outbound trigger 目录可写;与 onhand/inbound 同口径要求共享哨兵。
"""
from app.services.freight_rpa.outbound.trigger_io import outbound_trigger_dir
return _check_rpa_worker_ready_for_capability(
trigger_dir=outbound_trigger_dir(),
required_capability=OUTBOUND_EXPORT_CAPABILITY,
worker_predicate=is_outbound_rpa_worker,
require_sentinel=True,
)
def assert_outbound_rpa_worker_ready() -> None:
"""
写出站 trigger 前的强制就绪检查。
核心逻辑:镜像 assert_inbound_rpa_worker_ready;离线则抛 ServiceUnavailableError,
排程不得进入 awaiting_outbound_rpa_callback。
Raises:
ServiceUnavailableError: worker 未就绪、共享目录无效或心跳存储不可用。
"""
from app.services.freight_rpa.outbound.trigger_io import outbound_trigger_dir
result = check_outbound_rpa_worker_ready()
if result.ready:
return
code = 'RPA.WORKER_OFFLINE'
if 'heartbeat_storage_unavailable' in result.reasons:
code = 'RPA.HEARTBEAT_STORAGE_UNAVAILABLE'
logger.warning(
'[rpa-heartbeat] outbound readiness blocked code=%s reasons=%s detail=%s',
code,
result.reasons,
result.detail_message,
)
raise ServiceUnavailableError(
result.message,
code=code,
details={
'reasons': result.reasons,
'detail_message': result.detail_message,
'checked_at': result.checked_at,
'shared_scope_id': shared_scope_id(),
'trigger_dir': str(outbound_trigger_dir() or ''),
},
)
def _freight_upload_config_reasons() -> list[str]:
"""
预检货代上传静态配置:凭据主密钥、上传队列目录、回调 API 基址。
@@ -852,19 +932,23 @@ def assert_inventory_rpa_worker_ready() -> None:
def serialize_rpa_worker_status(*, scope: str = "onhand") -> dict[str, Any]:
"""
管理端展示:按 scope 返回在手(onhand)或入站(inbound)Worker 心跳摘要。
管理端展示:按 scope 返回在手(onhand)、入站(inbound)或出站(outbound)Worker 心跳摘要。
Args:
scope: onhand(默认,库存 RPA 工作台)或 inbound(入站 RPA 工作台);
取值为后端内部与前端查询参数约定,与 Worker 上报能力标识(Task 12)无关。
scope: onhand(默认,库存 RPA 工作台)、inbound(入站)或 outbound(出站);
取值为后端内部与前端查询参数约定,与 Worker 上报能力标识无关。
"""
from app.services.freight_rpa.upload.trigger_io import upload_trigger_dir
from app.services.freight_rpa.inbound.trigger_io import inbound_trigger_dir
from app.services.freight_rpa.outbound.trigger_io import outbound_trigger_dir
normalized = str(scope or "onhand").strip().lower()
if normalized == "inbound":
readiness = check_receipt_rpa_worker_ready()
readiness = check_inbound_rpa_worker_ready()
trigger_path = str(inbound_trigger_dir() or "")
elif normalized == "outbound":
readiness = check_outbound_rpa_worker_ready()
trigger_path = str(outbound_trigger_dir() or "")
else:
readiness = check_inventory_rpa_worker_ready()
trigger_path = str(onhand_trigger_dir() or "")