feat(cron): 实现定时任务管理功能

- 新增 cron模块,支持定时任务管理- 实现了任务列表获取、任务添加、任务移除和任务状态获取等接口
- 添加了默认任务,包括数据同步、数据清理、健康检查和缓存刷新等
- 实现了优雅关闭功能,确保在服务停止时正确停止所有任务
- 添加了定时任务相关文档和使用指南
This commit is contained in:
hu xiaotong
2025-06-23 15:19:38 +08:00
parent 89a6cdc001
commit cecb19e497
16 changed files with 1571 additions and 7 deletions

60
api/cron/v1/cron.go Normal file
View File

@@ -0,0 +1,60 @@
package v1
import (
"github.com/gogf/gf/v2/frame/g"
)
// GetJobListReq 获取任务列表请求
type GetJobListReq struct {
g.Meta `path:"/cron/jobs" method:"get" tags:"Cron" summary:"Get all cron jobs"`
}
// GetJobListRes 获取任务列表响应
type GetJobListRes struct {
Jobs []*JobInfo `json:"jobs" dc:"任务列表"`
}
// JobInfo 任务信息
type JobInfo struct {
Name string `json:"name" dc:"任务名称"`
Cron string `json:"cron" dc:"Cron表达式"`
Status bool `json:"status" dc:"任务状态"`
NextTime string `json:"nextTime" dc:"下次执行时间"`
}
// AddJobReq 添加任务请求
type AddJobReq struct {
g.Meta `path:"/cron/jobs" method:"post" tags:"Cron" summary:"Add a new cron job"`
Name string `json:"name" v:"required" dc:"任务名称"`
Cron string `json:"cron" v:"required" dc:"Cron表达式"`
}
// AddJobRes 添加任务响应
type AddJobRes struct {
Success bool `json:"success" dc:"是否成功"`
Message string `json:"message" dc:"响应消息"`
}
// RemoveJobReq 移除任务请求
type RemoveJobReq struct {
g.Meta `path:"/cron/jobs/{name}" method:"delete" tags:"Cron" summary:"Remove a cron job"`
Name string `json:"name" v:"required" dc:"任务名称"`
}
// RemoveJobRes 移除任务响应
type RemoveJobRes struct {
Success bool `json:"success" dc:"是否成功"`
Message string `json:"message" dc:"响应消息"`
}
// GetJobStatusReq 获取任务状态请求
type GetJobStatusReq struct {
g.Meta `path:"/cron/jobs/{name}/status" method:"get" tags:"Cron" summary:"Get job status"`
Name string `json:"name" v:"required" dc:"任务名称"`
}
// GetJobStatusRes 获取任务状态响应
type GetJobStatusRes struct {
Name string `json:"name" dc:"任务名称"`
Status bool `json:"status" dc:"任务状态"`
}