feat: 加固运行期可靠性并补齐调度与观测接口

- 运行句柄按 run_id 归属,修复取消后重跑时闸门被旧运行删除的竞态
- 调度循环改用取消令牌退出,启动时对账遗留运行,校验 rows_affected
- 三类任务统一参与调度,新增 /api/v1/stats 与 /api/v1/schedule
- SQLite 启用 WAL 与 busy_timeout,日志清理跳过活跃文件,单行上限 256 KiB
- 前端缓存管理令牌并在 403 时重试一次,仪表盘改用统计接口
This commit is contained in:
2026-10-26 00:38:16 +08:00
parent 773196d689
commit 815258c19b
19 changed files with 990 additions and 93 deletions
+35 -2
View File
@@ -15,6 +15,38 @@ const TASK_COLUMNS: &str = "
created_at, updated_at
";
#[derive(Debug, Clone, Copy, Default)]
pub struct TaskCounts {
pub total: u64,
pub enabled: u64,
pub script: u64,
pub http: u64,
pub process: u64,
}
/// 单条 SQL 统计全部指标,供仪表盘使用。
pub async fn counts(pool: &SqlitePool) -> ApiResult<TaskCounts> {
let (total, enabled, script, http, process): (i64, i64, i64, i64, i64) = sqlx::query_as(
"SELECT
COUNT(*),
COALESCE(SUM(enabled), 0),
COALESCE(SUM(action_type = 'script'), 0),
COALESCE(SUM(action_type = 'http'), 0),
COALESCE(SUM(action_type = 'process'), 0)
FROM tasks",
)
.fetch_one(pool)
.await?;
Ok(TaskCounts {
total: u64::try_from(total).unwrap_or_default(),
enabled: u64::try_from(enabled).unwrap_or_default(),
script: u64::try_from(script).unwrap_or_default(),
http: u64::try_from(http).unwrap_or_default(),
process: u64::try_from(process).unwrap_or_default(),
})
}
pub struct ListFilters {
pub q: Option<String>,
pub enabled: Option<bool>,
@@ -233,14 +265,15 @@ pub async fn list(pool: &SqlitePool, filters: &ListFilters) -> ApiResult<TaskLis
})
}
pub async fn list_enabled_script_tasks(pool: &SqlitePool) -> ApiResult<Vec<TaskResponse>> {
/// 启动时恢复调度用的列表:所有启用任务都参与调度,不再只限脚本。
pub async fn list_enabled_tasks(pool: &SqlitePool) -> ApiResult<Vec<TaskResponse>> {
let rows = sqlx::query_as::<_, TaskRow>(
"SELECT
id, name, cron, timezone, enabled, timeout_secs, concurrency_policy,
retry_max_attempts, retry_delay_secs, action_type, action_json,
created_at, updated_at
FROM tasks
WHERE enabled = 1 AND action_type = 'script'
WHERE enabled = 1
ORDER BY updated_at DESC, id ASC",
)
.fetch_all(pool)