From e6a6182a0cab6974ebdf24e2c7e5ce19747ac8cb Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 10 Jun 2026 20:06:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .agent/skills/new-async-task/SKILL.md | 9 ++--- .../references/CODE-EXAMPLES.md | 35 ++++++------------- .../common/admin/task-executions.tsx | 2 +- internal/apps/admin/task/routers.go | 2 +- internal/task/errs.go | 2 +- internal/task/executor.go | 5 +-- 6 files changed, 20 insertions(+), 35 deletions(-) diff --git a/.agent/skills/new-async-task/SKILL.md b/.agent/skills/new-async-task/SKILL.md index 90b18193..91e0a26b 100644 --- a/.agent/skills/new-async-task/SKILL.md +++ b/.agent/skills/new-async-task/SKILL.md @@ -56,12 +56,9 @@ Admin dispatch -> ValidateAndNormalizePayload -> DispatchTask - 在 `internal/task/worker/worker.go` 的 Asynq mux 中添加 `mux.HandleFunc(task.YourAsynqTask, task.ProcessTask)`。 - 所有业务任务都应交给 `task.ProcessTask`,由 executor 根据 task type 分发到 handler。 -5. 如需 Cron 调度,补齐配置链路。 - - 在 `internal/task/scheduler/scheduler.go` 注册 cron。 - - 在 `internal/config/model.go` 添加 scheduler config 字段。 - - 在 `config.example.yaml` 添加对应配置项。 - - runtime 代码从 `config.Config` 读取配置,不直接读环境变量。 - - Scheduler 直接入队的任务可能没有 Admin 创建的 `TaskExecution` 记录;需要可见执行记录时,优先通过 Admin dispatch 触发。 +5. 如需 Cron 调度,系统默认定时任务必须通过 SQL 迁移(goose)初始化。 + - 确保任务在 `DispatchableTasks` 中已正确配置 `TaskMeta`。 + - 在 `internal/db/migrator/goose/postgres` 和 `sqlite` 下编写 migration 脚本,使用 `INSERT INTO schedules` 语句初始化任务,指定 `task_type` 和 `cron` 等字段。必须妥善处理冲突(如 `ON CONFLICT DO NOTHING`)以支持幂等。 6. 如改动 Admin API。 - handler 放在 `internal/apps/admin//` 或现有 Admin task 模块内。 diff --git a/.agent/skills/new-async-task/references/CODE-EXAMPLES.md b/.agent/skills/new-async-task/references/CODE-EXAMPLES.md index 20c8795d..f4758b07 100644 --- a/.agent/skills/new-async-task/references/CODE-EXAMPLES.md +++ b/.agent/skills/new-async-task/references/CODE-EXAMPLES.md @@ -199,34 +199,21 @@ func StartWorker() error { ## Cron 调度和配置 -如果任务需要定时运行,补齐 scheduler、config struct 和 `config.example.yaml`。 +系统默认的定时任务必须通过 Goose SQL 迁移初始化插入到 `schedules` 表。 -```go -const ( - cleanupDedupWindow = 23 * time.Hour - cleanupMaxRetry = 3 -) +在 `internal/db/migrator/goose/postgres` 下的示例: -if _, err = scheduler.Register( - config.Config.Scheduler.CleanupUnusedUploadsTaskCron, - asynq.NewTask(task.CleanupUnusedUploadsTask, nil), - asynq.Unique(cleanupDedupWindow), - asynq.MaxRetry(cleanupMaxRetry), -); err != nil { - return -} +```sql +-- +goose Up +INSERT INTO schedules (id, name, task_type, cron, payload, is_active, created_at, updated_at) +VALUES (1, '清理未使用上传', 'cleanup_unused_uploads', '0 */2 * * *', '{}', TRUE, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) +ON CONFLICT (id) DO NOTHING; + +-- +goose Down +-- 根据业务需求决定是否需要在此删除 ``` -```go -type schedulerConfig struct { - CleanupUnusedUploadsTaskCron string `mapstructure:"cleanup_unused_uploads_task_cron"` -} -``` - -```yaml -scheduler: - cleanup_unused_uploads_task_cron: "0 */2 * * *" -``` +对于 `sqlite` 也可以使用类似的 `INSERT INTO ... ON CONFLICT(id) DO NOTHING` 语法。数据库更新后,后端会自动热重载调度器。 ## Handler 测试 diff --git a/frontend/components/common/admin/task-executions.tsx b/frontend/components/common/admin/task-executions.tsx index 100208ec..a3538c87 100644 --- a/frontend/components/common/admin/task-executions.tsx +++ b/frontend/components/common/admin/task-executions.tsx @@ -379,7 +379,7 @@ export function TaskExecutionsManager() { {detailLoading ? : } 刷新详情 - {selectedExecution?.status === "failed" && selectedExecution.retryable && selectedExecution.retry_count < selectedExecution.max_retry && ( + {selectedExecution && selectedExecution.status !== "pending" && selectedExecution.status !== "running" && selectedExecution.retryable && selectedExecution.retry_count < selectedExecution.max_retry && (