feat(schedule): 优化排程任务配置与进度契约处理逻辑

This commit is contained in:
hugo committed 2026-07-08 16:09:28 +08:00
1 parent d54f51ddc3
commit 99453aa52a
65 files changed
+2837 -487

No files matched your search

+51 -19
View File
@@ -21,15 +21,21 @@ ScheduleExecutor = Callable[[ScheduleTask, ScheduleRun], ScheduleExecuteResult]
def normalize_platform_api_path(path: str | None) -> str:
"""
归一化 platform job key 或完整 URL。
核心逻辑:http(s) 完整 URL 原样返回(外部集成端点);
其余值统一委托 schedule_contract.normalize_job_key(契约层与执行层单一归一化入口)。
"""
value = str(path or "").strip()
if not value:
return ""
parsed = urlparse(value)
if parsed.scheme in {"http", "https"}:
return value
if not value.startswith("/"):
value = "/" + value
return value
from app.services.schedule.schedule_contract import normalize_job_key
return normalize_job_key(value)
def _build_srm_conn_str() -> str:
@@ -67,11 +73,38 @@ def _resolve_schedule_user(task: ScheduleTask) -> str:
return str(getattr(task.owner, "username", "") or task.owner_user_id or "schedule_system")
def _run_platform_job_in_process(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecuteResult:
"""
进程内执行已注册平台 Job(无需 HTTP,也不依赖 INTERNAL_API_BASE_URL)。
核心逻辑:从 job_registry 按 platform_api_path 查 handler 直接调用。
"""
from app.services.schedule.job_registry import run_platform_job
key = normalize_platform_api_path(task.platform_api_path)
payload = {
"run_no": run.run_no,
"schedule_task_id": task.id,
"schedule_name": task.name,
"dify_agent_id": task.dify_agent_id,
}
run.api_request_json = payload
db.session.commit()
return run_platform_job(key, task, run, payload)
def _run_common_executor(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecuteResult:
endpoint = normalize_platform_api_path(task.platform_api_path)
if not endpoint:
raise RuntimeError("platform_api_path is missing")
# [STEP 1] 普通 job key(非完整 URL)优先进程内直接调 registry,避免 HTTP 与 base URL 依赖
if not (endpoint.startswith("http://") or endpoint.startswith("https://")):
from app.services.schedule.job_registry import get_platform_job
if get_platform_job(endpoint):
return _run_platform_job_in_process(task, run)
# [STEP 2] 完整 URL(外部集成)仍走 HTTP;base URL 仅用于补全相对路径
base = str(current_app.config.get("INTERNAL_API_BASE_URL") or "").strip().rstrip("/")
if endpoint.startswith("http://") or endpoint.startswith("https://"):
url = endpoint
@@ -79,7 +112,9 @@ def _run_common_executor(task: ScheduleTask, run: ScheduleRun) -> ScheduleExecut
name = endpoint.lstrip("/")
url = f"{base}/api/internal/jobs/{name}"
else:
url = endpoint
raise RuntimeError(
f"platform_api_path '{endpoint}' is not a registered job and INTERNAL_API_BASE_URL is not configured"
)
payload = {
"run_no": run.run_no,
@@ -185,6 +220,10 @@ def _run_generic_check_job(task: ScheduleTask, run: ScheduleRun) -> ScheduleExec
_build_record_text,
)
dst = getattr(task, "data_source_type", None)
if dst not in ("database", "external_api"):
raise RuntimeError(f"Invalid data_source_type for data_pipeline: {dst!r}")
cfg = get_schedule_sql_config_by_task_id(task.id)
if not cfg:
return 0, 0, 0, 0, [], [], []
@@ -263,25 +302,18 @@ def _run_generic_check_job(task: ScheduleTask, run: ScheduleRun) -> ScheduleExec
def list_registered_api_paths() -> list[dict[str, str]]:
from app.models import ScheduleTask
results: list[dict[str, str]] = []
tasks = ScheduleTask.query.filter(ScheduleTask.platform_api_path.isnot(None)).all()
for t in tasks:
results.append({"value": t.platform_api_path, "label": t.name or str(t.id)})
return results
from app.services.schedule.job_registry import list_platform_jobs
return [{"value": job.key, "label": job.label} for job in list_platform_jobs()]
def resolve_schedule_executor(task: ScheduleTask) -> ScheduleExecutor:
dst = getattr(task, "data_source_type", None) or "database"
if dst == "faq_hot_aggregate":
from app.services.schedule.faq_hot_aggregate_job import run_faq_hot_aggregate_schedule
return run_faq_hot_aggregate_schedule
if dst == "shipping_inventory_rpa":
from app.services.schedule.shipping_inventory_rpa_job import run_shipping_inventory_rpa_schedule
return run_shipping_inventory_rpa_schedule
if dst in ("database", "external_api"):
st = getattr(task, "schedule_type", None) or "data_pipeline"
if st == "data_pipeline":
return _run_generic_check_job
return _run_common_executor
if st == "platform_job":
return _run_common_executor
raise RuntimeError(f"Unsupported schedule_type: {st}")
_SCHEDULE_HEADER_REGISTRY: dict[str, tuple[list[str], dict[str, str]]] = {}