feat(schedule): 优化排程任务配置与进度契约处理逻辑
This commit is contained in:
1 parent
d54f51ddc3
commit
99453aa52a
65 files changed
+2837
-487
No files matched your search
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
backend/app/services/schedule/job_registry.py
|
||||
|
||||
用途:平台内置排程 Job Registry(key → handler)。
|
||||
依赖:shipping_inventory_rpa_job、faq_hot_aggregate_job、fixed_assets_snapshot_job。
|
||||
逻辑:internal_jobs API 与 platform_job 排程通过 platform_api_path 引用已注册 key。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable
|
||||
|
||||
from app.models import ScheduleRun, ScheduleTask
|
||||
from app.services.schedule.schedule_contract import normalize_job_key
|
||||
|
||||
ScheduleExecuteResult = tuple[int, int, int, int, list[dict[str, Any]], list[str], list[dict[str, Any]]]
|
||||
PlatformJobHandler = Callable[[ScheduleTask, ScheduleRun], ScheduleExecuteResult]
|
||||
|
||||
_REGISTRY: dict[str, "PlatformJobDefinition"] = {}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PlatformJobDefinition:
|
||||
"""平台内置 Job 元数据与执行 handler。"""
|
||||
|
||||
key: str
|
||||
label: str
|
||||
handler: PlatformJobHandler
|
||||
description: str = ""
|
||||
# 前端 i18n key(如 admin.schedule.form.platformJobShippingInventory);
|
||||
# 前端下拉优先 $t(label_key),label 仅作 fallback/debug。
|
||||
label_key: str = ""
|
||||
|
||||
|
||||
def register_platform_job(definition: PlatformJobDefinition) -> None:
|
||||
"""注册平台 Job;重复 key 覆盖(便于热重载测试)。"""
|
||||
key = normalize_job_key(definition.key)
|
||||
if not key:
|
||||
raise ValueError("platform job key is required")
|
||||
_REGISTRY[key] = PlatformJobDefinition(
|
||||
key=key,
|
||||
label=definition.label,
|
||||
handler=definition.handler,
|
||||
description=definition.description,
|
||||
label_key=definition.label_key,
|
||||
)
|
||||
|
||||
|
||||
def get_platform_job(key: str) -> PlatformJobDefinition | None:
|
||||
"""按 key 查询已注册 Job。"""
|
||||
return _REGISTRY.get(normalize_job_key(key))
|
||||
|
||||
|
||||
def list_platform_jobs() -> list[PlatformJobDefinition]:
|
||||
"""返回全部已注册 Job(按 key 排序)。"""
|
||||
return [_REGISTRY[k] for k in sorted(_REGISTRY)]
|
||||
|
||||
|
||||
def is_registered_platform_job(key: str) -> bool:
|
||||
"""判断 key 是否已注册。"""
|
||||
return normalize_job_key(key) in _REGISTRY
|
||||
|
||||
|
||||
def run_platform_job(
|
||||
key: str,
|
||||
task: ScheduleTask,
|
||||
run: ScheduleRun,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> ScheduleExecuteResult:
|
||||
"""
|
||||
执行已注册平台 Job。
|
||||
Args:
|
||||
key: platform_api_path job key
|
||||
task: 排程任务
|
||||
run: 执行批次
|
||||
payload: internal API 请求体(部分 handler 可读 agent_id 等)
|
||||
Returns:
|
||||
ScheduleExecuteResult 七元组
|
||||
Raises:
|
||||
KeyError: 未注册 key
|
||||
"""
|
||||
definition = get_platform_job(key)
|
||||
if not definition:
|
||||
raise KeyError(f"Unknown platform job: {key}")
|
||||
_ = payload # 预留扩展;FAQ 等从 task 读取 dify_agent_id
|
||||
return definition.handler(task, run)
|
||||
|
||||
|
||||
def _register_builtin_platform_jobs() -> None:
|
||||
"""模块加载时注册内置平台 Job。"""
|
||||
from app.services.schedule.faq_hot_aggregate_job import run_faq_hot_aggregate_schedule
|
||||
from app.services.schedule.fixed_assets_snapshot_job import run_fixed_assets_snapshot_schedule
|
||||
from app.services.schedule.shipping_inventory_rpa_job import run_shipping_inventory_rpa_schedule
|
||||
|
||||
register_platform_job(PlatformJobDefinition(
|
||||
key="shipping-inventory-rpa",
|
||||
label="货代库存 RPA",
|
||||
label_key="admin.schedule.form.platformJobShippingInventory",
|
||||
handler=run_shipping_inventory_rpa_schedule,
|
||||
description="批量写入库存 RPA export trigger,由 freight_rpa_worker 异步导出。",
|
||||
))
|
||||
register_platform_job(PlatformJobDefinition(
|
||||
key="faq-aggregate-hot",
|
||||
label="FAQ 热点聚合",
|
||||
label_key="admin.schedule.form.platformJobFaqHotAggregate",
|
||||
handler=run_faq_hot_aggregate_schedule,
|
||||
description="ChatLog 热点发现,结果写入 faq_hot_candidates。",
|
||||
))
|
||||
register_platform_job(PlatformJobDefinition(
|
||||
key="fixed-assets-snapshot",
|
||||
label="固资快照",
|
||||
label_key="admin.schedule.form.platformJobFixedAssetsSnapshot",
|
||||
handler=run_fixed_assets_snapshot_schedule,
|
||||
description="同步 PLM 固资快照至本地 manifest。",
|
||||
))
|
||||
|
||||
|
||||
_register_builtin_platform_jobs()
|
||||
Reference in new issue
Block a user