feat(schedule): 任务支持「每天定时 / 每周定时」,用选择器而不是手写 cron
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

- schedule.py: 新增调度计算模块(纯函数,便于单测)。三种模式:interval(每 N 分钟)/ daily(选钟点)/ weekly(选星期 + 钟点)
- 钟点模式是「固定时刻」而非「固定延迟」——从日历重算,所以某轮跑晚了不会把之后每一轮都拖晚。interval 保持原语义:从上一轮开始计时
- 抖动只加给 interval。给「每天 9:00」也加抖动就成了 9:00–9:01 随机触发,操作者选的时间被悄悄改掉,只会像 bug
- 模型 / schema / service: 新增 schedule_mode / schedule_hours / schedule_days / schedule_minute。时钟字段存逗号分隔文本——几个小整数、永远整体读写,单开一张表只会换来 join。interval_minutes 保留且仍是默认值,已有任务不受影响
- service: 改动任何调度字段都按合并后的状态重算 next_run_at。重新启用也算改动,否则停用一个月再打开会带着一个月前的 next_run_at,一保存就立即触发
- 前端: 运行方式三选一 + 小时/星期胶囊多选 + 分钟下拉,并实时预览结果句子。任务卡片改显示后端拼好的 schedule_label,避免列表和编辑器对同一计划给出两种说法
- 校验: 钟点模式至少选一个时间,按周至少选一个星期

tests/test_schedule.py 新增 24 个用例,含「恰好等于当前时刻的档位归属下一天」这个会让调度器自循环的边界。
This commit is contained in:
2026-10-07 15:33:17 +08:00
parent 242e3a7837
commit 8f4e5586e9
10 changed files with 776 additions and 22 deletions
+36
View File
@@ -1,5 +1,7 @@
/** Formatting helpers for the monitoring dashboard. */
import type { ScheduleMode } from '@/types/monitor'
/** Compact count for display: mirrors how the platform itself abbreviates. */
export function formatCount(value: number | null | undefined): string {
if (value === null || value === undefined) return '—'
@@ -42,3 +44,37 @@ export function formatInterval(minutes: number): string {
if (minutes % 60 === 0) return `${minutes / 60} 小时`
return `${minutes} 分钟`
}
const WEEKDAY_NAMES = '一二三四五六日'
/**
* Preview of a schedule, for the editor only.
*
* The task list renders the backend's own `schedule_label` instead. This exists
* so the current selection can be read back before it is saved -- which means the
* two are the same sentence written twice, and a change to one belongs in both.
*/
export function describeSchedule(
mode: ScheduleMode,
intervalMinutes: number,
hours: number[],
days: number[],
minute: number,
): string {
const pad = (value: number) => String(value).padStart(2, '0')
const sorted = (values: number[]) => [...values].sort((a, b) => a - b)
if (mode === 'interval') return `每 ${formatInterval(intervalMinutes)}`
if (hours.length === 0) return '未设置时间'
const clock = sorted(hours)
.map((hour) => `${pad(hour)}:${pad(minute)}`)
.join('、')
if (mode === 'daily' || days.length === 0) return `每天 ${clock}`
const labels = sorted(days)
.map((day) => `周${WEEKDAY_NAMES[day]}`)
.join('、')
return `${labels} ${clock}`
}