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

38
internal/service/cron.go Normal file
View File

@@ -0,0 +1,38 @@
package service
import (
"context"
)
// CronService 定义了定时任务相关的业务接口
type CronService interface {
// StartAllJobs 启动所有定时任务
StartAllJobs(ctx context.Context) error
// StopAllJobs 停止所有定时任务
StopAllJobs(ctx context.Context) error
// AddJob 添加定时任务
AddJob(ctx context.Context, name, cron string, job func()) error
// RemoveJob 移除定时任务
RemoveJob(ctx context.Context, name string) error
// GetJobStatus 获取任务状态
GetJobStatus(ctx context.Context, name string) (bool, error)
}
var cronService CronService
// Cron 返回 CronService 的实例
func Cron() CronService {
if cronService == nil {
panic("implement not found for interface CronService")
}
return cronService
}
// SetCron 注册 CronService 实现
func SetCron(s CronService) {
cronService = s
}