feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约

将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-17 23:18:48 +08:00
1 parent bf3154033b
commit ffc0d4a430
147 files changed
+4221 -2678

No files matched your search

+15 -13
View File
@@ -1,10 +1,10 @@
"""
scripts/freight_rpa/common/rpa_heartbeat.py
用途:货代 RPA Worker 向后端上报心跳(POST /api/shipping-notice/rpa/heartbeat)。
用途:货代 RPA Worker 向后端上报心跳(POST /api/freight-rpa/heartbeat(路径常量见 common/rpa_callbacks.HEARTBEAT))。
依赖:rpa_common.env、urllib、socket。
逻辑:由库存/收料 Worker 主循环或守护线程按 FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS 周期调用;
库存声明 inventory_export,收料声明 receipt_export;库存侧可写共享目录哨兵。
逻辑:由 onhand/inbound Worker 主循环或守护线程按 FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS 周期调用;
onhand 声明 onhand_export,inbound(原收料)声明 inbound_export;onhand 侧可写共享目录哨兵。
"""
from __future__ import annotations
@@ -15,11 +15,11 @@ from pathlib import Path as _Path
_RPA_ROOT = next(
(_c for _c in _Path(__file__).resolve().parents
if (_c / "connectors").is_dir() and (_c / "common").is_dir()
and (_c / "inventory").is_dir() and (_c / "receipt").is_dir()),
and (_c / "onhand").is_dir() and (_c / "inbound").is_dir()),
None,
)
if _RPA_ROOT is not None:
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "onhand", "inbound"):
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
if _dir not in _sys.path:
_sys.path.insert(0, _dir)
@@ -37,6 +37,8 @@ from pathlib import Path
from typing import Any
from rpa_common import env as _env
# 心跳路径单一真相(/api/freight-rpa/heartbeat),与业务回调共用 rpa_callbacks 常量。
from rpa_callbacks import HEARTBEAT as _HEARTBEAT_PATH # noqa: E402
_RUNTIME_STATUS = "idle"
_LAST_HEARTBEAT_MONO = 0.0
@@ -82,7 +84,7 @@ def _resolve_shared_scope_id() -> str:
value = str(raw or "").strip()
if value:
return value
trigger = str(_env("FREIGHT_RPA_SHARED_INVENTORY_TRIGGER_DIR") or "").replace("\\", "/").lower()
trigger = str(_env("FREIGHT_RPA_SHARED_ONHAND_TRIGGER_DIR") or "").replace("\\", "/").lower()
for token in ("hxops-rpa-test-data", "hxops-rpa-prod-data"):
if token in trigger:
return token
@@ -136,14 +138,14 @@ def build_heartbeat_payload(
"""
组装心跳 payload。
核心逻辑:库存 Worker 声明 inventory_export;收料 Worker(worker_id 以 -receipt
结尾,或 trigger_dir 路径含 /receipt/)声明 receipt_export;开启上传时附加 freight_upload。
核心逻辑:onhand Worker 声明 onhand_export;inbound Worker(worker_id 以 -inbound
结尾,或 trigger_dir 路径含 /inbound/)声明 inbound_export;开启上传时附加 freight_upload。
"""
hostname = socket.gethostname()
worker_id = _resolve_worker_id(hostname)
trigger_norm = str(trigger_dir or "").replace("\\", "/").lower()
is_receipt = str(worker_id).lower().endswith("-receipt") or "/receipt/" in f"/{trigger_norm}/"
capabilities = ["receipt_export" if is_receipt else "inventory_export"]
is_inbound = str(worker_id).lower().endswith("-inbound") or "/inbound/" in f"/{trigger_norm}/"
capabilities = ["inbound_export" if is_inbound else "onhand_export"]
upload_enabled = str(_env("FREIGHT_RPA_WORKER_UPLOAD_ENABLED", "false") or "false").strip().lower() in {
"1", "true", "yes", "on",
}
@@ -179,7 +181,7 @@ def post_heartbeat(payload: dict[str, Any]) -> bool:
print("[heartbeat] skipped: missing SHIPPING_NOTICE_TOOL_API_KEY", file=sys.stderr)
return False
url = f"{base}/api/shipping-notice/rpa/heartbeat"
url = f"{base}{_HEARTBEAT_PATH}"
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
req = urllib.request.Request(
url,
@@ -225,7 +227,7 @@ def _start_heartbeat_request(payload: dict[str, Any]) -> bool:
thread = threading.Thread(
target=post_heartbeat,
args=(payload,),
name="shipping-rpa-heartbeat",
name="freight-rpa-heartbeat",
daemon=True,
)
_HEARTBEAT_THREAD = thread
@@ -291,6 +293,6 @@ def start_heartbeat_loop(
except Exception:
print("[heartbeat] loop tick failed", file=sys.stderr)
thread = threading.Thread(target=_loop, name="shipping-rpa-heartbeat-loop", daemon=True)
thread = threading.Thread(target=_loop, name="freight-rpa-heartbeat-loop", daemon=True)
thread.start()
return thread