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:
1 parent
bf3154033b
commit
ffc0d4a430
147 files changed
+4221
-2678
No files matched your search
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
services/freight_rpa/onhand/attempts.py
|
||||
|
||||
用途:统计任务的 onhand(库存)RPA 导出与货代上传重试次数,并返回统一上限。
|
||||
依赖:ShippingNotice、ShippingTaskEvent 模型与事件查询逻辑(仅 models,无业务编排 import)。
|
||||
逻辑:由 shipping_notice 的 inventory_rpa_workflow、fulfillment_workflow 等模块调用本文件,
|
||||
决定是否还能继续触发 RPA 重试;事件类型字符串(inventory_rpa_* 等)是落库数据值,
|
||||
随 models 命名一并留待批次 S 处理,本模块只改包归属与标识符词根。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from app.models import ShippingNotice, ShippingTaskEvent
|
||||
|
||||
DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS = 3
|
||||
|
||||
_ONHAND_SUCCESS_EVENTS = {
|
||||
"inventory_uploaded",
|
||||
"inventory_parsed",
|
||||
"inventory_rpa_drop_ingested",
|
||||
"inventory_rpa_ready",
|
||||
"inventory_template_mapping_used",
|
||||
}
|
||||
|
||||
_ONHAND_ATTEMPT_RESET_EVENTS = _ONHAND_SUCCESS_EVENTS | {
|
||||
"decision_retry_rpa",
|
||||
}
|
||||
|
||||
_UPLOAD_SUCCESS_EVENTS = {
|
||||
"upload_rpa_success",
|
||||
"upload_manual_confirmed",
|
||||
}
|
||||
|
||||
|
||||
def onhand_rpa_attempts(task: ShippingNotice | None) -> int:
|
||||
"""统计自上次库存成功事件以来的 RPA 库存导出触发/失败次数。"""
|
||||
if not task or task.id is None:
|
||||
return 0
|
||||
|
||||
last_reset = (
|
||||
ShippingTaskEvent.query.filter_by(task_id=task.id)
|
||||
.filter(ShippingTaskEvent.event_type.in_(_ONHAND_ATTEMPT_RESET_EVENTS))
|
||||
.order_by(ShippingTaskEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
since: datetime | None = last_reset.created_at if last_reset else None
|
||||
|
||||
triggered = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
||||
ShippingTaskEvent.event_type == "inventory_rpa_triggered"
|
||||
)
|
||||
failed = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
||||
ShippingTaskEvent.event_type == "inventory_rpa_failed"
|
||||
)
|
||||
if since:
|
||||
triggered = triggered.filter(ShippingTaskEvent.created_at >= since)
|
||||
failed = failed.filter(ShippingTaskEvent.created_at >= since)
|
||||
|
||||
return max(triggered.count(), failed.count())
|
||||
|
||||
|
||||
def upload_rpa_attempts(task: ShippingNotice | None) -> int:
|
||||
"""统计自上次上传成功事件以来的 RPA 货代上传触发/失败次数。"""
|
||||
if not task or task.id is None:
|
||||
return 0
|
||||
|
||||
last_success = (
|
||||
ShippingTaskEvent.query.filter_by(task_id=task.id)
|
||||
.filter(ShippingTaskEvent.event_type.in_(_UPLOAD_SUCCESS_EVENTS))
|
||||
.order_by(ShippingTaskEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
since: datetime | None = last_success.created_at if last_success else None
|
||||
|
||||
triggered = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
||||
ShippingTaskEvent.event_type == "upload_rpa_triggered"
|
||||
)
|
||||
failed = ShippingTaskEvent.query.filter_by(task_id=task.id).filter(
|
||||
ShippingTaskEvent.event_type == "upload_rpa_failed"
|
||||
)
|
||||
if since:
|
||||
triggered = triggered.filter(ShippingTaskEvent.created_at >= since)
|
||||
failed = failed.filter(ShippingTaskEvent.created_at >= since)
|
||||
|
||||
return max(triggered.count(), failed.count())
|
||||
|
||||
|
||||
def max_rpa_upload_attempts() -> int:
|
||||
"""RPA 货代上传最大重试次数(环境变量 FREIGHT_RPA_UPLOAD_MAX_ATTEMPTS)。"""
|
||||
value = os.getenv("FREIGHT_RPA_UPLOAD_MAX_ATTEMPTS")
|
||||
if value is None or not str(value).strip():
|
||||
return DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS
|
||||
try:
|
||||
parsed = int(value)
|
||||
except (TypeError, ValueError):
|
||||
return DEFAULT_RPA_UPLOAD_MAX_ATTEMPTS
|
||||
return max(parsed, 1)
|
||||
Reference in new issue
Block a user