2023-11-27 22:37:33 +08:00
|
|
|
package timerx
|
2023-08-27 23:39:58 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2024-05-22 15:02:39 +08:00
|
|
|
"fmt"
|
2023-08-27 23:39:58 +08:00
|
|
|
"runtime/debug"
|
2025-09-18 21:18:35 +08:00
|
|
|
"strconv"
|
2023-08-27 23:39:58 +08:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
"github.com/google/uuid"
|
2025-10-04 22:47:54 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2025-10-05 16:30:41 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-05-22 15:02:39 +08:00
|
|
|
"github.com/yuninks/cachex"
|
|
|
|
|
"github.com/yuninks/lockx"
|
2025-10-04 20:44:16 +08:00
|
|
|
"github.com/yuninks/timerx/heartbeat"
|
|
|
|
|
"github.com/yuninks/timerx/leader"
|
2025-07-24 17:13:17 +08:00
|
|
|
"github.com/yuninks/timerx/logger"
|
2025-07-25 22:58:45 +08:00
|
|
|
"github.com/yuninks/timerx/priority"
|
2023-08-27 23:39:58 +08:00
|
|
|
)
|
|
|
|
|
|
2023-11-13 23:49:42 +08:00
|
|
|
// 功能描述
|
2024-04-04 10:58:57 +08:00
|
|
|
|
2023-11-13 23:49:42 +08:00
|
|
|
// 这是基于Redis的定时任务调度器,能够有效的在服务集群里面调度任务,避免了单点压力过高或单点故障问题
|
|
|
|
|
// 由于所有的服务代码是一致的,也就是一个定时任务将在所有的服务都有注册,具体调度到哪个服务运行看调度结果
|
|
|
|
|
|
|
|
|
|
type Cluster struct {
|
2025-09-18 09:56:24 +08:00
|
|
|
ctx context.Context // context
|
2025-09-18 15:34:01 +08:00
|
|
|
cancel context.CancelFunc // 取消函数
|
2025-09-18 09:56:24 +08:00
|
|
|
redis redis.UniversalClient // redis
|
|
|
|
|
timeout time.Duration // job执行超时时间
|
|
|
|
|
logger logger.Logger // 日志
|
|
|
|
|
keyPrefix string // key前缀
|
|
|
|
|
location *time.Location // 根据时区计算的时间
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-09-22 13:51:28 +08:00
|
|
|
lockKey string // 全局计算的key
|
|
|
|
|
zsetKey string // 有序集合的key
|
|
|
|
|
listKey string // 可执行的任务列表的key
|
|
|
|
|
setKey string // 重入集合的key
|
2025-09-19 18:43:21 +08:00
|
|
|
executeInfoKey string // 执行情况的key
|
2025-06-11 15:12:08 +08:00
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
wg sync.WaitGroup // 等待组
|
|
|
|
|
workerList sync.Map // 注册的任务列表
|
|
|
|
|
stopChan chan struct{} //
|
|
|
|
|
instanceId string // 实例ID
|
|
|
|
|
|
2025-07-25 22:58:45 +08:00
|
|
|
priority *priority.Priority // 全局优先级
|
|
|
|
|
priorityKey string // 全局优先级的key
|
2025-09-18 09:56:24 +08:00
|
|
|
usePriority bool // 是否使用优先级
|
|
|
|
|
|
2025-10-05 16:30:41 +08:00
|
|
|
leader *leader.Leader // Leader
|
|
|
|
|
heartbeat *heartbeat.HeartBeat // 心跳
|
|
|
|
|
cache *cachex.Cache // 本地缓存
|
|
|
|
|
cronParser *cron.Parser // cron表达式解析器
|
2025-10-05 21:43:41 +08:00
|
|
|
batchSize int // 批量获取任务的数量
|
2025-10-14 11:13:12 +08:00
|
|
|
workerChan chan struct{} // worker
|
|
|
|
|
maxWorkers int // 最大worker数量
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-04 10:58:57 +08:00
|
|
|
// 初始化定时器
|
|
|
|
|
// 全局只需要初始化一次
|
2025-09-24 14:50:30 +08:00
|
|
|
func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix string, opts ...Option) (*Cluster, error) {
|
|
|
|
|
|
|
|
|
|
if red == nil {
|
|
|
|
|
return nil, errors.New("redis is nil")
|
|
|
|
|
}
|
2024-04-04 10:58:57 +08:00
|
|
|
|
2025-08-23 18:55:59 +08:00
|
|
|
op := newOptions(opts...)
|
|
|
|
|
|
2025-09-18 15:34:01 +08:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2025-09-18 09:56:24 +08:00
|
|
|
|
2025-09-18 21:18:35 +08:00
|
|
|
U, _ := uuid.NewV7()
|
|
|
|
|
|
2025-08-23 18:55:59 +08:00
|
|
|
clu := &Cluster{
|
2025-10-04 20:44:16 +08:00
|
|
|
ctx: ctx,
|
|
|
|
|
cancel: cancel,
|
|
|
|
|
redis: red,
|
|
|
|
|
cache: cachex.NewCache(),
|
|
|
|
|
timeout: op.timeout,
|
|
|
|
|
logger: op.logger,
|
|
|
|
|
keyPrefix: keyPrefix,
|
|
|
|
|
location: op.location,
|
|
|
|
|
lockKey: "timer:cluster_globalLockKey" + keyPrefix, // 定时器的全局锁
|
|
|
|
|
zsetKey: "timer:cluster_zsetKey" + keyPrefix, // 有序集合
|
|
|
|
|
listKey: "timer:cluster_listKey" + keyPrefix, // 列表
|
|
|
|
|
setKey: "timer:cluster_setKey" + keyPrefix, // 重入集合
|
|
|
|
|
priorityKey: "timer:cluster_priorityKey" + keyPrefix, // 全局优先级的key
|
|
|
|
|
executeInfoKey: "timer:cluster_executeInfoKey" + keyPrefix, // 执行情况的key 有序集合
|
2025-10-14 15:07:53 +08:00
|
|
|
usePriority: false,
|
2025-10-04 20:44:16 +08:00
|
|
|
stopChan: make(chan struct{}),
|
|
|
|
|
instanceId: U.String(),
|
2025-10-05 16:30:41 +08:00
|
|
|
cronParser: op.cronParser,
|
2025-10-05 21:43:41 +08:00
|
|
|
batchSize: op.batchSize,
|
2025-10-14 12:13:28 +08:00
|
|
|
workerChan: make(chan struct{}, op.maxWorkers),
|
|
|
|
|
maxWorkers: op.maxWorkers,
|
2025-08-23 18:55:59 +08:00
|
|
|
}
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-08-23 19:00:24 +08:00
|
|
|
// 初始化优先级
|
2025-08-27 15:52:09 +08:00
|
|
|
|
2025-10-14 15:07:53 +08:00
|
|
|
if op.priorityType != priorityTypeNone {
|
|
|
|
|
|
|
|
|
|
clu.usePriority = true
|
|
|
|
|
|
|
|
|
|
if op.priorityType == priorityTypeVersion {
|
|
|
|
|
pVal, err := priority.PriorityByVersion(op.priorityVersion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
clu.logger.Errorf(ctx, "PriorityByVersion version:%s err:%v", op.priorityVersion, err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
op.priorityVal = pVal
|
|
|
|
|
}
|
2025-09-24 14:50:30 +08:00
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
pri, err := priority.InitPriority(
|
|
|
|
|
ctx,
|
|
|
|
|
red,
|
2025-10-04 22:47:54 +08:00
|
|
|
clu.keyPrefix,
|
2025-10-04 20:44:16 +08:00
|
|
|
op.priorityVal,
|
|
|
|
|
priority.WithLogger(clu.logger),
|
|
|
|
|
priority.WithInstanceId(clu.instanceId),
|
|
|
|
|
priority.WithSource("cluster"),
|
|
|
|
|
)
|
2025-09-24 14:50:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
clu.logger.Errorf(ctx, "InitPriority err:%v", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
clu.priority = pri
|
2025-08-27 15:52:09 +08:00
|
|
|
}
|
2025-08-23 18:55:59 +08:00
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
// 初始化leader
|
|
|
|
|
le, err := leader.InitLeader(
|
|
|
|
|
ctx,
|
|
|
|
|
clu.redis,
|
2025-10-04 22:47:54 +08:00
|
|
|
clu.keyPrefix,
|
2025-10-04 20:44:16 +08:00
|
|
|
leader.WithLogger(clu.logger),
|
|
|
|
|
leader.WithPriority(clu.priority),
|
|
|
|
|
leader.WithInstanceId(clu.instanceId),
|
|
|
|
|
leader.WithSource("cluster"),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
clu.logger.Infof(ctx, "InitLeader err:%v", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
clu.leader = le
|
|
|
|
|
|
|
|
|
|
// 初始化心跳
|
|
|
|
|
heart, err := heartbeat.InitHeartBeat(
|
|
|
|
|
ctx,
|
|
|
|
|
clu.redis,
|
|
|
|
|
clu.keyPrefix,
|
|
|
|
|
heartbeat.WithInstanceId(clu.instanceId),
|
|
|
|
|
heartbeat.WithLeader(clu.leader),
|
|
|
|
|
heartbeat.WithLogger(clu.logger),
|
|
|
|
|
heartbeat.WithPriority(clu.priority),
|
2025-10-05 21:43:41 +08:00
|
|
|
heartbeat.WithSource("cluster"),
|
2025-10-04 20:44:16 +08:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
clu.logger.Errorf(ctx, "InitHeartBeat err:%v", err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
clu.heartbeat = heart
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 启动守护进程
|
|
|
|
|
clu.startDaemon()
|
|
|
|
|
|
2025-09-18 21:18:35 +08:00
|
|
|
clu.logger.Infof(ctx, "InitCluster success keyPrefix:%s instanceId:%s", clu.keyPrefix, clu.instanceId)
|
|
|
|
|
|
2025-09-24 14:50:30 +08:00
|
|
|
return clu, nil
|
2025-09-18 09:56:24 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:34:01 +08:00
|
|
|
// Stop 停止集群定时器
|
2025-09-18 21:18:35 +08:00
|
|
|
func (l *Cluster) Stop() {
|
|
|
|
|
close(l.stopChan)
|
2025-10-04 19:00:44 +08:00
|
|
|
if l.usePriority && l.priority != nil {
|
|
|
|
|
l.priority.Close()
|
|
|
|
|
}
|
2025-10-04 21:33:57 +08:00
|
|
|
if l.leader != nil {
|
|
|
|
|
l.leader.Close()
|
|
|
|
|
}
|
|
|
|
|
if l.heartbeat != nil {
|
|
|
|
|
l.heartbeat.Close()
|
|
|
|
|
}
|
|
|
|
|
if l.cancel != nil {
|
|
|
|
|
l.cancel()
|
|
|
|
|
}
|
2025-10-04 20:44:16 +08:00
|
|
|
|
2025-09-18 21:18:35 +08:00
|
|
|
l.wg.Wait()
|
2025-09-18 15:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 守护任务
|
|
|
|
|
func (l *Cluster) startDaemon() {
|
2025-08-23 18:55:59 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 任务调度
|
|
|
|
|
l.wg.Add(1)
|
|
|
|
|
go l.scheduleTasks()
|
2025-08-27 15:52:09 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 任务执行
|
|
|
|
|
l.wg.Add(1)
|
|
|
|
|
go l.executeTasks()
|
|
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
l.wg.Add(1)
|
|
|
|
|
go l.cleanExecuteInfoLoop()
|
2025-09-18 21:18:35 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
func (l *Cluster) cleanExecuteInfoLoop() {
|
|
|
|
|
l.wg.Done()
|
2025-09-18 21:18:35 +08:00
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
ticker := time.NewTicker(time.Minute * 5)
|
2025-09-18 21:18:35 +08:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-l.stopChan:
|
|
|
|
|
return
|
|
|
|
|
case <-l.ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case <-ticker.C:
|
2025-10-04 20:44:16 +08:00
|
|
|
if l.leader.IsLeader() {
|
|
|
|
|
l.cleanExecuteInfo()
|
2025-09-18 21:18:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 20:44:16 +08:00
|
|
|
// 清除过期任务
|
|
|
|
|
func (l *Cluster) cleanExecuteInfo() error {
|
2025-09-19 18:43:21 +08:00
|
|
|
// 移除执行信息
|
|
|
|
|
l.redis.ZRemRangeByScore(l.ctx, l.executeInfoKey, "0", strconv.FormatInt(time.Now().Add(-15*time.Minute).UnixMilli(), 10)).Err()
|
|
|
|
|
return nil
|
2025-09-18 21:18:35 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// scheduleTasks 调度任务(只有leader执行)
|
|
|
|
|
func (c *Cluster) scheduleTasks() {
|
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
|
|
ticker := time.NewTicker(200 * time.Millisecond)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ticker.C:
|
2025-10-04 20:44:16 +08:00
|
|
|
if !c.leader.IsLeader() {
|
2025-09-18 09:56:24 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if c.usePriority && !c.priority.IsLatest(c.ctx) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
c.calculateNextTimes()
|
|
|
|
|
c.moveReadyTasks()
|
|
|
|
|
case <-c.stopChan:
|
|
|
|
|
return
|
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-11 15:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 每月执行一次
|
|
|
|
|
// @param ctx 上下文
|
|
|
|
|
// @param taskId 任务ID
|
|
|
|
|
// @param day 每月的几号
|
|
|
|
|
// @param hour 小时
|
|
|
|
|
// @param minute 分钟
|
|
|
|
|
// @param second 秒
|
|
|
|
|
// @param callback 回调函数
|
|
|
|
|
// @param extendData 扩展数据
|
|
|
|
|
// @return error
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-10-04 18:51:22 +08:00
|
|
|
// nowTime := time.Now().In(c.location)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
jobData := JobData{
|
2025-10-04 18:51:22 +08:00
|
|
|
JobType: JobTypeEveryMonth,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-04 18:51:22 +08:00
|
|
|
// CreateTime: nowTime,
|
|
|
|
|
Day: day,
|
|
|
|
|
Hour: hour,
|
|
|
|
|
Minute: minute,
|
|
|
|
|
Second: second,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 每周执行一次
|
|
|
|
|
// @param ctx context.Context 上下文
|
|
|
|
|
// @param taskId string 任务ID
|
|
|
|
|
// @param week time.Weekday 周
|
|
|
|
|
// @param hour int 小时
|
|
|
|
|
// @param minute int 分钟
|
|
|
|
|
// @param second int 秒
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EveryWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-10-04 18:51:22 +08:00
|
|
|
// nowTime := time.Now().In(c.location)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
jobData := JobData{
|
2025-10-04 18:51:22 +08:00
|
|
|
JobType: JobTypeEveryWeek,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-04 18:51:22 +08:00
|
|
|
// CreateTime: nowTime,
|
|
|
|
|
Weekday: week,
|
|
|
|
|
Hour: hour,
|
|
|
|
|
Minute: minute,
|
|
|
|
|
Second: second,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 每天执行一次
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EveryDay(ctx context.Context, taskId string, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-10-04 18:51:22 +08:00
|
|
|
// nowTime := time.Now().In(c.location)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
jobData := JobData{
|
2025-10-04 18:51:22 +08:00
|
|
|
JobType: JobTypeEveryDay,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-04 18:51:22 +08:00
|
|
|
// CreateTime: nowTime,
|
|
|
|
|
Hour: hour,
|
|
|
|
|
Minute: minute,
|
|
|
|
|
Second: second,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 每小时执行一次
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-10-04 18:51:22 +08:00
|
|
|
// nowTime := time.Now().In(c.location)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
jobData := JobData{
|
2025-10-04 18:51:22 +08:00
|
|
|
JobType: JobTypeEveryHour,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-04 18:51:22 +08:00
|
|
|
// CreateTime: nowTime,
|
|
|
|
|
Minute: minute,
|
|
|
|
|
Second: second,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 每分钟执行一次
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-10-04 18:51:22 +08:00
|
|
|
// nowTime := time.Now().In(c.location)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
jobData := JobData{
|
2025-10-04 18:51:22 +08:00
|
|
|
JobType: JobTypeEveryMinute,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-04 18:51:22 +08:00
|
|
|
// CreateTime: nowTime,
|
|
|
|
|
Second: second,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 特定时间间隔
|
2024-05-31 13:05:51 +08:00
|
|
|
func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2024-05-20 09:35:12 +08:00
|
|
|
|
|
|
|
|
if spaceTime < 0 {
|
|
|
|
|
c.logger.Errorf(ctx, "间隔时间不能小于0")
|
|
|
|
|
return errors.New("间隔时间不能小于0")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 13:40:31 +08:00
|
|
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
|
|
|
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, c.location)
|
2024-05-22 15:02:39 +08:00
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
jobData := JobData{
|
2024-05-22 15:02:39 +08:00
|
|
|
JobType: JobTypeInterval,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2024-05-22 15:02:39 +08:00
|
|
|
BaseTime: zeroTime, // 默认当天的零点
|
2024-05-20 09:35:12 +08:00
|
|
|
IntervalTime: spaceTime,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return c.addJob(ctx, jobData, callback, extendData)
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-05 16:30:41 +08:00
|
|
|
// 定时任务
|
|
|
|
|
// 使用的是秒级cron表达式,可以使用Option设置cronParser
|
|
|
|
|
// @param ctx context.Context 上下文
|
|
|
|
|
// @param taskId string 任务ID
|
|
|
|
|
// @param cronExpression string cron表达式
|
|
|
|
|
// @param callback callback 回调函数
|
|
|
|
|
// @param extendData interface{} 扩展数据
|
|
|
|
|
// @return error
|
|
|
|
|
func (l *Cluster) Cron(ctx context.Context, taskId string, cronExpression string, callback func(ctx context.Context, extendData any) error, extendData any, opt ...Option) error {
|
2025-12-01 13:40:31 +08:00
|
|
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
|
|
|
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, l.location)
|
2025-10-05 16:30:41 +08:00
|
|
|
|
2025-10-10 21:03:00 +08:00
|
|
|
options := newEmptyOptions(opt...)
|
2025-10-05 16:30:41 +08:00
|
|
|
cronParser := l.cronParser
|
|
|
|
|
if options.cronParser != nil {
|
|
|
|
|
cronParser = options.cronParser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sche, err := GetCronSche(cronExpression, cronParser)
|
|
|
|
|
if err != nil {
|
|
|
|
|
l.logger.Errorf(ctx, "Cron cronExpression error:%s", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobData := JobData{
|
|
|
|
|
JobType: JobTypeCron,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: taskId,
|
2025-10-05 16:30:41 +08:00
|
|
|
BaseTime: zeroTime, // 默认当天的零点
|
|
|
|
|
CronExpression: cronExpression,
|
|
|
|
|
CronSchedule: sche,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
return l.addJob(ctx, jobData, callback, extendData)
|
2025-10-05 16:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 09:35:12 +08:00
|
|
|
// 统一添加任务
|
|
|
|
|
// @param ctx context.Context 上下文
|
|
|
|
|
// @param taskId string 任务ID
|
|
|
|
|
// @param jobData *JobData 任务数据
|
|
|
|
|
// @param callback callback 回调函数
|
|
|
|
|
// @param extendData interface{} 扩展数据
|
|
|
|
|
// @return error
|
2025-10-05 19:21:39 +08:00
|
|
|
func (l *Cluster) addJob(ctx context.Context, jobData JobData, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
2025-09-18 09:56:24 +08:00
|
|
|
// 判断是否重复
|
2025-10-05 19:21:39 +08:00
|
|
|
_, ok := l.workerList.Load(jobData.TaskId)
|
2023-08-27 23:39:58 +08:00
|
|
|
if ok {
|
2025-10-05 19:21:39 +08:00
|
|
|
l.logger.Errorf(ctx, "Cluster addJob taskId exits:%s", jobData.TaskId)
|
2025-09-18 09:56:24 +08:00
|
|
|
return ErrTaskIdExists
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 校验时间是否合法
|
|
|
|
|
_, err := GetNextTime(time.Now().In(l.location), jobData)
|
2024-05-20 09:35:12 +08:00
|
|
|
if err != nil {
|
2025-09-18 09:56:24 +08:00
|
|
|
l.logger.Errorf(ctx, "Cluster addJob GetNextTime err:%s", err.Error())
|
2024-05-20 09:35:12 +08:00
|
|
|
return err
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t := timerStr{
|
2023-11-13 23:49:42 +08:00
|
|
|
Callback: callback,
|
|
|
|
|
ExtendData: extendData,
|
2025-10-05 19:21:39 +08:00
|
|
|
TaskId: jobData.TaskId,
|
2024-05-20 09:35:12 +08:00
|
|
|
JobData: &jobData,
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
l.workerList.Store(jobData.TaskId, t)
|
2025-09-18 09:56:24 +08:00
|
|
|
|
2025-10-05 19:21:39 +08:00
|
|
|
l.logger.Infof(ctx, "Cluster addJob taskId:%s", jobData.TaskId)
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
return nil
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算下一次执行的时间
|
2025-09-18 09:56:24 +08:00
|
|
|
func (l *Cluster) calculateNextTimes() {
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
pipe := l.redis.Pipeline()
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2024-05-08 13:01:55 +08:00
|
|
|
// 根据内部注册的任务列表计算下一次执行的时间
|
2025-09-18 09:56:24 +08:00
|
|
|
l.workerList.Range(func(key, value interface{}) bool {
|
2023-08-27 23:39:58 +08:00
|
|
|
val := value.(timerStr)
|
2024-05-20 09:35:12 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
nextTime, err := GetNextTime(time.Now().In(l.location), *val.JobData)
|
2025-09-14 23:47:40 +08:00
|
|
|
if err != nil {
|
2025-09-18 09:56:24 +08:00
|
|
|
l.logger.Errorf(l.ctx, "Cluster calculateNextTimes GetNextTime err:%s %s", val.TaskId, err.Error())
|
2025-09-14 23:47:40 +08:00
|
|
|
return true
|
|
|
|
|
}
|
2024-05-22 15:02:39 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// l.logger.Infof(l.ctx, "Cluster calculateNextTimes GetNextTime nextTime:%s %s", val.TaskId, nextTime.Format(time.RFC3339))
|
2024-05-22 15:02:39 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// 使用Lua脚本原子性添加任务
|
2024-05-22 15:02:39 +08:00
|
|
|
script := `
|
2025-09-18 09:56:24 +08:00
|
|
|
local zsetKey = KEYS[1]
|
|
|
|
|
local score = ARGV[1]
|
|
|
|
|
local taskID = ARGV[2]
|
|
|
|
|
local expireTime = ARGV[3]
|
2025-10-05 21:43:41 +08:00
|
|
|
local lockKey = ARGV[4]
|
2025-09-18 09:56:24 +08:00
|
|
|
|
|
|
|
|
-- 检查是否已存在
|
|
|
|
|
local existing = redis.call('zscore', zsetKey, taskID)
|
|
|
|
|
if existing and tonumber(existing) <= tonumber(score) then
|
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 设置NX锁避免重复计算
|
|
|
|
|
local lockAcquired = redis.call('set', lockKey, 1, 'NX', 'EX', expireTime)
|
|
|
|
|
if not lockAcquired then
|
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
redis.call('zadd', zsetKey, score, taskID)
|
|
|
|
|
return 1
|
|
|
|
|
`
|
|
|
|
|
|
2025-09-18 21:18:35 +08:00
|
|
|
lockKey := fmt.Sprintf("%s_%s_%d", l.keyPrefix, val.TaskId, nextTime.UnixMilli())
|
2025-10-05 21:43:41 +08:00
|
|
|
_, err = pipe.Eval(l.ctx, script, []string{l.zsetKey},
|
|
|
|
|
nextTime.UnixMilli(), val.TaskId, 60, lockKey).Result()
|
2025-09-18 09:56:24 +08:00
|
|
|
if err != nil {
|
|
|
|
|
l.logger.Errorf(l.ctx, "Failed to schedule task: %v", err)
|
2024-05-22 15:02:39 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
_, err := pipe.Exec(l.ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
l.logger.Errorf(l.ctx, "Cluster Failed to schedule task: %v", err)
|
|
|
|
|
}
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// moveReadyTasks 移动就绪任务到执行列表
|
|
|
|
|
func (c *Cluster) moveReadyTasks() {
|
2023-09-02 13:32:04 +08:00
|
|
|
script := `
|
2025-09-18 09:56:24 +08:00
|
|
|
local zsetKey = KEYS[1]
|
|
|
|
|
local listKey = KEYS[2]
|
|
|
|
|
local maxTime = ARGV[1]
|
|
|
|
|
local limit = ARGV[2]
|
|
|
|
|
|
|
|
|
|
local tasks = redis.call('zrangebyscore', zsetKey, 0, maxTime, 'LIMIT', 0, limit)
|
|
|
|
|
for i, taskID in ipairs(tasks) do
|
|
|
|
|
redis.call('zrem', zsetKey, taskID)
|
|
|
|
|
redis.call('lpush', listKey, taskID)
|
2023-09-02 13:32:04 +08:00
|
|
|
end
|
2025-09-18 09:56:24 +08:00
|
|
|
return #tasks
|
2023-09-02 13:32:04 +08:00
|
|
|
`
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
result, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey, c.listKey},
|
2025-10-05 21:43:41 +08:00
|
|
|
time.Now().UnixMilli(), c.batchSize).Result()
|
2025-09-18 09:56:24 +08:00
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
|
c.logger.Errorf(c.ctx, "Failed to move ready tasks: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-05-27 20:28:11 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
if count, ok := result.(int64); ok && count > 0 {
|
|
|
|
|
c.logger.Infof(c.ctx, "Cluster moveReadyTasks Moved %d tasks to ready list", count)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-27 20:28:11 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
// executeTasks 执行任务
|
|
|
|
|
func (c *Cluster) executeTasks() {
|
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
|
|
for {
|
2025-10-14 11:13:12 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
select {
|
|
|
|
|
case <-c.stopChan:
|
|
|
|
|
return
|
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
|
return
|
2025-10-14 12:07:31 +08:00
|
|
|
case c.workerChan <- struct{}{}:
|
2025-10-20 18:18:22 +08:00
|
|
|
func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
<-c.workerChan
|
|
|
|
|
}()
|
2025-06-11 15:12:08 +08:00
|
|
|
|
2025-10-20 18:18:22 +08:00
|
|
|
if c.usePriority && !c.priority.IsLatest(c.ctx) {
|
2025-10-04 21:33:57 +08:00
|
|
|
time.Sleep(5 * time.Second)
|
2025-10-20 18:18:22 +08:00
|
|
|
return
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2024-05-27 20:28:11 +08:00
|
|
|
|
2025-10-20 18:18:22 +08:00
|
|
|
taskID, err := c.redis.BLPop(c.ctx, 10*time.Second, c.listKey).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err != redis.Nil {
|
|
|
|
|
c.logger.Errorf(c.ctx, "Failed to pop task: %v", err)
|
|
|
|
|
// Redis 异常,休眠一会儿
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(taskID) < 2 {
|
|
|
|
|
c.logger.Errorf(c.ctx, "Invalid BLPop result: %v", taskID)
|
|
|
|
|
// 数据异常,继续下一个
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-05-27 20:28:11 +08:00
|
|
|
|
2025-10-20 18:18:22 +08:00
|
|
|
go c.processTask(taskID[1])
|
|
|
|
|
}()
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
2025-09-18 09:56:24 +08:00
|
|
|
}
|
2024-04-04 10:58:57 +08:00
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 20:28:11 +08:00
|
|
|
type ReJobData struct {
|
|
|
|
|
TaskId string
|
|
|
|
|
Times int
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
// 执行任务
|
2025-09-18 09:56:24 +08:00
|
|
|
func (l *Cluster) processTask(taskId string) {
|
2025-10-14 11:13:12 +08:00
|
|
|
|
2025-09-19 18:43:21 +08:00
|
|
|
begin := time.Now()
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
ctx, cancel := context.WithTimeout(l.ctx, l.timeout)
|
2024-05-24 09:55:34 +08:00
|
|
|
defer cancel()
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-09-19 18:43:21 +08:00
|
|
|
u, _ := uuid.NewV7()
|
|
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, "trace_id", u.String())
|
|
|
|
|
|
|
|
|
|
l.logger.Infof(ctx, "doTask timer begin taskId:%s", taskId)
|
|
|
|
|
|
|
|
|
|
// 上报执行情况
|
2025-10-04 20:48:08 +08:00
|
|
|
executeVal := fmt.Sprintf("tid:%s|insId:%s|uuid:%s|time:%s", taskId, l.instanceId, u.String(), begin.Format(time.RFC3339Nano))
|
2025-10-04 22:00:08 +08:00
|
|
|
l.redis.ZAdd(ctx, l.executeInfoKey, redis.Z{
|
2025-09-22 13:51:28 +08:00
|
|
|
Score: float64(begin.UnixMilli()),
|
2025-09-19 18:43:21 +08:00
|
|
|
Member: executeVal,
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
val, ok := l.workerList.Load(taskId)
|
2023-08-27 23:39:58 +08:00
|
|
|
if !ok {
|
2025-09-18 09:56:24 +08:00
|
|
|
l.logger.Errorf(ctx, "doTask timer:任务不存在:%s", taskId)
|
2023-08-27 23:39:58 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-09-14 23:47:40 +08:00
|
|
|
t, ok := val.(timerStr)
|
2025-09-14 19:05:10 +08:00
|
|
|
if !ok {
|
2025-09-18 09:56:24 +08:00
|
|
|
l.logger.Errorf(ctx, "doTask timer:任务不存在:%s", taskId)
|
2025-09-14 19:05:10 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
// 这里加一个全局锁
|
2025-09-18 09:56:24 +08:00
|
|
|
lock, err := lockx.NewGlobalLock(ctx, l.redis, taskId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
l.logger.Errorf(ctx, "doTask timer:获取锁失败:%s", taskId)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if b, err := lock.Lock(); !b {
|
|
|
|
|
l.logger.Errorf(ctx, "doTask timer:获取锁失败:%s %+v", taskId, err)
|
2023-08-27 23:39:58 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
2024-05-24 09:55:34 +08:00
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
2025-09-19 18:43:21 +08:00
|
|
|
l.logger.Errorf(ctx, "doTask timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
2024-05-24 09:55:34 +08:00
|
|
|
}
|
2025-09-18 09:56:24 +08:00
|
|
|
|
|
|
|
|
l.logger.Infof(ctx, "doTask timer:执行任务耗时:%s %dms", taskId, time.Since(begin).Milliseconds())
|
2024-05-24 09:55:34 +08:00
|
|
|
}()
|
|
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
// 执行任务
|
2025-09-18 09:56:24 +08:00
|
|
|
if err := t.Callback(ctx, t.ExtendData); err != nil {
|
|
|
|
|
l.logger.Errorf(ctx, "doTask timer:执行任务失败:%s %+v", taskId, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 23:39:58 +08:00
|
|
|
}
|