Files
syops/backend/app/services/shipping_notice/receipt_rpa.py
T
hugo abf3d48927 feat(in-transit-transfer): 自动调拨联动收料就绪并分层来源错误
- 调度整合:退役 in_transit_receipt_file_job,收料归档收敛 shipping_receipt_rpa_archive,排程注册表统一
- 自动调拨 Job:调拨前确保各来源当日收料文件就绪(缺失自动触发收料 RPA),严格等量匹配后生成 ERP 调拨单
- worker 手动唤醒:排程工作台支持手动唤醒与 runs 透视,heartbeat 协议同步
- 收料缺失幸存 T2:parse_failed 状态落库(迁移 q4c5)
- 来源错误分层与 stale 时钟换源:改挂 inventory_checked_at 库存查得时刻,展示阈值与执行门禁解耦(迁移 t5a6)
- 新增 wecom_sender 企微通知:每非空 Run 汇总一条,通知失败不影响 ERP 结果
- 同步排程工作台/调拨卡片前端、chat/admin 文案四语言、RPA 脚本与测试全家
2026-09-30 00:20:06 +08:00

57 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/app/services/shipping_notice/receipt_rpa.py
用途:独立收料 RPA trigger 的目录解析与批量载荷原子写入。
依赖:Flask current_app 配置 FREIGHT_RPA_SHARED_RECEIPT_TRIGGER_DIR。
逻辑:freight_receipt_rpa_job / receipt_trigger_service 调用写 trigger;
Worker 仅消费 shipping_receipt_rpa.v1,绝不回落到库存 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 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)