- AGENTS.md §8.1 红线改写:env 命名空间恢复 FREIGHT_RPA_*,SHIPPING_NOTICE_RPA_* 一刀切废弃、不设兼容层
- 全仓机械前缀替换(70 文件)并删除兼容层;4 个既有基线测试失败一并清零(过期桩/存储隔离/排程 fail-fast 接线)
- 运行期数据目录统一 freight_rpa/{inventory,receipt,common}:settings 默认、4 个 env、2 个 ps1 三侧一致
- 代码目录 scripts/rpa 更名 scripts/freight_rpa 与数据根对齐;全仓路径引用同步,README 部署走查与升级小节
- 收料部署对齐库存:计划任务注册脚本(独立任务名)、可选 -SessionReuse 会话复用、DSV 老别名链移除
- readiness 加固:排程离线立即 failed(不挂 awaiting);无 Redis 时追加进程内存储原因码与本地测试指引
- DSV 会话准备遇 SSO 重定向中止时改走自动登录,首源不再因未登录判死
79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
"""独立收料 RPA trigger 的目录和载荷构造服务。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import tempfile
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
from flask import current_app
|
||
|
||
|
||
def receipt_trigger_dir() -> Path | None:
|
||
"""返回收料专用 trigger 目录;绝不回落到库存 trigger 目录。"""
|
||
value = str(current_app.config.get('FREIGHT_RPA_SHARED_RECEIPT_TRIGGER_DIR') or '').strip()
|
||
return Path(value) if value else None
|
||
|
||
|
||
def receipt_warehouse_values(config: Any) -> list[str]:
|
||
"""从收料配置的查询条件中提取 Warehouse 条件值(去空去重,保持顺序)。
|
||
|
||
Warehouse 条件即逐仓拆分维度(与库存配置同构,不单独持久化仓库清单);
|
||
排程/触发按返回值逐仓展开执行单元。DBS 配置由校验保证恰一条非空条件。
|
||
"""
|
||
values: list[str] = []
|
||
for item in (config.query_parameters_json or []):
|
||
if not isinstance(item, dict) or str(item.get('name') or '').strip().lower() != 'warehouse':
|
||
continue
|
||
for raw in (item.get('values') or []):
|
||
cleaned = str(raw or '').strip()
|
||
if cleaned and cleaned not in values:
|
||
values.append(cleaned)
|
||
return values
|
||
|
||
|
||
def receipt_unit_parameters(config: Any) -> list[dict[str, Any]]:
|
||
"""构建单个执行单元的受控条件:剥离 Warehouse 条件。
|
||
|
||
Worker 会按单元的 warehouse_id 自动追加单值 Warehouse 条件,
|
||
快照里保留用户维护的 Warehouse 条件会与之重复冲突。
|
||
"""
|
||
return [
|
||
dict(item) for item in (config.query_parameters_json or [])
|
||
if not (isinstance(item, dict) and str(item.get('name') or '').strip().lower() == 'warehouse')
|
||
]
|
||
|
||
|
||
def write_receipt_export_batch_trigger(run_no: str, units: list[dict[str, Any]], payload: dict[str, Any] | None = None) -> str | None:
|
||
"""写入收料批量 trigger;worker 仅允许消费 shipping_receipt_rpa.v1。"""
|
||
trigger_dir = receipt_trigger_dir()
|
||
if trigger_dir is None or not units:
|
||
return None
|
||
trigger_dir.mkdir(parents=True, exist_ok=True)
|
||
safe_run_no = ''.join(char for char in run_no if char.isalnum() or char in {'-', '_'}) or 'receipt'
|
||
body = {
|
||
'schema_version': 'shipping_receipt_rpa.v1', 'action': 'receipt_export_batch',
|
||
'batch_run_no': run_no, 'requested_at': datetime.utcnow().isoformat(), 'units': units,
|
||
**(payload or {}),
|
||
}
|
||
path = trigger_dir / f'receipt_export_batch_{safe_run_no}.json'
|
||
# Worker 轮询 *.json;必须先写入同目录临时文件后原子替换,不能让 Worker
|
||
# 领取到半截 JSON 并误入 failed/,这不会影响库存 trigger 的既有实现。
|
||
fd, temp_name = tempfile.mkstemp(prefix=f'.{path.stem}.', suffix='.tmp', dir=str(trigger_dir))
|
||
try:
|
||
with os.fdopen(fd, 'w', encoding='utf-8') as handle:
|
||
handle.write(json.dumps(body, ensure_ascii=False, indent=2))
|
||
handle.flush()
|
||
os.fsync(handle.fileno())
|
||
os.replace(temp_name, path)
|
||
except Exception:
|
||
try:
|
||
Path(temp_name).unlink(missing_ok=True)
|
||
except OSError:
|
||
pass
|
||
raise
|
||
return str(path)
|