This commit is contained in:
sophie committed 2026-09-08 08:58:14 +08:00
commit 21d41acf50
732 files changed
+55672 -15251

No files matched your search

@@ -5,7 +5,8 @@ backend/app/services/schedule/scheduler_service.py
依赖:schedule_contract、job_params、cron_util、timezone_util、excel_service、wecom_notify、kb_sync。
逻辑:api/schedule/tasks.py 与 import_service 调用 create_task/update_task/enable_task;
job_params 在本层经 normalize_job_params_for_contract 唯一归一化(含切换 platform_api_path
未传参时重置默认值);schedule worker 调用 run_task_now 并经 resolve_schedule_executor 分发 handler。
未传参时重置默认值);schedule worker 调用 run_task_now 并经 resolve_schedule_executor 分发 handler;
handler 抛错时 failed_count=1,排程记 failed。
"""
from __future__ import annotations
@@ -43,20 +44,16 @@ def request_cancel_run(run_id: int) -> None:
持久化取消指定排程运行,供 Web 与独立 worker 跨进程协作。
核心逻辑:将对应 queued/running 通用 Job 标记为 cancelled;执行器通过
ScheduleRun 状态/存在性复检停止可中断的长循环。
ScheduleRun 状态/存在性复检停止可中断的长循环。business_ref 兼容
`schedule_run:{id}` 与 `schedule_run:{id}:{platform_api_path}`。
"""
from app.jobs.types import JobType
from app.models import Job
from app.services.schedule.queue_service import query_jobs_for_schedule_run
now = datetime.utcnow()
jobs = (
db.session.query(Job)
.filter(
Job.domain == "schedule",
Job.job_type == JobType.SCHEDULE_RUN.value,
Job.business_ref == f"schedule_run:{int(run_id)}",
Job.status.in_(("queued", "running")),
)
query_jobs_for_schedule_run(run_id)
.filter(Job.status.in_(("queued", "running")))
.all()
)
for job in jobs:
@@ -493,6 +490,11 @@ def enable_task(task: ScheduleTask, *, commit: bool = True, check_options: bool
def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id: int | None, run: ScheduleRun | None = None) -> ScheduleRun:
"""
立即执行一条排程并写入 ScheduleRun 终态。
核心逻辑:handler 未捕获异常记 failed_count=1,避免配置错误被标成 success。
"""
recover_stale_awaiting_rpa_schedule_runs(task)
recover_stale_receipt_rpa_schedule_runs(task)
@@ -527,7 +529,7 @@ def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id:
executor = resolve_schedule_executor(task)
total_count, success_count, failed_count, skipped_count, details, errors, step_logs = executor(task, run)
except Exception as exc:
total_count, success_count, failed_count, skipped_count, details, errors = 0, 0, 0, 0, [], [str(exc)]
total_count, success_count, failed_count, skipped_count, details, errors = 0, 0, 1, 0, [], [str(exc)]
step_logs = [{"step": "executor_error", "status": "failed", "detail": {"error": str(exc)}}]
try:
@@ -564,8 +566,15 @@ def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id:
duration_ms = int((time.time() - start_ts) * 1000)
if current_app:
current_app.logger.info(
"[schedule-run] run_no=%s total=%d success=%d failed=%d skipped=%d errors=%d",
run.run_no, total_count, success_count, failed_count, skipped_count, len(errors),
"[schedule-run] name=%s path=%s run_no=%s total=%d success=%d failed=%d skipped=%d errors=%d",
task.name,
task.platform_api_path,
run.run_no,
total_count,
success_count,
failed_count,
skipped_count,
len(errors),
)
if total_count == 0 and not errors:
status = "success"