From c85322bcb0683a179a5cc545f32cce72284c767b Mon Sep 17 00:00:00 2001 From: engigu Date: Tue, 24 Mar 2026 11:48:11 +0800 Subject: [PATCH] chore: udpate cron expr limit --- internal/executor/cron.go | 19 ++++++++++++++++- web/src/views/tasks/TaskDialog.vue | 34 ++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/internal/executor/cron.go b/internal/executor/cron.go index 8bdb97a..ab2c038 100644 --- a/internal/executor/cron.go +++ b/internal/executor/cron.go @@ -2,6 +2,8 @@ package executor import ( "math/rand" + "strings" + "fmt" "sync" "time" @@ -61,6 +63,7 @@ func (m *CronManager) Stop() { m.logger.Infof("[CronManager] 调度管理服务已停止") } + // AddTask 添加或更新计划任务 func (m *CronManager) AddTask(task CronTask) error { m.mu.Lock() @@ -83,7 +86,8 @@ func (m *CronManager) AddTask(task CronTask) error { languages := task.GetLanguages() useMise := task.UseMise() - entryID, err := m.cron.AddFunc(task.GetSchedule(), func() { + schedule := strings.TrimSpace(task.GetSchedule()) + entryID, err := m.cron.AddFunc(schedule, func() { defer func() { if r := recover(); r != nil { m.logger.Errorf("[CronManager] 任务 #%s 执行过程中发生 Panic: %v", taskID, r) @@ -182,6 +186,19 @@ func (m *CronManager) triggerNextRunEvent(taskID string, req *ExecutionRequest) // ValidateCron 校验 Cron 表达式 func (m *CronManager) ValidateCron(expression string) error { + expression = strings.TrimSpace(expression) + if expression == "" { + return fmt.Errorf("cron 表达式不能为空") + } + + // 如果不是以 @ 开头的描述符,检查位数 + if !strings.HasPrefix(expression, "@") { + fields := strings.Fields(expression) + if len(fields) != 6 { + return fmt.Errorf("cron 表达式必须为 6 位 (秒 分 时 日 月 周)") + } + } + parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor) _, err := parser.Parse(expression) return err diff --git a/web/src/views/tasks/TaskDialog.vue b/web/src/views/tasks/TaskDialog.vue index 48378ba..ecbfc4d 100644 --- a/web/src/views/tasks/TaskDialog.vue +++ b/web/src/views/tasks/TaskDialog.vue @@ -62,12 +62,21 @@ const cronDescription = computed(() => { return getCronDescription(form.value.schedule, (navigator as any).language) }) +const isCronValid = computed(() => { + if (!form.value.schedule) return true + const s = form.value.schedule.trim() + if (s.startsWith('@')) return true + const fields = s.split(/\s+/).filter(Boolean) + return fields.length === 6 +}) + // 监听 concurrencyEnabled 的变化,同步到 concurrency watch(concurrencyEnabled, (val) => { concurrency.value = val ? 1 : 0 }) function onConcurrencyChange(val: boolean) { + concurrencyEnabled.value = val } @@ -426,8 +435,10 @@ async function save() { } emit('update:open', false) emit('saved') - } catch (error) { - toast.error('保存失败') + } catch (error: any) { + toast.error('保存失败', { + description: error.message || '未知错误' + }) } } @@ -445,6 +456,7 @@ async function save() { +
@@ -657,7 +669,7 @@ async function save() {
- + @@ -749,14 +761,22 @@ async function save() {
- -
- + + +
+ 表达式位数错误: 必须为 6 位 (秒 分 时 日 月 周) +
+ +
+
+ +
{{ cronDescription }}
- 格式指导: 秒 分 时 日 月 周 + 格式指导: 秒 分 时 日 月 周 (必须使用 6 位表达式)