Files
abot/admin/dashboard/blueprints/system_jobs.py
liuwei 1db8681636 完善后台任务中心历史摘要视图
- 为系统任务和插件调度补充批量历史摘要查询,支持最近成功时间、最近失败原因与累计成功失败次数

- 任务列表接口合并内存运行态与数据库日志态,服务重启后后台仍可回看最近执行结果

- 系统任务页与插件调度页新增健康状态、历史执行摘要与插件调度快捷启停入口

- 更新工程优化文档,记录 7.3 第一阶段当前进展
2026-04-30 16:21:29 +08:00

203 lines
8.8 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.
# -*- coding: utf-8 -*-
from datetime import datetime
from flask import Blueprint, current_app, jsonify, render_template, request
from utils.decorator.async_job import async_job
from .auth import login_required
system_jobs_bp = Blueprint("system_jobs", __name__, url_prefix="/system_jobs")
def _normalize_datetime_text(value):
"""统一时间文本格式为 `YYYY-MM-DD HH:MM:SS`。"""
if value is None:
return value
if isinstance(value, datetime):
return value.strftime("%Y-%m-%d %H:%M:%S")
text = str(value)
if "T" in text:
return text.replace("T", " ")[:19]
return text
def _build_job_health_status(*, enabled: bool, running: bool, last_status: str, latest_success_at, latest_failure_summary: str) -> str:
"""根据任务启停、运行态和历史结果输出后台可读的健康状态。"""
# 状态设计尽量贴近运维判断顺序:
# 1. 停用态单独标记,避免和“从未执行”混淆;
# 2. 执行中的任务优先展示 running方便后台快速识别实时动作
# 3. 最近一次执行失败时直接标记 failed让异常任务在列表里一眼可见
# 4. 有成功历史且最近不是失败时视为 healthy否则落到 idle。
if not enabled:
return "disabled"
if running:
return "running"
if str(last_status or "").strip().lower() == "failed":
return "failed"
if latest_success_at or str(last_status or "").strip().lower() == "success":
return "healthy"
if str(latest_failure_summary or "").strip():
return "failed"
return "idle"
def _build_job_health_message(*, health_status: str, latest_success_at, latest_failure_summary: str) -> str:
"""为后台列表生成一句简短的任务健康提示。"""
if health_status == "disabled":
return "任务已停用"
if health_status == "running":
return "任务正在执行中"
if health_status == "failed":
return str(latest_failure_summary or "最近一次执行失败").strip()
if health_status == "healthy":
success_text = _normalize_datetime_text(latest_success_at)
return f"最近成功于 {success_text}" if success_text else "任务近期执行正常"
return "暂无执行记录"
@system_jobs_bp.route("/")
@login_required
def page_system_jobs():
return render_template("system_jobs.html")
@system_jobs_bp.route("/api/jobs", methods=["GET"])
@login_required
def api_list_jobs():
server = current_app.dashboard_server
db_rows = server.system_job_db.list_jobs()
runtime_rows = async_job.get_jobs_snapshot()
runtime_by_key = {row.get("job_key", ""): row for row in runtime_rows if row.get("job_key")}
job_keys = [str(row.get("job_key") or "").strip() for row in db_rows if str(row.get("job_key") or "").strip()]
latest_log_by_key = server.system_job_db.get_latest_logs_map(job_keys)
history_summary_by_key = server.system_job_db.get_job_history_summary_map(job_keys)
result = []
for row in db_rows:
job_key = row.get("job_key")
runtime = runtime_by_key.get(job_key, {})
latest_log = latest_log_by_key.get(job_key, {})
history_summary = history_summary_by_key.get(job_key, {})
last_status = runtime.get("last_status") or latest_log.get("status") or "never"
last_run_at = runtime.get("last_run_at") or latest_log.get("triggered_at")
last_error = runtime.get("last_error") or ""
if not last_error and str(last_status or "").strip().lower() == "failed":
last_error = (
str(latest_log.get("summary") or "").strip()
or str(history_summary.get("latest_failure_summary") or "").strip()
)
health_status = _build_job_health_status(
enabled=bool(row.get("enabled", 0)),
running=bool(runtime.get("running", False)),
last_status=str(last_status or ""),
latest_success_at=history_summary.get("latest_success_at"),
latest_failure_summary=str(history_summary.get("latest_failure_summary") or ""),
)
result.append(
{
"job_key": job_key,
"name": row.get("name", ""),
"description": row.get("description", ""),
"trigger_type": row.get("trigger_type", ""),
"trigger_config": row.get("trigger_config", {}),
"enabled": bool(row.get("enabled", 0)),
"runtime_job_id": runtime.get("id"),
"runtime_enabled": runtime.get("enabled"),
"running": runtime.get("running", False),
"trigger_text": runtime.get("trigger_text", ""),
"last_run_at": _normalize_datetime_text(last_run_at),
"last_status": last_status,
"last_error": last_error,
"last_duration_ms": runtime.get("last_duration_ms") or latest_log.get("duration_ms"),
"next_run_at": _normalize_datetime_text(runtime.get("next_run_at")),
"run_count": runtime.get("run_count", 0),
"success_count": runtime.get("success_count", 0),
"fail_count": runtime.get("fail_count", 0),
"latest_success_at": _normalize_datetime_text(history_summary.get("latest_success_at")),
"latest_failed_at": _normalize_datetime_text(history_summary.get("latest_failed_at")),
"latest_failure_summary": str(history_summary.get("latest_failure_summary") or "").strip(),
"history_success_count": int(history_summary.get("history_success_count", 0) or 0),
"history_fail_count": int(history_summary.get("history_fail_count", 0) or 0),
"history_total_count": int(history_summary.get("history_total_count", 0) or 0),
"health_status": health_status,
"health_message": _build_job_health_message(
health_status=health_status,
latest_success_at=history_summary.get("latest_success_at"),
latest_failure_summary=str(history_summary.get("latest_failure_summary") or "").strip(),
),
}
)
return jsonify({"success": True, "data": result})
@system_jobs_bp.route("/api/jobs/<job_key>", methods=["PUT"])
@login_required
def api_update_job(job_key: str):
server = current_app.dashboard_server
payload = request.get_json(silent=True) or {}
updates = {}
for key in ("name", "description", "trigger_type", "trigger_config", "enabled"):
if key in payload:
updates[key] = payload[key]
if not updates:
return jsonify({"success": False, "message": "没有可更新字段"}), 400
ok = server.system_job_db.update_job(job_key, updates)
if not ok:
return jsonify({"success": False, "message": "数据库更新失败"}), 500
# 配置变更后立即重载调度器,确保实时生效
server.system_job_loader.reload_from_db()
return jsonify({"success": True, "message": "更新成功"})
@system_jobs_bp.route("/api/jobs/<job_key>/trigger", methods=["POST"])
@login_required
def api_trigger_job(job_key: str):
server = current_app.dashboard_server
job_id = async_job.get_job_id_by_key(job_key)
if not job_id:
server.system_job_loader.reload_from_db()
job_id = async_job.get_job_id_by_key(job_key)
if not job_id:
return jsonify({"success": False, "message": "任务未加载或已禁用"}), 404
ok, msg = async_job.trigger_job_now(job_id, operator="dashboard")
code = 200 if ok else 400
return jsonify({"success": ok, "message": msg}), code
@system_jobs_bp.route("/api/jobs/<job_key>/logs", methods=["GET"])
@login_required
def api_job_logs(job_key: str):
server = current_app.dashboard_server
limit = int(request.args.get("limit", 100))
db_logs = server.system_job_db.get_job_logs(job_key, limit=limit)
# 为了兼容前端既有表头(time/level/message),这里做一层字段映射。
logs = []
for row in db_logs:
status = str(row.get("status") or "")
level = "error" if status == "failed" else ("success" if status == "success" else "info")
logs.append(
{
"time": _normalize_datetime_text(row.get("triggered_at")),
"level": level,
"message": row.get("summary") or "",
"status": status,
"duration_ms": row.get("duration_ms"),
"detail_json": row.get("detail_json") or {},
}
)
return jsonify({"success": True, "data": logs})
@system_jobs_bp.route("/api/reload", methods=["POST"])
@login_required
def api_reload_jobs():
server = current_app.dashboard_server
server.system_job_loader.reload_from_db()
return jsonify({"success": True, "message": "已按数据库配置重载系统定时任务"})