Compare commits
67 Commits
v1.0.11
...
56c479b7b2
| Author | SHA1 | Date | |
|---|---|---|---|
| 56c479b7b2 | |||
| 01f1d60ab1 | |||
| d07f4aae64 | |||
| 092771ebd8 | |||
| f873130a20 | |||
| 058c1b8bec | |||
| 53fe8e4a8a | |||
| feaab7f585 | |||
| c3c6fd05cb | |||
| a9f37bc4e7 | |||
| 459911a579 | |||
| bcd18b698a | |||
| 49f9e8bde6 | |||
| de3568de42 | |||
| 1b9e9b757b | |||
| 2c762dee2a | |||
| bae669a1d9 | |||
| 9caa984846 | |||
| 1f9684080d | |||
| 3ace37f16d | |||
| 063370380a | |||
| 372033cfa3 | |||
| 27717c26be | |||
| 7ab9ac48c2 | |||
| 793a8da1af | |||
| 62e0d03c9e | |||
| 14eb90bf7d | |||
| 81ce4f67d3 | |||
| 737eef2157 | |||
| 2d6e77352f | |||
| 304d27e0ac | |||
| fffe60f975 | |||
| 62bdf4fcd2 | |||
| e14305f66c | |||
| 16a392a266 | |||
| 970a1ca33c | |||
| 84569dc290 | |||
| 85d041753e | |||
| 0c4e92f164 | |||
| 0be8fd0cdc | |||
| 062a39b209 | |||
| c50530d4bb | |||
| e4c453baca | |||
| 4db3cf81b7 | |||
| f79be3955f | |||
| 549eee700e | |||
| db1735de30 | |||
| a0ec69b7a2 | |||
| 44eeb8468d | |||
| 0cff7af265 | |||
| 28359fbf23 | |||
| e62cacf1df | |||
| 464b467868 | |||
| c351cb084f | |||
| 503cabdcbf | |||
| d39a8b14ee | |||
| ddea445188 | |||
| ddc5cd8cb1 | |||
| bb5b01071a | |||
| 2fa0430403 | |||
| 2a7092ab0a | |||
| 037d8cf107 | |||
| ecaac58926 | |||
| 3716f97eaf | |||
| c14d65c46a | |||
| b5e3b6088b | |||
| ca0d5a1b99 |
@@ -0,0 +1,5 @@
|
|||||||
|
log
|
||||||
|
cache
|
||||||
|
test.txt
|
||||||
|
|
||||||
|
|
||||||
+403
-220
@@ -2,17 +2,22 @@ package timerx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/google/uuid"
|
||||||
uuid "github.com/satori/go.uuid"
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/yuninks/cachex"
|
"github.com/yuninks/cachex"
|
||||||
"github.com/yuninks/lockx"
|
"github.com/yuninks/lockx"
|
||||||
|
"github.com/yuninks/timerx/heartbeat"
|
||||||
|
"github.com/yuninks/timerx/leader"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 功能描述
|
// 功能描述
|
||||||
@@ -20,20 +25,12 @@ import (
|
|||||||
// 这是基于Redis的定时任务调度器,能够有效的在服务集群里面调度任务,避免了单点压力过高或单点故障问题
|
// 这是基于Redis的定时任务调度器,能够有效的在服务集群里面调度任务,避免了单点压力过高或单点故障问题
|
||||||
// 由于所有的服务代码是一致的,也就是一个定时任务将在所有的服务都有注册,具体调度到哪个服务运行看调度结果
|
// 由于所有的服务代码是一致的,也就是一个定时任务将在所有的服务都有注册,具体调度到哪个服务运行看调度结果
|
||||||
|
|
||||||
// 暂不支持删除定时器,因为这个定时器的设计是基于全局的,如果删除了,那么其他服务就不知道了
|
|
||||||
|
|
||||||
// 单例模式
|
|
||||||
var clusterOnceLimit sync.Once
|
|
||||||
|
|
||||||
// 已注册的任务列表
|
|
||||||
var clusterWorkerList sync.Map
|
|
||||||
|
|
||||||
type Cluster struct {
|
type Cluster struct {
|
||||||
ctx context.Context
|
ctx context.Context // context
|
||||||
redis redis.UniversalClient
|
cancel context.CancelFunc // 取消函数
|
||||||
cache *cachex.Cache
|
redis redis.UniversalClient // redis
|
||||||
timeout time.Duration
|
timeout time.Duration // job执行超时时间
|
||||||
logger Logger
|
logger logger.Logger // 日志
|
||||||
keyPrefix string // key前缀
|
keyPrefix string // key前缀
|
||||||
location *time.Location // 根据时区计算的时间
|
location *time.Location // 根据时区计算的时间
|
||||||
|
|
||||||
@@ -41,19 +38,43 @@ type Cluster struct {
|
|||||||
zsetKey string // 有序集合的key
|
zsetKey string // 有序集合的key
|
||||||
listKey string // 可执行的任务列表的key
|
listKey string // 可执行的任务列表的key
|
||||||
setKey string // 重入集合的key
|
setKey string // 重入集合的key
|
||||||
}
|
executeInfoKey string // 执行情况的key
|
||||||
|
|
||||||
var clu *Cluster = nil
|
wg sync.WaitGroup // 等待组
|
||||||
|
workerList sync.Map // 注册的任务列表
|
||||||
|
stopChan chan struct{} //
|
||||||
|
instanceId string // 实例ID
|
||||||
|
|
||||||
|
priority *priority.Priority // 全局优先级
|
||||||
|
priorityKey string // 全局优先级的key
|
||||||
|
usePriority bool // 是否使用优先级
|
||||||
|
|
||||||
|
leader *leader.Leader // Leader
|
||||||
|
heartbeat *heartbeat.HeartBeat // 心跳
|
||||||
|
cache *cachex.Cache // 本地缓存
|
||||||
|
cronParser *cron.Parser // cron表达式解析器
|
||||||
|
batchSize int // 批量获取任务的数量
|
||||||
|
workerChan chan struct{} // worker
|
||||||
|
maxWorkers int // 最大worker数量
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化定时器
|
// 初始化定时器
|
||||||
// 全局只需要初始化一次
|
// 全局只需要初始化一次
|
||||||
func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix string, opts ...Option) *Cluster {
|
func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix string, opts ...Option) (*Cluster, error) {
|
||||||
|
|
||||||
|
if red == nil {
|
||||||
|
return nil, errors.New("redis is nil")
|
||||||
|
}
|
||||||
|
|
||||||
clusterOnceLimit.Do(func() {
|
|
||||||
op := newOptions(opts...)
|
op := newOptions(opts...)
|
||||||
|
|
||||||
clu = &Cluster{
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
U, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
clu := &Cluster{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
redis: red,
|
redis: red,
|
||||||
cache: cachex.NewCache(),
|
cache: cachex.NewCache(),
|
||||||
timeout: op.timeout,
|
timeout: op.timeout,
|
||||||
@@ -64,30 +85,175 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
|
|||||||
zsetKey: "timer:cluster_zsetKey" + keyPrefix, // 有序集合
|
zsetKey: "timer:cluster_zsetKey" + keyPrefix, // 有序集合
|
||||||
listKey: "timer:cluster_listKey" + keyPrefix, // 列表
|
listKey: "timer:cluster_listKey" + keyPrefix, // 列表
|
||||||
setKey: "timer:cluster_setKey" + keyPrefix, // 重入集合
|
setKey: "timer:cluster_setKey" + keyPrefix, // 重入集合
|
||||||
|
priorityKey: "timer:cluster_priorityKey" + keyPrefix, // 全局优先级的key
|
||||||
|
executeInfoKey: "timer:cluster_executeInfoKey" + keyPrefix, // 执行情况的key 有序集合
|
||||||
|
usePriority: false,
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
instanceId: U.String(),
|
||||||
|
cronParser: op.cronParser,
|
||||||
|
batchSize: op.batchSize,
|
||||||
|
workerChan: make(chan struct{}, op.maxWorkers),
|
||||||
|
maxWorkers: op.maxWorkers,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置锁的超时时间
|
// 初始化优先级
|
||||||
lockx.InitOption(lockx.SetTimeout(op.timeout))
|
|
||||||
|
|
||||||
// 监听任务
|
if op.priorityType != priorityTypeNone {
|
||||||
go clu.watch()
|
|
||||||
|
|
||||||
timer := time.NewTicker(time.Millisecond * 200)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
pri, err := priority.InitPriority(
|
||||||
|
ctx,
|
||||||
|
red,
|
||||||
|
clu.keyPrefix,
|
||||||
|
op.priorityVal,
|
||||||
|
priority.WithLogger(clu.logger),
|
||||||
|
priority.WithInstanceId(clu.instanceId),
|
||||||
|
priority.WithSource("cluster"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
clu.logger.Errorf(ctx, "InitPriority err:%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
clu.priority = pri
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化leader
|
||||||
|
le, err := leader.InitLeader(
|
||||||
|
ctx,
|
||||||
|
clu.redis,
|
||||||
|
clu.keyPrefix,
|
||||||
|
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),
|
||||||
|
heartbeat.WithSource("cluster"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
clu.logger.Errorf(ctx, "InitHeartBeat err:%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
clu.heartbeat = heart
|
||||||
|
|
||||||
|
// 启动守护进程
|
||||||
|
clu.startDaemon()
|
||||||
|
|
||||||
|
clu.logger.Infof(ctx, "InitCluster success keyPrefix:%s instanceId:%s", clu.keyPrefix, clu.instanceId)
|
||||||
|
|
||||||
|
return clu, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop 停止集群定时器
|
||||||
|
func (l *Cluster) Stop() {
|
||||||
|
close(l.stopChan)
|
||||||
|
if l.usePriority && l.priority != nil {
|
||||||
|
l.priority.Close()
|
||||||
|
}
|
||||||
|
if l.leader != nil {
|
||||||
|
l.leader.Close()
|
||||||
|
}
|
||||||
|
if l.heartbeat != nil {
|
||||||
|
l.heartbeat.Close()
|
||||||
|
}
|
||||||
|
if l.cancel != nil {
|
||||||
|
l.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 守护任务
|
||||||
|
func (l *Cluster) startDaemon() {
|
||||||
|
|
||||||
|
// 任务调度
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.scheduleTasks()
|
||||||
|
|
||||||
|
// 任务执行
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.executeTasks()
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.cleanExecuteInfoLoop()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Cluster) cleanExecuteInfoLoop() {
|
||||||
|
l.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Minute * 5)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
go func(ctx context.Context) {
|
|
||||||
Loop:
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-l.stopChan:
|
||||||
clu.getTask()
|
return
|
||||||
clu.getNextTime()
|
case <-l.ctx.Done():
|
||||||
case <-ctx.Done():
|
return
|
||||||
break Loop
|
case <-ticker.C:
|
||||||
|
if l.leader.IsLeader() {
|
||||||
|
l.cleanExecuteInfo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除过期任务
|
||||||
|
func (l *Cluster) cleanExecuteInfo() error {
|
||||||
|
// 移除执行信息
|
||||||
|
l.redis.ZRemRangeByScore(l.ctx, l.executeInfoKey, "0", strconv.FormatInt(time.Now().Add(-15*time.Minute).UnixMilli(), 10)).Err()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// scheduleTasks 调度任务(只有leader执行)
|
||||||
|
func (c *Cluster) scheduleTasks() {
|
||||||
|
defer c.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(200 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
if !c.leader.IsLeader() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.usePriority && !c.priority.IsLatest(c.ctx) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.calculateNextTimes()
|
||||||
|
c.moveReadyTasks()
|
||||||
|
case <-c.stopChan:
|
||||||
|
return
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(ctx)
|
|
||||||
})
|
|
||||||
return clu
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每月执行一次
|
// 每月执行一次
|
||||||
@@ -101,18 +267,19 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
|
|||||||
// @param extendData 扩展数据
|
// @param extendData 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
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 {
|
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 {
|
||||||
nowTime := time.Now().In(c.location)
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryMonth,
|
JobType: JobTypeEveryMonth,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Day: day,
|
Day: day,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每周执行一次
|
// 每周执行一次
|
||||||
@@ -123,82 +290,120 @@ func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour i
|
|||||||
// @param minute int 分钟
|
// @param minute int 分钟
|
||||||
// @param second int 秒
|
// @param second int 秒
|
||||||
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 {
|
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 {
|
||||||
nowTime := time.Now().In(c.location)
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryWeek,
|
JobType: JobTypeEveryWeek,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Weekday: week,
|
Weekday: week,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每天执行一次
|
// 每天执行一次
|
||||||
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 {
|
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 {
|
||||||
nowTime := time.Now().In(c.location)
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryDay,
|
JobType: JobTypeEveryDay,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每小时执行一次
|
// 每小时执行一次
|
||||||
func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
func (c *Cluster) EveryHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryHour,
|
JobType: JobTypeEveryHour,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每分钟执行一次
|
// 每分钟执行一次
|
||||||
func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
func (c *Cluster) EveryMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryMinute,
|
JobType: JobTypeEveryMinute,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 特定时间间隔
|
// 特定时间间隔
|
||||||
func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
nowTime := time.Now().In(c.location)
|
|
||||||
|
|
||||||
if spaceTime < 0 {
|
if spaceTime < 0 {
|
||||||
c.logger.Errorf(ctx, "间隔时间不能小于0")
|
c.logger.Errorf(ctx, "间隔时间不能小于0")
|
||||||
return errors.New("间隔时间不能小于0")
|
return errors.New("间隔时间不能小于0")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当天的零点时间
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
||||||
zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeInterval,
|
JobType: JobTypeInterval,
|
||||||
|
TaskId: taskId,
|
||||||
BaseTime: zeroTime, // 默认当天的零点
|
BaseTime: zeroTime, // 默认当天的零点
|
||||||
CreateTime: nowTime,
|
|
||||||
IntervalTime: spaceTime,
|
IntervalTime: spaceTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, taskId, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定时任务
|
||||||
|
// 使用的是秒级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 {
|
||||||
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
||||||
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, l.location)
|
||||||
|
|
||||||
|
options := newEmptyOptions(opt...)
|
||||||
|
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,
|
||||||
|
TaskId: taskId,
|
||||||
|
BaseTime: zeroTime, // 默认当天的零点
|
||||||
|
CronExpression: cronExpression,
|
||||||
|
CronSchedule: sche,
|
||||||
|
}
|
||||||
|
|
||||||
|
return l.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 统一添加任务
|
// 统一添加任务
|
||||||
@@ -208,211 +413,163 @@ func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.
|
|||||||
// @param callback callback 回调函数
|
// @param callback callback 回调函数
|
||||||
// @param extendData interface{} 扩展数据
|
// @param extendData interface{} 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
func (c *Cluster) addJob(ctx context.Context, taskId string, jobData JobData, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
func (l *Cluster) addJob(ctx context.Context, jobData JobData, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) error {
|
||||||
_, ok := clusterWorkerList.Load(taskId)
|
// 判断是否重复
|
||||||
|
_, ok := l.workerList.Load(jobData.TaskId)
|
||||||
if ok {
|
if ok {
|
||||||
c.logger.Errorf(ctx, "key已存在:%s", taskId)
|
l.logger.Errorf(ctx, "Cluster addJob taskId exits:%s", jobData.TaskId)
|
||||||
return errors.New("key已存在")
|
return ErrTaskIdExists
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := GetNextTime(time.Now().In(c.location), jobData)
|
// 校验时间是否合法
|
||||||
|
_, err := GetNextTime(time.Now().In(l.location), jobData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf(ctx, "获取下次执行时间失败:%s", err.Error())
|
l.logger.Errorf(ctx, "Cluster addJob GetNextTime err:%s", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ctx, cancel := context.WithCancel(ctx)
|
|
||||||
// defer cancel()
|
|
||||||
|
|
||||||
// lock := lockx.NewGlobalLock(ctx, c.redis, taskId)
|
|
||||||
// tB := lock.Try(2)
|
|
||||||
// if !tB {
|
|
||||||
// c.logger.Errorf(ctx, "添加失败:%s", taskId)
|
|
||||||
// return errors.New("添加失败")
|
|
||||||
// }
|
|
||||||
// defer lock.Unlock()
|
|
||||||
|
|
||||||
t := timerStr{
|
t := timerStr{
|
||||||
Callback: callback,
|
Callback: callback,
|
||||||
ExtendData: extendData,
|
ExtendData: extendData,
|
||||||
TaskId: taskId,
|
TaskId: jobData.TaskId,
|
||||||
JobData: &jobData,
|
JobData: &jobData,
|
||||||
}
|
}
|
||||||
|
|
||||||
clusterWorkerList.Store(taskId, t)
|
l.workerList.Store(jobData.TaskId, t)
|
||||||
|
|
||||||
return err
|
l.logger.Infof(ctx, "Cluster addJob taskId:%s", jobData.TaskId)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算下一次执行的时间
|
// 计算下一次执行的时间
|
||||||
// TODO:注册的任务需放在Redis集中存储,因为本地的话,如果有多个服务,那么就会出现不一致的情况。但是要注意服务如何进行下线,由于是主动上报的,需要有一个机制进行删除过期的任务(添加任务&定时器轮训注册)
|
func (l *Cluster) calculateNextTimes() {
|
||||||
// TODO:考虑不同实例系统时间不一样,可能计算的下次时间不一致,会有重复执行的可能
|
|
||||||
func (c *Cluster) getNextTime() {
|
|
||||||
|
|
||||||
lock := lockx.NewGlobalLock(c.ctx, c.redis, c.lockKey)
|
pipe := l.redis.Pipeline()
|
||||||
// 获取锁
|
|
||||||
lockBool := lock.Lock()
|
|
||||||
if !lockBool {
|
|
||||||
// log.Println("timer:获取锁失败")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer lock.Unlock()
|
|
||||||
|
|
||||||
// 计算下一次时间
|
|
||||||
|
|
||||||
// p := c.redis.Pipeline()
|
|
||||||
|
|
||||||
// 根据内部注册的任务列表计算下一次执行的时间
|
// 根据内部注册的任务列表计算下一次执行的时间
|
||||||
clusterWorkerList.Range(func(key, value interface{}) bool {
|
l.workerList.Range(func(key, value interface{}) bool {
|
||||||
val := value.(timerStr)
|
val := value.(timerStr)
|
||||||
|
|
||||||
nextTime, _ := GetNextTime(time.Now().In(c.location), *val.JobData)
|
nextTime, err := GetNextTime(time.Now().In(l.location), *val.JobData)
|
||||||
|
if err != nil {
|
||||||
// fmt.Println(val.ExtendData, val.JobData, nextTime)
|
l.logger.Errorf(l.ctx, "Cluster calculateNextTimes GetNextTime err:%s %s", val.TaskId, err.Error())
|
||||||
|
|
||||||
// 内部判定是否重复
|
|
||||||
cacheKey := fmt.Sprintf("%s_%s_%d", c.keyPrefix, val.TaskId, nextTime.UnixMilli())
|
|
||||||
cacheVal, err := c.cache.Get(cacheKey)
|
|
||||||
if err == nil {
|
|
||||||
// 缓存已有值
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
valueNum := int(0)
|
|
||||||
if cacheVal != nil {
|
|
||||||
valueNum = cacheVal.(int)
|
|
||||||
}
|
|
||||||
if valueNum > 2 {
|
|
||||||
// 重试2次还是失败就不执行了
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// fmt.Println("计算时间1", val.ExtendData, time.UnixMilli(nextTime.UnixMilli()).Format("2006-01-02 15:04:05"))
|
|
||||||
|
|
||||||
// redis lua脚本,尝试设置nx锁时间为一分钟,如果能设置进去则添加到有序集合zsetKey
|
// l.logger.Infof(l.ctx, "Cluster calculateNextTimes GetNextTime nextTime:%s %s", val.TaskId, nextTime.Format(time.RFC3339))
|
||||||
|
|
||||||
|
// 使用Lua脚本原子性添加任务
|
||||||
script := `
|
script := `
|
||||||
local zsetKey = KEYS[1]
|
local zsetKey = KEYS[1]
|
||||||
|
local score = ARGV[1]
|
||||||
|
local taskID = ARGV[2]
|
||||||
|
local expireTime = ARGV[3]
|
||||||
|
local lockKey = ARGV[4]
|
||||||
|
|
||||||
local cacheKey = ARGV[1]
|
-- 检查是否已存在
|
||||||
local expireTime = ARGV[2]
|
local existing = redis.call('zscore', zsetKey, taskID)
|
||||||
|
if existing and tonumber(existing) <= tonumber(score) then
|
||||||
local score = ARGV[3]
|
return 0
|
||||||
local member = ARGV[4]
|
|
||||||
|
|
||||||
local res = redis.call('set', cacheKey, '', 'nx', 'ex', expireTime)
|
|
||||||
|
|
||||||
if res then
|
|
||||||
redis.call('zadd', zsetKey, score, member)
|
|
||||||
return "SUCCESS"
|
|
||||||
end
|
end
|
||||||
return "ERROR"
|
|
||||||
|
-- 设置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
|
||||||
`
|
`
|
||||||
|
|
||||||
// TODO:
|
lockKey := fmt.Sprintf("%s_%s_%d", l.keyPrefix, val.TaskId, nextTime.UnixMilli())
|
||||||
expireTime := time.Minute
|
_, err = pipe.Eval(l.ctx, script, []string{l.zsetKey},
|
||||||
|
nextTime.UnixMilli(), val.TaskId, 60, lockKey).Result()
|
||||||
res, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey}, cacheKey, expireTime.Seconds(), nextTime.UnixMilli(), val.TaskId).Result()
|
if err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Failed to schedule task: %v", err)
|
||||||
valueNum++
|
|
||||||
|
|
||||||
if err == nil && res.(string) == "SUCCESS" {
|
|
||||||
// 设置成功
|
|
||||||
valueNum = 10
|
|
||||||
|
|
||||||
// fmt.Println("计算时间2", val.ExtendData, time.UnixMilli(nextTime.UnixMilli()).Format("2006-01-02 15:04:05"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.cache.Set(cacheKey, valueNum, expireTime)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
// _, err := p.Exec(ctx)
|
_, err := pipe.Exec(l.ctx)
|
||||||
// _ = err
|
if err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Cluster Failed to schedule task: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取任务
|
// moveReadyTasks 移动就绪任务到执行列表
|
||||||
func (c *Cluster) getTask() {
|
func (c *Cluster) moveReadyTasks() {
|
||||||
// 定时去Redis获取任务
|
|
||||||
script := `
|
script := `
|
||||||
local token = redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])
|
local zsetKey = KEYS[1]
|
||||||
for i,v in ipairs(token) do
|
local listKey = KEYS[2]
|
||||||
redis.call('zrem',KEYS[1],v)
|
local maxTime = ARGV[1]
|
||||||
redis.call('lpush',KEYS[2],v)
|
local limit = ARGV[2]
|
||||||
end
|
|
||||||
return "OK"
|
|
||||||
`
|
|
||||||
c.redis.Eval(c.ctx, script, []string{c.zsetKey, c.listKey}, 0, time.Now().UnixMilli()).Result()
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
return #tasks
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey, c.listKey},
|
||||||
|
time.Now().UnixMilli(), c.batchSize).Result()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
c.logger.Errorf(c.ctx, "Failed to move ready tasks: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听任务
|
if count, ok := result.(int64); ok && count > 0 {
|
||||||
func (c *Cluster) watch() {
|
c.logger.Infof(c.ctx, "Cluster moveReadyTasks Moved %d tasks to ready list", count)
|
||||||
// 执行任务
|
}
|
||||||
go func() {
|
}
|
||||||
|
|
||||||
|
// executeTasks 执行任务
|
||||||
|
func (c *Cluster) executeTasks() {
|
||||||
|
defer c.wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
keys, err := c.redis.BLPop(c.ctx, time.Second*10, c.listKey).Result()
|
|
||||||
|
select {
|
||||||
|
case <-c.stopChan:
|
||||||
|
return
|
||||||
|
case <-c.ctx.Done():
|
||||||
|
return
|
||||||
|
case c.workerChan <- struct{}{}:
|
||||||
|
func() {
|
||||||
|
defer func() {
|
||||||
|
<-c.workerChan
|
||||||
|
}()
|
||||||
|
|
||||||
|
if c.usePriority && !c.priority.IsLatest(c.ctx) {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
taskID, err := c.redis.BLPop(c.ctx, 10*time.Second, c.listKey).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != redis.Nil {
|
if err != redis.Nil {
|
||||||
c.logger.Errorf(c.ctx, "BLPop watch err:%+v", err)
|
c.logger.Errorf(c.ctx, "Failed to pop task: %v", err)
|
||||||
|
// Redis 异常,休眠一会儿
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
}
|
}
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
_, ok := clusterWorkerList.Load(keys[1])
|
|
||||||
if !ok {
|
|
||||||
c.logger.Errorf(c.ctx, "watch timer:任务不存在%+v", keys[1])
|
|
||||||
|
|
||||||
rd := ReJobData{
|
if len(taskID) < 2 {
|
||||||
TaskId: keys[1],
|
c.logger.Errorf(c.ctx, "Invalid BLPop result: %v", taskID)
|
||||||
Times: 1,
|
// 数据异常,继续下一个
|
||||||
|
return
|
||||||
}
|
}
|
||||||
rdb, _ := json.Marshal(rd)
|
|
||||||
|
|
||||||
c.redis.SAdd(c.ctx, c.setKey, string(rdb))
|
go c.processTask(taskID[1])
|
||||||
continue
|
|
||||||
}
|
|
||||||
go c.doTask(c.ctx, keys[1])
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 处理重入任务
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
res, err := c.redis.SPop(c.ctx, c.setKey).Result()
|
|
||||||
if err != nil {
|
|
||||||
if err == redis.Nil {
|
|
||||||
// 已经是空了就不要浪费资源了
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
} else {
|
|
||||||
c.logger.Errorf(c.ctx, "SPop watch err:%+v", err)
|
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var rd ReJobData
|
|
||||||
err = json.Unmarshal([]byte(res), &rd)
|
|
||||||
if err != nil {
|
|
||||||
c.logger.Errorf(c.ctx, "json.Unmarshal err:%+v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
_, ok := clusterWorkerList.Load(rd.TaskId)
|
|
||||||
if !ok {
|
|
||||||
c.logger.Errorf(c.ctx, "watch timer:任务不存在%+v", rd.TaskId)
|
|
||||||
|
|
||||||
if rd.Times >= 3 {
|
|
||||||
// 重试3次还是失败就不执行了
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
rd.Times++
|
|
||||||
|
|
||||||
rdb, _ := json.Marshal(rd)
|
|
||||||
|
|
||||||
c.redis.SAdd(c.ctx, c.setKey, string(rdb))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
go c.doTask(c.ctx, rd.TaskId)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReJobData struct {
|
type ReJobData struct {
|
||||||
@@ -421,35 +578,61 @@ type ReJobData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行任务
|
// 执行任务
|
||||||
func (c *Cluster) doTask(ctx context.Context, taskId string) {
|
func (l *Cluster) processTask(taskId string) {
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
begin := time.Now()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(l.ctx, l.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
val, ok := clusterWorkerList.Load(taskId)
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, "trace_id", u.String())
|
||||||
|
|
||||||
|
l.logger.Infof(ctx, "doTask timer begin taskId:%s", taskId)
|
||||||
|
|
||||||
|
// 上报执行情况
|
||||||
|
executeVal := fmt.Sprintf("tid:%s|insId:%s|uuid:%s|time:%s", taskId, l.instanceId, u.String(), begin.Format(time.RFC3339Nano))
|
||||||
|
l.redis.ZAdd(ctx, l.executeInfoKey, redis.Z{
|
||||||
|
Score: float64(begin.UnixMilli()),
|
||||||
|
Member: executeVal,
|
||||||
|
})
|
||||||
|
|
||||||
|
val, ok := l.workerList.Load(taskId)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.logger.Errorf(ctx, "doTask timer:任务不存在:%s", taskId)
|
l.logger.Errorf(ctx, "doTask timer:任务不存在:%s", taskId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t, ok := val.(timerStr)
|
||||||
|
if !ok {
|
||||||
|
l.logger.Errorf(ctx, "doTask timer:任务不存在:%s", taskId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t := val.(timerStr)
|
|
||||||
|
|
||||||
// 这里加一个全局锁
|
// 这里加一个全局锁
|
||||||
lock := lockx.NewGlobalLock(ctx, c.redis, taskId)
|
lock, err := lockx.NewGlobalLock(ctx, l.redis, taskId)
|
||||||
tB := lock.Lock()
|
if err != nil {
|
||||||
if !tB {
|
l.logger.Errorf(ctx, "doTask timer:获取锁失败:%s", taskId)
|
||||||
c.logger.Errorf(ctx, "doTask timer:获取锁失败:%s", taskId)
|
return
|
||||||
|
}
|
||||||
|
if b, err := lock.Lock(); !b {
|
||||||
|
l.logger.Errorf(ctx, "doTask timer:获取锁失败:%s %+v", taskId, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
c.logger.Errorf(ctx, "timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
l.logger.Errorf(ctx, "doTask timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.logger.Infof(ctx, "doTask timer:执行任务耗时:%s %dms", taskId, time.Since(begin).Milliseconds())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, "trace_id", uuid.NewV4().String)
|
|
||||||
|
|
||||||
// 执行任务
|
// 执行任务
|
||||||
t.Callback(ctx, t.ExtendData)
|
if err := t.Callback(ctx, t.ExtendData); err != nil {
|
||||||
|
l.logger.Errorf(ctx, "doTask timer:执行任务失败:%s %+v", taskId, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-54
@@ -6,18 +6,29 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/redis/go-redis/v9"
|
||||||
"github.com/yuninks/timerx"
|
"github.com/yuninks/timerx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func redisInit() *redis.Client {
|
||||||
|
return redis.NewClient(&redis.Options{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
Password: "123456",
|
||||||
|
DB: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestCluster_AddEveryMonth(t *testing.T) {
|
func TestCluster_AddEveryMonth(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, err := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("InitCluster failed, err: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer cluster.Stop()
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
hour := 2
|
hour := 2
|
||||||
@@ -30,22 +41,22 @@ func TestCluster_AddEveryMonth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
extendData := "testData"
|
extendData := "testData"
|
||||||
|
|
||||||
err := cluster.EveryMonth(ctx, taskId, 1, hour, minute, second, callback, extendData)
|
err = cluster.EveryMonth(ctx, taskId, 1, hour, minute, second, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("AddEveryMonth failed, err: %v", err)
|
t.Errorf("AddEveryMonth failed, err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
|
||||||
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCluster_AddEveryWeek(t *testing.T) {
|
func TestCluster_AddEveryWeek(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
week := time.Sunday
|
week := time.Sunday
|
||||||
@@ -69,12 +80,10 @@ func TestCluster_AddEveryWeek(t *testing.T) {
|
|||||||
|
|
||||||
func TestCluster_AddEveryDay(t *testing.T) {
|
func TestCluster_AddEveryDay(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
hour := 2
|
hour := 2
|
||||||
@@ -97,12 +106,10 @@ func TestCluster_AddEveryDay(t *testing.T) {
|
|||||||
|
|
||||||
func TestCluster_AddEveryHour(t *testing.T) {
|
func TestCluster_AddEveryHour(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
minute := 3
|
minute := 3
|
||||||
@@ -124,12 +131,10 @@ func TestCluster_AddEveryHour(t *testing.T) {
|
|||||||
|
|
||||||
func TestCluster_AddEveryMinute(t *testing.T) {
|
func TestCluster_AddEveryMinute(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
second := 4
|
second := 4
|
||||||
@@ -152,14 +157,12 @@ func TestCluster_Add(t *testing.T) {
|
|||||||
fmt.Println("66666")
|
fmt.Println("66666")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
fmt.Println("66666")
|
fmt.Println("66666")
|
||||||
redis := redis.NewClient(&redis.Options{
|
redis := redisInit()
|
||||||
Addr: "localhost:6379",
|
|
||||||
})
|
|
||||||
defer redis.Close()
|
defer redis.Close()
|
||||||
|
|
||||||
t.Log("6666")
|
t.Log("6666")
|
||||||
|
|
||||||
cluster := timerx.InitCluster(ctx, redis, "test")
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
||||||
|
|
||||||
taskId := "testTask"
|
taskId := "testTask"
|
||||||
dur := time.Second
|
dur := time.Second
|
||||||
@@ -172,37 +175,10 @@ func TestCluster_Add(t *testing.T) {
|
|||||||
|
|
||||||
err := cluster.EverySpace(ctx, taskId, dur, callback, extendData)
|
err := cluster.EverySpace(ctx, taskId, dur, callback, extendData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Add failed, err: %v", err)
|
t.Errorf("Add failed,1 err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Second * 20)
|
time.Sleep(time.Second * 20)
|
||||||
|
|
||||||
|
|
||||||
// TODO: verify the job is added to the cluster and can be executed after the specified duration
|
// TODO: verify the job is added to the cluster and can be executed after the specified duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
client := redis.NewClient(&redis.Options{
|
|
||||||
Addr: "127.0.0.1" + ":" + "6379",
|
|
||||||
Password: "", // no password set
|
|
||||||
DB: 0, // use default DB
|
|
||||||
})
|
|
||||||
if client == nil {
|
|
||||||
fmt.Println("redis init error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Redis = client
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRedis(t *testing.T) {
|
|
||||||
fmt.Println("6666")
|
|
||||||
t.Log("fffff")
|
|
||||||
// t.Fail()
|
|
||||||
// t.Error("ffff")
|
|
||||||
// Redis.Set(context.Background(), "dddd", "dddd", 0)
|
|
||||||
// str, err := Redis.Get(context.Background(), "dddd").Result()
|
|
||||||
// fmt.Println("ssss", str, err)
|
|
||||||
// t.Log(str, err)
|
|
||||||
// t.Fail()
|
|
||||||
}
|
|
||||||
|
|||||||
+154
-31
@@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/yuninks/timerx"
|
"github.com/yuninks/timerx"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -26,27 +28,85 @@ func main() {
|
|||||||
// re()
|
// re()
|
||||||
// d()
|
// d()
|
||||||
// cluster()
|
// cluster()
|
||||||
once()
|
// once()
|
||||||
|
single()
|
||||||
|
// prioritys()
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func single() error {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ops := []timerx.Option{
|
||||||
|
timerx.WithCronParserSecond(),
|
||||||
|
}
|
||||||
|
|
||||||
|
single := timerx.InitSingle(ctx, ops...)
|
||||||
|
|
||||||
|
single.Cron(ctx, "test_cron1", "*/5 * * * * ?", callback, "这是cron任务1", timerx.WithCronParserSecond())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func prioritys() {
|
||||||
|
|
||||||
|
client := getRedis()
|
||||||
|
ctx := context.Background()
|
||||||
|
pro, _ := priority.InitPriority(ctx, client, "test", 10)
|
||||||
|
|
||||||
|
for {
|
||||||
|
b := pro.IsLatest(ctx)
|
||||||
|
fmt.Println("isLatest", b)
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func once() {
|
func once() {
|
||||||
client := getRedis()
|
client := getRedis()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
w := OnceWorker{}
|
w := OnceWorker{}
|
||||||
one := timerx.InitOnce(ctx, client, "test", w)
|
|
||||||
|
ver, err := priority.PriorityByVersion("v2.2.3.4.5")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ops := []timerx.Option{
|
||||||
|
timerx.WithPriority(ver),
|
||||||
|
}
|
||||||
|
|
||||||
|
one, err := timerx.InitOnce(ctx, client, "test_once", w, ops...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
d := OnceData{
|
d := OnceData{
|
||||||
Num: 1,
|
Num: 3,
|
||||||
}
|
}
|
||||||
dy, _ := json.Marshal(d)
|
// dy, _ := json.Marshal(d)
|
||||||
|
|
||||||
err := one.Create("test", "test", 1*time.Second, dy)
|
err = one.Create("test", "test3", 1*time.Second, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
// d = OnceData{
|
||||||
|
// Num: 4,
|
||||||
|
// }
|
||||||
|
// dd := 123
|
||||||
|
// dy, _ = json.Marshal(d)
|
||||||
|
// err = one.Save("test", "test4", 2*time.Second, dd)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// err = one.Save("test", "test5", 5*time.Second, dd)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err)
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,40 +116,88 @@ type OnceData struct {
|
|||||||
|
|
||||||
type OnceWorker struct{}
|
type OnceWorker struct{}
|
||||||
|
|
||||||
func (l OnceWorker) Worker(ctx context.Context, taskType string, taskId string, attachData []byte) *timerx.OnceWorkerResp {
|
func (l OnceWorker) Worker(ctx context.Context, taskType timerx.OnceTaskType, taskId string, attachData interface{}) *timerx.OnceWorkerResp {
|
||||||
|
// 追加写入文件
|
||||||
|
file, err := os.OpenFile("./test3.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
file.WriteString(fmt.Sprintf("执行时间:%s\n", time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
|
||||||
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
|
fmt.Println("执行时间:", time.Now().Format("2006-01-02 15:04:05"))
|
||||||
fmt.Println(taskId, taskType)
|
// fmt.Println(taskType, taskId)
|
||||||
fmt.Printf("原来的参数:%s\n", string(attachData))
|
|
||||||
|
|
||||||
d := OnceData{}
|
// fmt.Printf("原来的参数:%+v %T\n", attachData, attachData)
|
||||||
|
|
||||||
json.Unmarshal(attachData, &d)
|
// v, ok := attachData.(int64)
|
||||||
|
// fmt.Println("vvvvvvv", v, ok)
|
||||||
|
// fmt.Printf()
|
||||||
|
|
||||||
d.Num++
|
// d := OnceData{}
|
||||||
|
|
||||||
fmt.Println(d)
|
// json.Unmarshal(ab, &d)
|
||||||
|
|
||||||
dy, _ := json.Marshal(d)
|
// d.Num++
|
||||||
|
|
||||||
|
// fmt.Println(d)
|
||||||
|
|
||||||
|
// dy, _ := json.Marshal(d)
|
||||||
|
|
||||||
return &timerx.OnceWorkerResp{
|
return &timerx.OnceWorkerResp{
|
||||||
Retry: true,
|
Retry: true,
|
||||||
AttachData: dy,
|
AttachData: attachData,
|
||||||
DelayTime: 1 * time.Second,
|
DelayTime: time.Second,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cluster() {
|
func cluster() {
|
||||||
client := getRedis()
|
client := getRedis()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cluster := timerx.InitCluster(ctx, client, "test")
|
|
||||||
err := cluster.EverySpace(ctx, "test_space", 1*time.Second, aa, "这是秒任务")
|
// log := loggerx.NewLogger(ctx,loggerx.SetToConsole(),loggerx.SetEscapeHTML(false))
|
||||||
|
// _ = log
|
||||||
|
|
||||||
|
cluster, _ := timerx.InitCluster(ctx, client, "test2", timerx.WithPriority(104))
|
||||||
|
err := cluster.EverySpace(ctx, "test_space1", 1*time.Second, callback, "这是秒任务1")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
err = cluster.EveryMinute(ctx, "test_min", 15, aa, "这是分钟任务")
|
err = cluster.EverySpace(ctx, "test_space2", 2*time.Second, callback, "这是秒任务2")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
err = cluster.EveryHour(ctx, "test_hour", 30, 0, aa, "这是小时任务")
|
err = cluster.EverySpace(ctx, "test_space3", 5*time.Second, callback, "这是秒任务3")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
err = cluster.EveryDay(ctx, "test_day", 11, 0, 0, aa, "这是天任务")
|
|
||||||
|
err = cluster.EveryMinute(ctx, "test_min1", 15, callback, "这是分钟任务1")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
err = cluster.EveryMinute(ctx, "test_min2", 30, callback, "这是分钟任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
err = cluster.EveryHour(ctx, "test_hour1", 30, 0, callback, "这是小时任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = cluster.EveryHour(ctx, "test_hour2", 30, 15, callback, "这是小时任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
err = cluster.EveryDay(ctx, "test_day1", 5, 0, 0, callback, "这是天任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = cluster.EveryDay(ctx, "test_day2", 9, 20, 0, callback, "这是天任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = cluster.EveryDay(ctx, "test_day3", 10, 30, 30, callback, "这是天任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 默认秒级表达式
|
||||||
|
err = cluster.Cron(ctx, "test_cron1", "*/5 * * * * ?", callback, "这是cron任务1", timerx.WithCronParserSecond())
|
||||||
|
fmt.Println(err)
|
||||||
|
err = cluster.Cron(ctx, "test_cron2", "0/5 * * * * ?", callback, "这是cron任务2", timerx.WithCronParserSecond())
|
||||||
|
fmt.Println("这是cron任务2:", err)
|
||||||
|
// 自定义解析器
|
||||||
|
err = cluster.Cron(ctx, "test_cron3", "@every 2s", callback, "这是cron任务3", timerx.WithCronParserOption(cron.Descriptor))
|
||||||
|
fmt.Println("这是cron任务3:", err)
|
||||||
|
// Linux标准解析器
|
||||||
|
err = cluster.Cron(ctx, "test_cron4", "*/5 * * * *", callback, "这是cron任务4", timerx.WithCronParserLinux())
|
||||||
|
fmt.Println("这是cron任务4:", err)
|
||||||
|
// 仅符号解析器
|
||||||
|
err = cluster.Cron(ctx, "test_cron5", "@every 5s", callback, "这是cron任务5", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println("这是cron任务5:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func worker() {
|
func worker() {
|
||||||
@@ -131,21 +239,36 @@ func re() {
|
|||||||
client := getRedis()
|
client := getRedis()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cl := timerx.InitCluster(ctx, client, "kkkk")
|
cl, _ := timerx.InitCluster(ctx, client, "kkkk")
|
||||||
cl.EverySpace(ctx, "test1", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test1", 1*time.Millisecond, callback, "data")
|
||||||
cl.EverySpace(ctx, "test2", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test2", 1*time.Millisecond, callback, "data")
|
||||||
cl.EverySpace(ctx, "test3", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test3", 1*time.Millisecond, callback, "data")
|
||||||
cl.EverySpace(ctx, "test4", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test4", 1*time.Millisecond, callback, "data")
|
||||||
cl.EverySpace(ctx, "test5", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test5", 1*time.Millisecond, callback, "data")
|
||||||
cl.EverySpace(ctx, "test6", 1*time.Millisecond, aa, "data")
|
cl.EverySpace(ctx, "test6", 1*time.Millisecond, callback, "data")
|
||||||
|
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func aa(ctx context.Context, data interface{}) error {
|
func callback(ctx context.Context, data interface{}) error {
|
||||||
|
|
||||||
fmt.Println("-执行时间:", data, time.Now().Format("2006-01-02 15:04:05"))
|
fmt.Println("-执行时间:", data, time.Now().Format("2006-01-02 15:04:05"))
|
||||||
// fmt.Println(data)
|
// fmt.Println(data)
|
||||||
// time.Sleep(time.Second * 5)
|
// time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
|
// 追加到文件
|
||||||
|
file, err := os.OpenFile("./test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("打开文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, err = file.WriteString(fmt.Sprintf("-执行时间:%v %s\n", data, time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("写入文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +284,7 @@ func d() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.ZAdd(context.Background(), "lockx:test2", &redis.Z{
|
client.ZAdd(context.Background(), "lockx:test2", redis.Z{
|
||||||
Score: 50,
|
Score: 50,
|
||||||
Member: "test",
|
Member: "test",
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package timerx
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// 定时器不存在
|
||||||
|
ErrTimerNotFound = errors.New("timer not found")
|
||||||
|
// 任务ID不能为空
|
||||||
|
ErrTaskIdEmpty = errors.New("taskId can not be empty")
|
||||||
|
// 每月的天数必须在0-31之间
|
||||||
|
ErrMonthDay = errors.New("month day must be between 0 and 31")
|
||||||
|
// 小时必须在0-23之间
|
||||||
|
ErrHour = errors.New("hour must be between 0 and 23")
|
||||||
|
// 分钟必须在0-59之间
|
||||||
|
ErrMinute = errors.New("minute must be between 0 and 59")
|
||||||
|
// 秒必须在0-59之间
|
||||||
|
ErrSecond = errors.New("second must be between 0 and 59")
|
||||||
|
// 回调函数不能为空
|
||||||
|
ErrCallbackEmpty = errors.New("callback can not be empty")
|
||||||
|
// 星期必须在0-6之间
|
||||||
|
ErrWeekday = errors.New("weekday must be between Sunday and Saturday")
|
||||||
|
// 创建时间不能为空
|
||||||
|
ErrCreateTime = errors.New("create time can not be empty")
|
||||||
|
// 基准时间不能为空
|
||||||
|
ErrBaseTime = errors.New("base time can not be empty")
|
||||||
|
// 间隔时间必须大于0
|
||||||
|
ErrIntervalTime = errors.New("interval time must be greater than 0")
|
||||||
|
// 任务Id已存在
|
||||||
|
ErrTaskIdExists = errors.New("taskId already exists")
|
||||||
|
// 任务已执行
|
||||||
|
ErrTaskExecuted = errors.New("task already executed")
|
||||||
|
// cron表达式错误
|
||||||
|
ErrCronExpression = errors.New("cron expression error")
|
||||||
|
// ErrCronParser 错误
|
||||||
|
ErrCronParser = errors.New("cron parser error")
|
||||||
|
// ExecuteTime 错误
|
||||||
|
ErrExecuteTime = errors.New("execute time error")
|
||||||
|
// RunCount 错误
|
||||||
|
ErrRunCount = errors.New("run count error")
|
||||||
|
// DelayTime 错误
|
||||||
|
ErrDelayTime = errors.New("delay time error")
|
||||||
|
// 任务已存在
|
||||||
|
ErrTaskExists = errors.New("task already exists")
|
||||||
|
)
|
||||||
@@ -0,0 +1,299 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/timerx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var save_path = "./cluster.log"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
client := getRedis()
|
||||||
|
|
||||||
|
ops := []timerx.Option{}
|
||||||
|
|
||||||
|
clu, err := timerx.InitCluster(ctx, client, "cluster_01", ops...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
space(ctx, clu)
|
||||||
|
minute(ctx, clu)
|
||||||
|
hour(ctx, clu)
|
||||||
|
day(ctx, clu)
|
||||||
|
week(ctx, clu)
|
||||||
|
month(ctx, clu)
|
||||||
|
cron(ctx, clu)
|
||||||
|
|
||||||
|
select {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// space
|
||||||
|
func space(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每秒执行一次
|
||||||
|
err := clu.EverySpace(ctx, "space_test_second_1", 1*time.Second, callback, "space 这是秒任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_second_5", 5*time.Second, callback, "space 这是5秒任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每分钟执行一次
|
||||||
|
err = clu.EverySpace(ctx, "space_test_minute_1", 1*time.Minute, callback, "space 这是分钟任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_minute_5", 5*time.Minute, callback, "space 这是5分钟任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每小时执行一次
|
||||||
|
err = clu.EverySpace(ctx, "space_test_hour_1", 1*time.Hour, callback, "space 这是小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_hour_2", 2*time.Hour, callback, "space 这是2小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_hour_3", 3*time.Hour, callback, "space 这是3小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_hour_4", 4*time.Hour, callback, "space 这是4小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_hour_5", 5*time.Hour, callback, "space 这是5小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每天执行一次
|
||||||
|
err = clu.EverySpace(ctx, "space_test_day_1", 24*time.Hour, callback, "space 这是天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_day_2", 2*24*time.Hour, callback, "space 这是2天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
err = clu.EverySpace(ctx, "space_test_day_3", 3*24*time.Hour, callback, "space 这是3天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每周执行一次
|
||||||
|
err = clu.EverySpace(ctx, "space_test_week_1", 7*24*time.Hour, callback, "space 这是7天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每月执行一次
|
||||||
|
err = clu.EverySpace(ctx, "space_test_month_1", 30*24*time.Hour, callback, "space 这是30天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// minute
|
||||||
|
func minute(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每分钟0s执行一次
|
||||||
|
err := clu.EveryMinute(ctx, "minute_test_min1", 0, callback, "minute 这是分钟任务0")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟5s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min5", 5, callback, "minute 这是分钟任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟10s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min10", 10, callback, "minute 这是分钟任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟15s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min15", 15, callback, "minute 这是分钟任务15")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟30s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min30", 30, callback, "minute 这是分钟任务30")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟45s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min45", 45, callback, "minute 这是分钟任务45")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟50s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min50", 50, callback, "minute 这是分钟任务50")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟55s执行一次
|
||||||
|
err = clu.EveryMinute(ctx, "minute_test_min55", 55, callback, "minute 这是分钟任务55")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hour
|
||||||
|
func hour(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每小时的第0分钟15s执行一次
|
||||||
|
err := clu.EveryHour(ctx, "hour_test_hour1", 0, 15, callback, "hour 这是小时任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第5分钟30s执行一次
|
||||||
|
err = clu.EveryHour(ctx, "hour_test_hour2", 5, 30, callback, "hour 这是小时任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第10分钟45s执行一次
|
||||||
|
err = clu.EveryHour(ctx, "hour_test_hour3", 10, 45, callback, "hour 这是小时任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第15分钟0s执行一次
|
||||||
|
err = clu.EveryHour(ctx, "hour_test_hour4", 15, 0, callback, "hour 这是小时任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第20分钟15s执行一次
|
||||||
|
err = clu.EveryHour(ctx, "hour_test_hour5", 20, 15, callback, "hour 这是小时任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Day
|
||||||
|
func day(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每天的00:00:00执行一次
|
||||||
|
err := clu.EveryDay(ctx, "day_test_day1", 0, 0, 0, callback, "day 这是天任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的02:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day2", 2, 0, 0, callback, "day 这是天任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的04:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day3", 4, 0, 0, callback, "day 这是天任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的06:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day4", 6, 0, 0, callback, "day 这是天任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的08:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day5", 8, 0, 0, callback, "day 这是天任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的10:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day6", 10, 0, 0, callback, "day 这是天任务6")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的12:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day7", 12, 0, 0, callback, "day 这是天任务7")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的14:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day8", 14, 0, 0, callback, "day 这是天任务8")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的16:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day9", 16, 0, 0, callback, "day 这是天任务9")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的18:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day10", 18, 0, 0, callback, "day 这是天任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的20:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day11", 20, 0, 0, callback, "day 这是天任务11")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的22:00:00执行一次
|
||||||
|
err = clu.EveryDay(ctx, "day_test_day12", 22, 0, 0, callback, "day 这是天任务12")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Week
|
||||||
|
func week(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每周一 10:00:00 执行
|
||||||
|
err := clu.EveryWeek(ctx, "week_test_week1", 1, 10, 0, 0, callback, "week 这是周任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周二 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week2", 2, 10, 0, 0, callback, "week 这是周任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周三 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week3", 3, 10, 0, 0, callback, "week 这是周任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周四 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week4", 4, 10, 0, 0, callback, "week 这是周任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周五 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week5", 5, 10, 0, 0, callback, "week 这是周任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周六 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week6", 6, 10, 0, 0, callback, "week 这是周任务6")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周日 10:00:00 执行
|
||||||
|
err = clu.EveryWeek(ctx, "week_test_week7", 0, 10, 0, 0, callback, "week 这是周任务7")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Month
|
||||||
|
func month(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 每月的第1号 10:00:00 执行
|
||||||
|
err := clu.EveryMonth(ctx, "month_test_month1", 1, 10, 0, 0, callback, "month 这是月任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第5号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month5", 5, 10, 0, 0, callback, "month 这是月任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第10号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month10", 10, 10, 0, 0, callback, "month 这是月任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第15号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month15", 15, 10, 0, 0, callback, "month 这是月任务15")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第20号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month20", 20, 10, 0, 0, callback, "month 这是月任务20")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第25号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month25", 25, 10, 0, 0, callback, "month 这是月任务25")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第28号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month28", 28, 10, 0, 0, callback, "month 这是月任务28")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第29号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month29", 29, 10, 0, 0, callback, "month 这是月任务29")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第30号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month30", 30, 10, 0, 0, callback, "month 这是月任务30")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第31号 10:00:00 执行
|
||||||
|
err = clu.EveryMonth(ctx, "month_test_month31", 31, 10, 0, 0, callback, "month 这是月任务31")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func cron(ctx context.Context, clu *timerx.Cluster) {
|
||||||
|
// 秒级表达式 5秒执行一次
|
||||||
|
err := clu.Cron(ctx, "cron_test_cron1", "*/5 * * * * ?", callback, "cron 这是cron任务1", timerx.WithCronParserSecond())
|
||||||
|
fmt.Println(err)
|
||||||
|
// Linux表达式 5分钟执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron2", "*/5 * * * *", callback, "cron 这是cron任务2", timerx.WithCronParserLinux())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 5秒执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron3", "@every 5s", callback, "cron 这是cron任务3", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每天执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron4", "@daily", callback, "cron 这是cron任务4", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每月执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron5", "@monthly", callback, "cron 这是cron任务5", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每年执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron6", "@yearly", callback, "cron 这是cron任务6", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每周执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron7", "@weekly", callback, "cron 这是cron任务7", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每小时执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron8", "@hourly", callback, "cron 这是cron任务8", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每分钟执行一次
|
||||||
|
err = clu.Cron(ctx, "cron_test_cron9", "@minutely", callback, "cron 这是cron任务9", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRedis() *redis.Client {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1" + ":" + "6379",
|
||||||
|
Password: "123456", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
})
|
||||||
|
if client == nil {
|
||||||
|
panic("redis init error")
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func callback(ctx context.Context, extendData any) error {
|
||||||
|
|
||||||
|
fmt.Println("任务执行了", extendData, "时间:", time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
// 解析文件路径,每天一个文件
|
||||||
|
path, _ := filepath.Abs("./")
|
||||||
|
// 拼接文件路径
|
||||||
|
save_path = filepath.Join(path,"/cache/cluster/"+time.Now().Format("2006-01-02")+".log")
|
||||||
|
// 创建文件夹
|
||||||
|
dir := filepath.Dir(save_path)
|
||||||
|
os.MkdirAll(dir, 0755)
|
||||||
|
|
||||||
|
// 追加到文件
|
||||||
|
file, err := os.OpenFile(save_path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("打开文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, err = file.WriteString(fmt.Sprintf("执行时间:%v %s\n", extendData, time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("写入文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,257 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/timerx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var save_path = "./once.log"
|
||||||
|
|
||||||
|
const (
|
||||||
|
OnceTaskTypeNormal timerx.OnceTaskType = "normal"
|
||||||
|
OnceTaskTypeUrgent timerx.OnceTaskType = "urgent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
client := getRedis()
|
||||||
|
once, err := timerx.InitOnce(ctx, client, "once_01", &OnceWorker{}, timerx.WithBatchSize(1000))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// intervalSaveTime(ctx, once)
|
||||||
|
// intervalSave(ctx, once)
|
||||||
|
// intervalcreateTask(ctx, once)
|
||||||
|
// intervalCreateTime(ctx, once)
|
||||||
|
// benchmarkJob(ctx, once)
|
||||||
|
stabilityTest(ctx, once)
|
||||||
|
|
||||||
|
select {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 稳定性测试
|
||||||
|
func stabilityTest(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
timer := time.NewTicker(time.Second)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case t := <-timer.C:
|
||||||
|
fmt.Println("time:", t)
|
||||||
|
|
||||||
|
str := t.Format("2006-01-02 15:04:05")
|
||||||
|
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_0_%d", time.Now().Unix()), 0, "Create 间隔0s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_1_%d", time.Now().Unix()), time.Second*1, "Create 间隔1s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_10_%d", time.Now().Unix()), time.Second*10, "Create 间隔10s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_15_%d", time.Now().Unix()), time.Second*15, "Create 间隔15s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_30_%d", time.Now().Unix()), time.Second*30, "Create 间隔30s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_60_%d", time.Now().Unix()), time.Second*60, "Create 间隔60s ["+str+"]")
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_120_%d", time.Now().Unix()), time.Second*120, "Create 间隔120s ["+str+"]") // 2分钟
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_300_%d", time.Now().Unix()), time.Second*300, "Create 间隔300s ["+str+"]") // 5分钟
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_900_%d", time.Now().Unix()), time.Second*900, "Create 间隔900s ["+str+"]") // 15分钟
|
||||||
|
once.Create(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_1800_%d", time.Now().Unix()), time.Second*1800, "Create 间隔1800s ["+str+"]") // 30分钟
|
||||||
|
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_0_%d", time.Now().Unix()), 0, "Save 间隔0s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_1_%d", time.Now().Unix()), time.Second*1, "Save 间隔1s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_10_%d", time.Now().Unix()), time.Second*10, "Save 间隔10s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_15_%d", time.Now().Unix()), time.Second*15, "Save 间隔15s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_30_%d", time.Now().Unix()), time.Second*30, "Save 间隔30s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_60_%d", time.Now().Unix()), time.Second*60, "Save 间隔60s ["+str+"]")
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_120_%d", time.Now().Unix()), time.Second*120, "Save 间隔120s ["+str+"]") // 2分钟
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_300_%d", time.Now().Unix()), time.Second*300, "Save 间隔300s ["+str+"]") // 5分钟
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_900_%d", time.Now().Unix()), time.Second*900, "Save 间隔900s ["+str+"]") // 15分钟
|
||||||
|
once.Save(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_1800_%d", time.Now().Unix()), time.Second*1800, "Save 间隔1800s ["+str+"]") // 30分钟
|
||||||
|
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_0_%d", time.Now().Unix()), time.Now(), "CreateByTime 当前时间 ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_1_%d", time.Now().Unix()), time.Now().Add(time.Second*1), "CreateByTime 当前时间+1s ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_10_%d", time.Now().Unix()), time.Now().Add(time.Second*10), "CreateByTime 当前时间+10s ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_15_%d", time.Now().Unix()), time.Now().Add(time.Second*15), "CreateByTime 当前时间+15s ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_30_%d", time.Now().Unix()), time.Now().Add(time.Second*30), "CreateByTime 当前时间+30s ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_60_%d", time.Now().Unix()), time.Now().Add(time.Second*60), "CreateByTime 当前时间+60s ["+str+"]")
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_120_%d", time.Now().Unix()), time.Now().Add(time.Second*120), "CreateByTime 当前时间+120s ["+str+"]") // 2分钟
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_300_%d", time.Now().Unix()), time.Now().Add(time.Second*300), "CreateByTime 当前时间+300s ["+str+"]") // 5分钟
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_900_%d", time.Now().Unix()), time.Now().Add(time.Second*900), "CreateByTime 当前时间+900s ["+str+"]") // 15分钟
|
||||||
|
once.CreateByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_create_by_1800_%d", time.Now().Unix()), time.Now().Add(time.Second*1800), "CreateByTime 当前时间+1800s ["+str+"]") // 30分钟
|
||||||
|
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_0_%d", time.Now().Unix()), time.Now(), "SaveByTime 当前时间 ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_1_%d", time.Now().Unix()), time.Now().Add(time.Second*1), "SaveByTime 当前时间+1s ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_10_%d", time.Now().Unix()), time.Now().Add(time.Second*10), "SaveByTime 当前时间+10s ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_15_%d", time.Now().Unix()), time.Now().Add(time.Second*15), "SaveByTime 当前时间+15s ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_30_%d", time.Now().Unix()), time.Now().Add(time.Second*30), "SaveByTime 当前时间+30s ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_60_%d", time.Now().Unix()), time.Now().Add(time.Second*60), "SaveByTime 当前时间+60s ["+str+"]")
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_120_%d", time.Now().Unix()), time.Now().Add(time.Second*120), "SaveByTime 当前时间+120s ["+str+"]") // 2分钟
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_300_%d", time.Now().Unix()), time.Now().Add(time.Second*300), "SaveByTime 当前时间+300s ["+str+"]") // 5分钟
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_900_%d", time.Now().Unix()), time.Now().Add(time.Second*900), "SaveByTime 当前时间+900s ["+str+"]") // 15分钟
|
||||||
|
once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("stabilityTest_task_save_by_1800_%d", time.Now().Unix()), time.Now().Add(time.Second*1800), "SaveByTime 当前时间+1800s ["+str+"]") // 30分钟
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 指定时间测试
|
||||||
|
func intervalSaveTime(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
beginTime := time.Now()
|
||||||
|
|
||||||
|
for i := 1; i < 100; i++ {
|
||||||
|
|
||||||
|
execTime := beginTime.Add(time.Second * time.Duration(i))
|
||||||
|
|
||||||
|
err := once.SaveByTime(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), execTime, fmt.Sprintf("任务数据_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 间隔测试
|
||||||
|
func intervalSave(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
beginTime := time.Now()
|
||||||
|
|
||||||
|
for i := 1; i < 100; i++ {
|
||||||
|
|
||||||
|
execTime := beginTime.Add(time.Second * time.Duration(i))
|
||||||
|
|
||||||
|
err := once.Save(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), time.Until(execTime), fmt.Sprintf("任务数据_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建间隔测试
|
||||||
|
func intervalcreateTask(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
beginTime := time.Now()
|
||||||
|
|
||||||
|
for i := 1; i < 100; i++ {
|
||||||
|
|
||||||
|
execTime := beginTime.Add(time.Second * time.Duration(i))
|
||||||
|
|
||||||
|
err := once.Create(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), time.Until(execTime), fmt.Sprintf("任务数据A_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
err = once.Create(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), time.Until(execTime), fmt.Sprintf("任务数据B_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func intervalCreateTime(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
beginTime := time.Now()
|
||||||
|
|
||||||
|
for i := 1; i < 100; i++ {
|
||||||
|
|
||||||
|
execTime := beginTime.Add(time.Second * time.Duration(i))
|
||||||
|
|
||||||
|
err := once.CreateByTime(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), execTime, fmt.Sprintf("任务数据A_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
err = once.CreateByTime(ctx, timerx.OnceTaskType("intervalSaveTime"), fmt.Sprintf("intervalSaveTime_task_%d", i), execTime, fmt.Sprintf("任务数据B_%d 预期时间%s", i, execTime.Format("2006-01-02 15:04:05")))
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 压力测试
|
||||||
|
func benchmarkJob(ctx context.Context, once *timerx.Once) {
|
||||||
|
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
ch := make(chan ChanStatus, 1000)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for a := 0; a < 100; a++ {
|
||||||
|
go func(a int) {
|
||||||
|
for status := range ch {
|
||||||
|
// fmt.Println("协程", a, "处理任务", status)
|
||||||
|
// time.Sleep(10 * time.Millisecond) // 模拟处理时间
|
||||||
|
err := once.SaveByTime(ctx, OnceTaskTypeNormal, fmt.Sprintf("task_%d_%d", status.I, status.J), status.T, fmt.Sprintf("任务数据_%d_%d 预期时间%s", status.I, status.J, status.T.Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("保存任务失败:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(a)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
// 一千万任务,每个任务间隔1秒
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
runTime := t.Add(time.Duration(i) * time.Second)
|
||||||
|
for j := 0; j < 100; j++ {
|
||||||
|
ch <- ChanStatus{
|
||||||
|
I: i,
|
||||||
|
J: j,
|
||||||
|
T: runTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChanStatus struct {
|
||||||
|
I int
|
||||||
|
J int
|
||||||
|
T time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRedis() *redis.Client {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1" + ":" + "6379",
|
||||||
|
Password: "123456", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
})
|
||||||
|
if client == nil {
|
||||||
|
panic("redis init error")
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
type OnceWorker struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *OnceWorker) Worker(ctx context.Context, taskType timerx.OnceTaskType, taskId string, attachData interface{}) *timerx.OnceWorkerResp {
|
||||||
|
// 任务处理逻辑
|
||||||
|
callback(ctx, attachData)
|
||||||
|
|
||||||
|
return &timerx.OnceWorkerResp{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func callback(ctx context.Context, extendData any) error {
|
||||||
|
|
||||||
|
fmt.Println("任务执行了", extendData, "时间:", time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
|
// 解析文件路径,每天一个文件
|
||||||
|
path, _ := filepath.Abs("./")
|
||||||
|
// 拼接文件路径
|
||||||
|
save_path = filepath.Join(path, "/cache/once/"+time.Now().Format("2006-01-02")+".log")
|
||||||
|
// 创建文件夹
|
||||||
|
dir := filepath.Dir(save_path)
|
||||||
|
os.MkdirAll(dir, 0755)
|
||||||
|
|
||||||
|
// 追加到文件
|
||||||
|
file, err := os.OpenFile(save_path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("打开文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, err = file.WriteString(fmt.Sprintf("执行时间:%v [%s] \n", extendData, time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("写入文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,297 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/timerx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var save_path string = "./single.log"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ops := []timerx.Option{}
|
||||||
|
|
||||||
|
clu := timerx.InitSingle(ctx, ops...)
|
||||||
|
|
||||||
|
space(ctx, clu)
|
||||||
|
minute(ctx, clu)
|
||||||
|
hour(ctx, clu)
|
||||||
|
day(ctx, clu)
|
||||||
|
week(ctx, clu)
|
||||||
|
month(ctx, clu)
|
||||||
|
cron(ctx, clu)
|
||||||
|
|
||||||
|
select {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// space
|
||||||
|
func space(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每秒执行一次
|
||||||
|
_, err := clu.EverySpace(ctx, "space_test_second_1", 1*time.Second, callback, "space 这是秒任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_second_5", 5*time.Second, callback, "space 这是5秒任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每分钟执行一次
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_minute_1", 1*time.Minute, callback, "space 这是分钟任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_minute_5", 5*time.Minute, callback, "space 这是5分钟任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每小时执行一次
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_hour_1", 1*time.Hour, callback, "space 这是小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_hour_2", 2*time.Hour, callback, "space 这是2小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_hour_3", 3*time.Hour, callback, "space 这是3小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_hour_4", 4*time.Hour, callback, "space 这是4小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_hour_5", 5*time.Hour, callback, "space 这是5小时任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每天执行一次
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_day_1", 24*time.Hour, callback, "space 这是天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_day_2", 2*24*time.Hour, callback, "space 这是2天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_day_3", 3*24*time.Hour, callback, "space 这是3天任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
// 每周执行一次
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_week_1", 7*24*time.Hour, callback, "space 这是周任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
|
||||||
|
// 每月执行一次
|
||||||
|
_, err = clu.EverySpace(ctx, "space_test_month_1", 30*24*time.Hour, callback, "space 这是月任务")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// minute
|
||||||
|
func minute(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每分钟0s执行一次
|
||||||
|
_, err := clu.EveryMinute(ctx, "minute_test_min1", 0, callback, "minute 这是分钟任务0")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟5s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min5", 5, callback, "minute 这是分钟任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟10s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min10", 10, callback, "minute 这是分钟任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟15s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min15", 15, callback, "minute 这是分钟任务15")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟30s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min30", 30, callback, "minute 这是分钟任务30")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟45s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min45", 45, callback, "minute 这是分钟任务45")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟50s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min50", 50, callback, "minute 这是分钟任务50")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每分钟55s执行一次
|
||||||
|
_, err = clu.EveryMinute(ctx, "minute_test_min55", 55, callback, "minute 这是分钟任务55")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hour
|
||||||
|
func hour(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每小时的第0分钟15s执行一次
|
||||||
|
_, err := clu.EveryHour(ctx, "hour_test_hour1", 0, 15, callback, "hour 这是小时任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第5分钟30s执行一次
|
||||||
|
_, err = clu.EveryHour(ctx, "hour_test_hour2", 5, 30, callback, "hour 这是小时任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第10分钟45s执行一次
|
||||||
|
_, err = clu.EveryHour(ctx, "hour_test_hour3", 10, 45, callback, "hour 这是小时任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第15分钟0s执行一次
|
||||||
|
_, err = clu.EveryHour(ctx, "hour_test_hour4", 15, 0, callback, "hour 这是小时任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每小时的第20分钟15s执行一次
|
||||||
|
_, err = clu.EveryHour(ctx, "hour_test_hour5", 20, 15, callback, "hour 这是小时任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Day
|
||||||
|
func day(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每天的00:00:00执行一次
|
||||||
|
_, err := clu.EveryDay(ctx, "day_test_day1", 0, 0, 0, callback, "day 这是天任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的02:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day2", 2, 0, 0, callback, "day 这是天任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的04:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day3", 4, 0, 0, callback, "day 这是天任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的06:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day4", 6, 0, 0, callback, "day 这是天任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的08:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day5", 8, 0, 0, callback, "day 这是天任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的10:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day6", 10, 0, 0, callback, "day 这是天任务6")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的12:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day7", 12, 0, 0, callback, "day 这是天任务7")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的14:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day8", 14, 0, 0, callback, "day 这是天任务8")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的16:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day9", 16, 0, 0, callback, "day 这是天任务9")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的18:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day10", 18, 0, 0, callback, "day 这是天任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的20:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day11", 20, 0, 0, callback, "day 这是天任务11")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每天的22:00:00执行一次
|
||||||
|
_, err = clu.EveryDay(ctx, "day_test_day12", 22, 0, 0, callback, "day 这是天任务12")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Week
|
||||||
|
func week(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每周一 10:00:00 执行
|
||||||
|
_, err := clu.EveryWeek(ctx, "week_test_week1", 1, 10, 0, 0, callback, "week 这是周任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周二 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week2", 2, 10, 0, 0, callback, "week 这是周任务2")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周三 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week3", 3, 10, 0, 0, callback, "week 这是周任务3")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周四 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week4", 4, 10, 0, 0, callback, "week 这是周任务4")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周五 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week5", 5, 10, 0, 0, callback, "week 这是周任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周六 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week6", 6, 10, 0, 0, callback, "week 这是周任务6")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每周日 10:00:00 执行
|
||||||
|
_, err = clu.EveryWeek(ctx, "week_test_week7", 0, 10, 0, 0, callback, "week 这是周任务7")
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Month
|
||||||
|
func month(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 每月的第1号 10:00:00 执行
|
||||||
|
_, err := clu.EveryMonth(ctx, "month_test_month1", 1, 10, 0, 0, callback, "month 这是月任务1")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第5号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month5", 5, 10, 0, 0, callback, "month 这是月任务5")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第10号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month10", 10, 10, 0, 0, callback, "month 这是月任务10")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第15号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month15", 15, 10, 0, 0, callback, "month 这是月任务15")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第20号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month20", 20, 10, 0, 0, callback, "month 这是月任务20")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第25号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month25", 25, 10, 0, 0, callback, "month 这是月任务25")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第28号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month28", 28, 10, 0, 0, callback, "month 这是月任务28")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第29号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month29", 29, 10, 0, 0, callback, "month 这是月任务29")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第30号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month30", 30, 10, 0, 0, callback, "month 这是月任务30")
|
||||||
|
fmt.Println(err)
|
||||||
|
// 每月的第31号 10:00:00 执行
|
||||||
|
_, err = clu.EveryMonth(ctx, "month_test_month31", 31, 10, 0, 0, callback, "month 这是月任务31")
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func cron(ctx context.Context, clu *timerx.Single) {
|
||||||
|
// 秒级表达式 5秒执行一次
|
||||||
|
_, err := clu.Cron(ctx, "cron_test_cron1", "*/5 * * * * ?", callback, "cron 这是cron任务1", timerx.WithCronParserSecond())
|
||||||
|
fmt.Println(err)
|
||||||
|
// Linux表达式 5分钟执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron2", "*/5 * * * *", callback, "cron 这是cron任务2", timerx.WithCronParserLinux())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 5秒执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron3", "@every 5s", callback, "cron 这是cron任务3", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每天执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron4", "@daily", callback, "cron 这是cron任务4", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每月执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron5", "@monthly", callback, "cron 这是cron任务5", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每年执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron6", "@yearly", callback, "cron 这是cron任务6", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每周执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron7", "@weekly", callback, "cron 这是cron任务7", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每小时执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron8", "@hourly", callback, "cron 这是cron任务8", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
// 符号表达式 每分钟执行一次
|
||||||
|
_, err = clu.Cron(ctx, "cron_test_cron9", "@minutely", callback, "cron 这是cron任务9", timerx.WithCronParserDescriptor())
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRedis() *redis.Client {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1" + ":" + "6379",
|
||||||
|
Password: "123456", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
})
|
||||||
|
if client == nil {
|
||||||
|
panic("redis init error")
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func callback(ctx context.Context, extendData any) error {
|
||||||
|
|
||||||
|
fmt.Println("任务执行了", extendData, "时间:", time.Now().Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
|
// 解析文件路径,每天一个文件
|
||||||
|
path, _ := filepath.Abs("./")
|
||||||
|
// 拼接文件路径
|
||||||
|
save_path = filepath.Join(path, "/cache/single/"+time.Now().Format("2006-01-02")+".log")
|
||||||
|
// 创建文件夹
|
||||||
|
dir := filepath.Dir(save_path)
|
||||||
|
os.MkdirAll(dir, 0755)
|
||||||
|
|
||||||
|
// 追加到文件
|
||||||
|
file, err := os.OpenFile(save_path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("打开文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, err = file.WriteString(fmt.Sprintf("执行时间:%v %s\n", extendData, time.Now().Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("写入文件失败:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
module github.com/yuninks/timerx
|
module github.com/yuninks/timerx
|
||||||
|
|
||||||
go 1.19
|
go 1.24
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
github.com/google/uuid v1.6.0
|
||||||
github.com/satori/go.uuid v1.2.0
|
github.com/redis/go-redis/v9 v9.14.0
|
||||||
github.com/yuninks/cachex v1.0.5
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/yuninks/lockx v1.0.2
|
github.com/stretchr/testify v1.11.1
|
||||||
|
github.com/yuninks/cachex v1.0.6
|
||||||
|
github.com/yuninks/lockx v1.1.4
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,28 +1,36 @@
|
|||||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
github.com/redis/go-redis/v9 v9.14.0 h1:u4tNCjXOyzfgeLN+vAZaW1xUooqWDqVEsZN0U01jfAE=
|
||||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
github.com/redis/go-redis/v9 v9.14.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
|
||||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/yuninks/cachex v1.0.5 h1:Y2NmTsuEgwEVYb7FVFh5tUN67kmrUioeksQqLbOAwsM=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
github.com/yuninks/cachex v1.0.5/go.mod h1:5357qz18UvHTJSgZzkMamUzZoFzGeKG9+4tIUBXRSVM=
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
github.com/yuninks/lockx v1.0.2 h1:p0n791WmsU8D7YF2tQaNLwPE75jdd774unlJZRTNfaw=
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/yuninks/lockx v1.0.2/go.mod h1:J6wvuUELLcMn6FCmiZFt7K5w1QQAh1myL7h3JrZaQiQ=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
github.com/yuninks/cachex v1.0.6 h1:G5dJ8O0gLAGnLwydEHVCSSGv+p1z8KfZdjb5NdxxsVA=
|
||||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
github.com/yuninks/cachex v1.0.6/go.mod h1:5357qz18UvHTJSgZzkMamUzZoFzGeKG9+4tIUBXRSVM=
|
||||||
|
github.com/yuninks/lockx v1.1.4 h1:Wrd4aAU5apNWAamZM7yqoNlwHDjGY/X1YYrLXcgj1+s=
|
||||||
|
github.com/yuninks/lockx v1.1.4/go.mod h1:+HyRozwQHMHrykyOFlotV4Z+z2yrgRSdDl8TxxRMFzw=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -0,0 +1,155 @@
|
|||||||
|
package heartbeat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/timerx/leader"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 心跳
|
||||||
|
// 作用:上报实例最新存活状态,清理过期实例
|
||||||
|
// 依赖:leader priority
|
||||||
|
|
||||||
|
type HeartBeat struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
redis redis.UniversalClient // redis
|
||||||
|
logger logger.Logger
|
||||||
|
|
||||||
|
priority *priority.Priority // (允许nil)全局优先级
|
||||||
|
leader *leader.Leader // (允许nil)领导
|
||||||
|
|
||||||
|
heartbeatKey string // 心跳Key 有序集合
|
||||||
|
instanceId string // 实例ID
|
||||||
|
|
||||||
|
wg sync.WaitGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitHeartBeat(ctx context.Context, ref redis.UniversalClient, keyPrefix string, opts ...Option) (*HeartBeat, error) {
|
||||||
|
|
||||||
|
if ref == nil {
|
||||||
|
return nil, errors.New("redis is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
op := newOptions(opts...)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
l := &HeartBeat{
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
|
||||||
|
heartbeatKey: "timer:heartbeat_key" + op.source + keyPrefix,
|
||||||
|
|
||||||
|
priority: op.priority,
|
||||||
|
redis: ref,
|
||||||
|
logger: op.logger,
|
||||||
|
leader: op.leader,
|
||||||
|
instanceId: op.instanceId,
|
||||||
|
}
|
||||||
|
|
||||||
|
l.logger.Infof(l.ctx, "InitHeartBeat InstanceId %s lockKey:%s", l.instanceId, l.heartbeatKey)
|
||||||
|
|
||||||
|
l.startDaemon()
|
||||||
|
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *HeartBeat) Close() {
|
||||||
|
l.cleanHeartbeat(true)
|
||||||
|
l.cancel()
|
||||||
|
l.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *HeartBeat) startDaemon() {
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.heartbeatLoop()
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.cleanHeartbeatLoop()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 心跳上报
|
||||||
|
// 需要确定当前存活的实例&当前实例是否是领导
|
||||||
|
func (l *HeartBeat) heartbeatLoop() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
// 先执行一次
|
||||||
|
l.heartbeat()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
l.heartbeat()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单次心跳
|
||||||
|
func (l *HeartBeat) heartbeat() error {
|
||||||
|
err := l.redis.ZAdd(l.ctx, l.heartbeatKey, redis.Z{
|
||||||
|
Score: float64(time.Now().UnixMilli()),
|
||||||
|
Member: l.instanceId,
|
||||||
|
}).Err()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "heartbeat redis.ZAdd err:%v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 心跳清理(leader可操作)
|
||||||
|
func (l *HeartBeat) cleanHeartbeatLoop() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
if l.leader != nil {
|
||||||
|
if !l.leader.IsLeader() {
|
||||||
|
// l.logger.Infof(l.ctx, "cleanHeartbeatLoop not leader")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.cleanHeartbeat(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单次清理
|
||||||
|
func (l *HeartBeat) cleanHeartbeat(cleanSelf bool) error {
|
||||||
|
|
||||||
|
// 仅移除自己
|
||||||
|
if cleanSelf {
|
||||||
|
return l.redis.ZRem(l.ctx, l.heartbeatKey, l.instanceId).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除心跳
|
||||||
|
l.redis.ZRemRangeByScore(l.ctx, l.heartbeatKey, "0", strconv.FormatInt(time.Now().Add(-15*time.Second).UnixMilli(), 10)).Err()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package heartbeat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/yuninks/timerx/leader"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
logger logger.Logger // 日志
|
||||||
|
instanceId string // 实例ID
|
||||||
|
priority *priority.Priority // 全局优先级
|
||||||
|
leader *leader.Leader // Leader
|
||||||
|
source string // 来源服务
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() Options {
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
return Options{
|
||||||
|
logger: logger.NewLogger(),
|
||||||
|
instanceId: u.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func newOptions(opts ...Option) Options {
|
||||||
|
o := defaultOptions()
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&o)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithLogger(log logger.Logger) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.logger = log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithPriority(p *priority.Priority) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.priority = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithLeader(l *leader.Leader) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.leader = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithInstanceId(instanceId string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.instanceId = instanceId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSource(source string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.source = source
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package leader
|
||||||
|
|
||||||
|
// 竞选Leader
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/lockx"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 领导
|
||||||
|
// 作用:领导选举,是领导才执行,避免资源浪费
|
||||||
|
// 依赖:priority
|
||||||
|
|
||||||
|
type Leader struct {
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
isLeader bool // 是否是领导
|
||||||
|
leaderLock sync.RWMutex // 领导锁
|
||||||
|
leaderUniLockKey string // 领导唯一锁
|
||||||
|
leaderKey string // 上报当前的Leader
|
||||||
|
|
||||||
|
redis redis.UniversalClient // redis
|
||||||
|
logger logger.Logger
|
||||||
|
|
||||||
|
priority *priority.Priority // 全局优先级
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
|
instanceId string // 实例ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// leader负责选举
|
||||||
|
|
||||||
|
func InitLeader(ctx context.Context, ref redis.UniversalClient, keyPrefix string, opts ...Option) (*Leader, error) {
|
||||||
|
|
||||||
|
if ref == nil {
|
||||||
|
return nil, errors.New("redis is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
op := newOptions(opts...)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
l := &Leader{
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
redis: ref,
|
||||||
|
leaderUniLockKey: "timer:leader_lockKey" + op.source + keyPrefix,
|
||||||
|
leaderKey: "timer:leader" + op.source + keyPrefix,
|
||||||
|
priority: op.priority,
|
||||||
|
instanceId: op.instanceId,
|
||||||
|
logger: op.logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.leaderElection()
|
||||||
|
|
||||||
|
l.logger.Infof(l.ctx, "InitLeader InstanceId %s lockKey:%s leaderKey:%s", l.instanceId, l.leaderUniLockKey, l.leaderKey)
|
||||||
|
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Leader) Close() {
|
||||||
|
l.cancel()
|
||||||
|
l.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 领导选举
|
||||||
|
// 领导作用:全局推选一个人计算执行时间&移入队列,避免每个都进行计算浪费资源
|
||||||
|
func (l *Leader) leaderElection() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
// 先执行一次
|
||||||
|
l.getLeaderLock()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
l.getLeaderLock()
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 成为领导
|
||||||
|
func (l *Leader) getLeaderLock() error {
|
||||||
|
|
||||||
|
// 非当前优先级不用抢leader
|
||||||
|
if l.priority != nil && !l.priority.IsLatest(l.ctx) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(l.ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// 尝试加锁
|
||||||
|
lock, err := lockx.NewGlobalLock(ctx, l.redis, l.leaderUniLockKey)
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "getLeaderLock err:%+v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if b, _ := lock.Lock(); !b {
|
||||||
|
// 加锁失败 非Reader
|
||||||
|
l.leaderLock.Lock()
|
||||||
|
l.isLeader = false
|
||||||
|
l.leaderLock.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
// 加锁成功
|
||||||
|
l.leaderLock.Lock()
|
||||||
|
l.isLeader = true
|
||||||
|
l.leaderLock.Unlock()
|
||||||
|
|
||||||
|
// 上报当前的Leader实例
|
||||||
|
l.redis.Set(l.ctx, l.leaderKey, l.instanceId, time.Hour*24)
|
||||||
|
|
||||||
|
l.logger.Infof(l.ctx, "getLeaderLock Instance %s became leader", lock.GetValue())
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if l.priority == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
if !l.priority.IsLatest(l.ctx) {
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 等待超时退出
|
||||||
|
<-lock.GetCtx().Done()
|
||||||
|
|
||||||
|
// 已过期
|
||||||
|
// l.leaderLock.Lock()
|
||||||
|
// l.isLeader = false
|
||||||
|
// l.leaderLock.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// isCurrentLeader 检查当前实例是否是leader
|
||||||
|
func (c *Leader) IsLeader() bool {
|
||||||
|
c.leaderLock.RLock()
|
||||||
|
defer c.leaderLock.RUnlock()
|
||||||
|
return c.isLeader
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package leader
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
logger logger.Logger
|
||||||
|
instanceId string
|
||||||
|
priority *priority.Priority // 全局优先级
|
||||||
|
source string // 来源服务
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() Options {
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
return Options{
|
||||||
|
logger: logger.NewLogger(),
|
||||||
|
instanceId: u.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func newOptions(opts ...Option) Options {
|
||||||
|
o := defaultOptions()
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&o)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithLogger(log logger.Logger) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.logger = log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithPriority(p *priority.Priority) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.priority = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithInstanceId(instanceId string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.instanceId = instanceId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSource(source string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.source = source
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package timerx
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -6,8 +6,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Infof(ctx context.Context, format string, v ...interface{})
|
Infof(ctx context.Context, format string, v ...any)
|
||||||
Errorf(ctx context.Context, format string, v ...interface{})
|
// Warnf(ctx context.Context, format string, v ...any)
|
||||||
|
Errorf(ctx context.Context, format string, v ...any)
|
||||||
}
|
}
|
||||||
|
|
||||||
type defaultLogger struct{}
|
type defaultLogger struct{}
|
||||||
@@ -16,10 +17,14 @@ func NewLogger() *defaultLogger {
|
|||||||
return &defaultLogger{}
|
return &defaultLogger{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *defaultLogger) Infof(ctx context.Context, format string, v ...interface{}) {
|
func (l *defaultLogger) Infof(ctx context.Context, format string, v ...any) {
|
||||||
log.Printf("[INFO] "+format, v...)
|
log.Printf("[INFO] "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *defaultLogger) Errorf(ctx context.Context, format string, v ...interface{}) {
|
// func (l *defaultLogger) Warnf(ctx context.Context, format string, v ...any) {
|
||||||
|
// log.Printf("[WARN] "+format, v...)
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (l *defaultLogger) Errorf(ctx context.Context, format string, v ...any) {
|
||||||
log.Printf("[ERROR] "+format, v...)
|
log.Printf("[ERROR] "+format, v...)
|
||||||
}
|
}
|
||||||
+242
-64
@@ -3,114 +3,292 @@ package timerx
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 计算该任务下次执行时间
|
// 计算该任务下次执行时间
|
||||||
// @param job *JobData 任务数据
|
// @param job *JobData 任务数据
|
||||||
|
// @param t time.Time 当前时间
|
||||||
// @return time.Time 下次执行时间
|
// @return time.Time 下次执行时间
|
||||||
|
// @return error 错误信息
|
||||||
func GetNextTime(t time.Time, job JobData) (*time.Time, error) {
|
func GetNextTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
|
||||||
var next time.Time
|
if err := validateJobData(job); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var next *time.Time
|
||||||
|
var err error
|
||||||
|
|
||||||
switch job.JobType {
|
switch job.JobType {
|
||||||
case JobTypeEveryMonth:
|
case JobTypeEveryMonth:
|
||||||
next = calculateNextMonthTime(t, job)
|
next, err = calculateNextMonthTime(t, job)
|
||||||
case JobTypeEveryWeek:
|
case JobTypeEveryWeek:
|
||||||
next = calculateNextWeekTime(t, job)
|
next, err = calculateNextWeekTime(t, job)
|
||||||
case JobTypeEveryDay:
|
case JobTypeEveryDay:
|
||||||
next = calculateNextDayTime(t, job)
|
next, err = calculateNextDayTime(t, job)
|
||||||
case JobTypeEveryHour:
|
case JobTypeEveryHour:
|
||||||
next = calculateNextHourTime(t, job)
|
next, err = calculateNextHourTime(t, job)
|
||||||
case JobTypeEveryMinute:
|
case JobTypeEveryMinute:
|
||||||
next = calculateNextMinuteTime(t, job)
|
next, err = calculateNextMinuteTime(t, job)
|
||||||
case JobTypeInterval:
|
case JobTypeInterval:
|
||||||
next = calculateNextInterval(t, job)
|
next, err = calculateNextInterval(t, job)
|
||||||
|
case JobTypeCron:
|
||||||
|
next, err = calculateNextCronTime(t, job)
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("未知的任务类型: " + string(job.JobType))
|
return nil, errors.New("未知的任务类型: " + string(job.JobType))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return next, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数校验
|
||||||
|
func validateJobData(job JobData) error {
|
||||||
|
switch job.JobType {
|
||||||
|
case JobTypeEveryMonth:
|
||||||
|
if job.Day < 1 || job.Day > 31 {
|
||||||
|
return ErrMonthDay
|
||||||
|
}
|
||||||
|
case JobTypeEveryWeek:
|
||||||
|
if job.Weekday < time.Sunday || job.Weekday > time.Saturday {
|
||||||
|
return ErrWeekday
|
||||||
|
}
|
||||||
|
case JobTypeEveryDay:
|
||||||
|
if job.Hour < 0 || job.Hour > 23 {
|
||||||
|
return ErrHour
|
||||||
|
}
|
||||||
|
case JobTypeEveryHour:
|
||||||
|
if job.Minute < 0 || job.Minute > 59 {
|
||||||
|
return ErrMinute
|
||||||
|
}
|
||||||
|
case JobTypeEveryMinute:
|
||||||
|
if job.Second < 0 || job.Second > 59 {
|
||||||
|
return ErrSecond
|
||||||
|
}
|
||||||
|
case JobTypeInterval:
|
||||||
|
if job.IntervalTime <= 0 {
|
||||||
|
return ErrIntervalTime
|
||||||
|
}
|
||||||
|
if job.BaseTime.IsZero() {
|
||||||
|
return ErrBaseTime
|
||||||
|
}
|
||||||
|
case JobTypeCron:
|
||||||
|
if job.CronExpression == "" {
|
||||||
|
return ErrCronExpression
|
||||||
|
}
|
||||||
|
if job.CronSchedule == nil {
|
||||||
|
return ErrCronParser
|
||||||
|
}
|
||||||
|
_, err := calculateNextCronTime(time.Now(), job)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if job.Hour < 0 || job.Hour > 23 {
|
||||||
|
return ErrHour
|
||||||
|
}
|
||||||
|
if job.Minute < 0 || job.Minute > 59 {
|
||||||
|
return ErrMinute
|
||||||
|
}
|
||||||
|
if job.Second < 0 || job.Second > 59 {
|
||||||
|
return ErrSecond
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算间隔任务下一次执行时间
|
||||||
|
// 固定基准时间,因为在不同的实例中需要对齐基准点
|
||||||
|
func calculateNextInterval(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
if job.BaseTime.IsZero() {
|
||||||
|
return nil, ErrBaseTime
|
||||||
|
}
|
||||||
|
if job.IntervalTime <= 0 {
|
||||||
|
return nil, ErrIntervalTime
|
||||||
|
}
|
||||||
|
// 计算从基准时间到当前时间经过了多少个间隔
|
||||||
|
elapsed := t.Sub(job.BaseTime)
|
||||||
|
intervals := elapsed / job.IntervalTime
|
||||||
|
|
||||||
|
// 计算下一个执行时间
|
||||||
|
next := job.BaseTime.Add((intervals + 1) * job.IntervalTime)
|
||||||
|
|
||||||
|
// 需要整的
|
||||||
|
next = next.Round(job.IntervalTime)
|
||||||
|
|
||||||
|
// 确保下次执行时间不早于当前时间
|
||||||
|
if next.Before(t) || next.Equal(t) {
|
||||||
|
next = next.Add(job.IntervalTime)
|
||||||
|
}
|
||||||
|
|
||||||
return &next, nil
|
return &next, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextInterval(t time.Time, job JobData) time.Time {
|
func calculateNextMonthTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
// 从创建的时候开始计算
|
// 尝试当月是否有这个天数
|
||||||
cycle := t.Sub(job.BaseTime).Microseconds() / job.IntervalTime.Microseconds()
|
// time.Date(2025, 2, 30, 0, 0, 0, 0, t.Location()) => 2025-03-02 00:00:00 +0800 CST 当月不足往后补
|
||||||
return job.BaseTime.Add(job.IntervalTime * time.Duration(cycle+1))
|
currentMonthTime := time.Date(t.Year(), t.Month(), job.Day, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
|
||||||
|
// 如果日期无效(比如2月30号),则调整到该月最后一天
|
||||||
|
if currentMonthTime.Day() != job.Day {
|
||||||
|
// 获取该月的最后一天(0日就是上个月最后一天)
|
||||||
|
// time.Date(2025,2,0,0,0,0,0,time.Local) => 2025-01-31 00:00:00 +0800 CST
|
||||||
|
lastDay := time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()).Day()
|
||||||
|
if job.Day > lastDay {
|
||||||
|
currentMonthTime = time.Date(t.Year(), t.Month(), lastDay, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextMonthTime(t time.Time, job JobData) time.Time {
|
if currentMonthTime.After(t) {
|
||||||
// 判断是否可执行并返回下一个执行时间
|
return ¤tMonthTime, nil
|
||||||
|
|
||||||
if canRun(t, job) {
|
|
||||||
return time.Date(t.Year(), t.Month(), job.Day, job.Hour, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
|
||||||
// 下一个周期(下个月)
|
|
||||||
return time.Date(t.Year(), t.Month()+1, job.Day, job.Hour, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextWeekTime(t time.Time, job JobData) time.Time {
|
// 计算下个月的同一天
|
||||||
weekday := t.Weekday()
|
nextMonth := t.Month() + 1
|
||||||
days := int(job.Weekday - weekday)
|
year := t.Year()
|
||||||
if days < 0 {
|
if nextMonth > 12 {
|
||||||
days += 7
|
nextMonth = 1
|
||||||
}
|
year++
|
||||||
// 判断是否可执行并返回下一个执行时间
|
|
||||||
if canRun(t, job) {
|
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), job.Hour, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
|
||||||
// 下一个周期(下周)
|
|
||||||
return time.Date(t.Year(), t.Month(), t.Day()+days+7, job.Hour, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextDayTime(t time.Time, job JobData) time.Time {
|
nextMonthTime := time.Date(year, nextMonth, job.Day, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
// 判断是否可执行并返回下一个执行时间
|
// 如果日期无效,调整到下个月的最后一天
|
||||||
if canRun(t, job) {
|
if nextMonthTime.Day() != job.Day {
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), job.Hour, job.Minute, job.Second, 0, t.Location())
|
lastDay := time.Date(year, nextMonth+1, 0, 0, 0, 0, 0, t.Location()).Day()
|
||||||
|
if job.Day > lastDay {
|
||||||
|
nextMonthTime = time.Date(year, nextMonth, lastDay, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
}
|
}
|
||||||
// 下一个周期(明天)
|
|
||||||
return time.Date(t.Year(), t.Month(), t.Day()+1, job.Hour, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextHourTime(t time.Time, job JobData) time.Time {
|
return &nextMonthTime, nil
|
||||||
// 判断是否可执行并返回下一个执行时间
|
|
||||||
if canRun(t, job) {
|
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
|
||||||
// 下一个周期(下个小时)
|
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, job.Minute, job.Second, 0, t.Location())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateNextMinuteTime(t time.Time, job JobData) time.Time {
|
func calculateNextWeekTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
// 判断是否可执行并返回下一个执行时间
|
currentWeekday := t.Weekday()
|
||||||
if canRun(t, job) {
|
targetWeekday := job.Weekday
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), job.Second, 0, t.Location())
|
|
||||||
|
// 计算距离目标星期几的天数
|
||||||
|
daysToAdd := int(targetWeekday - currentWeekday)
|
||||||
|
if daysToAdd < 0 {
|
||||||
|
daysToAdd += 7
|
||||||
}
|
}
|
||||||
// 下一个周期(下分钟)
|
// 本周的目标时间
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute()+1, job.Second, 0, t.Location())
|
thisWeekTime := time.Date(t.Year(), t.Month(), t.Day()+daysToAdd, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
|
||||||
|
if thisWeekTime.After(t) {
|
||||||
|
return &thisWeekTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下周的目标时间
|
||||||
|
nextWeekTime := time.Date(t.Year(), t.Month(), t.Day()+daysToAdd+7, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
return &nextWeekTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateNextDayTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
// 今天的目标时间
|
||||||
|
todayTime := time.Date(t.Year(), t.Month(), t.Day(), job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
|
||||||
|
if todayTime.After(t) {
|
||||||
|
return &todayTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 明天的时间
|
||||||
|
nextDayTime := time.Date(t.Year(), t.Month(), t.Day()+1, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
return &nextDayTime, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateNextHourTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
// 计算当前小时的目标时间
|
||||||
|
currentHourTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), job.Minute, job.Second, 0, t.Location())
|
||||||
|
|
||||||
|
if currentHourTime.After(t) {
|
||||||
|
return ¤tHourTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下一个小时的时间
|
||||||
|
nextHourTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, job.Minute, job.Second, 0, t.Location())
|
||||||
|
return &nextHourTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateNextMinuteTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
// 计算当前分钟的目标时间
|
||||||
|
|
||||||
|
currentMinuteTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), job.Second, 0, t.Location())
|
||||||
|
|
||||||
|
if currentMinuteTime.After(t) {
|
||||||
|
return ¤tMinuteTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下一分钟的时间
|
||||||
|
nextMinuteTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute()+1, job.Second, 0, t.Location())
|
||||||
|
return &nextMinuteTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算cron任务下下次执行时间
|
||||||
|
func calculateNextCronTime(t time.Time, job JobData) (*time.Time, error) {
|
||||||
|
if job.CronExpression == "" {
|
||||||
|
return nil, ErrCronExpression
|
||||||
|
}
|
||||||
|
if job.CronSchedule == nil {
|
||||||
|
return nil, ErrCronParser
|
||||||
|
}
|
||||||
|
|
||||||
|
s := *job.CronSchedule
|
||||||
|
|
||||||
|
next := s.Next(t)
|
||||||
|
return &next, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCronSche(CronExpression string, cronParser *cron.Parser) (*cron.Schedule, error) {
|
||||||
|
if CronExpression == "" {
|
||||||
|
return nil, ErrCronExpression
|
||||||
|
}
|
||||||
|
if cronParser == nil {
|
||||||
|
return nil, ErrCronParser
|
||||||
|
}
|
||||||
|
sche, err := cronParser.Parse(CronExpression)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &sche, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否本周期可以运行
|
// 检查是否本周期可以运行
|
||||||
|
// 检查是否本周期可以运行(已弃用,使用新的时间比较逻辑)
|
||||||
|
// 保留此函数用于向后兼容,但建议使用新的时间计算逻辑
|
||||||
func canRun(t time.Time, job JobData) bool {
|
func canRun(t time.Time, job JobData) bool {
|
||||||
|
targetTime := time.Date(t.Year(), t.Month(), t.Day(), job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
|
|
||||||
switch job.JobType {
|
switch job.JobType {
|
||||||
case JobTypeEveryMonth:
|
case JobTypeEveryMonth:
|
||||||
return t.Day() < job.Day ||
|
// 对于月任务,需要比较日期
|
||||||
(t.Day() == job.Day && t.Hour() < job.Hour) ||
|
targetTime = time.Date(t.Year(), t.Month(), job.Day, job.Hour, job.Minute, job.Second, 0, t.Location())
|
||||||
(t.Day() == job.Day && t.Hour() == job.Hour && t.Minute() < job.Minute) ||
|
return !targetTime.Before(t)
|
||||||
(t.Day() == job.Day && t.Hour() == job.Hour && t.Minute() == job.Minute && t.Second() <= job.Second)
|
|
||||||
case JobTypeEveryWeek:
|
case JobTypeEveryWeek:
|
||||||
return t.Weekday() < job.Weekday ||
|
// 对于周任务,需要比较星期
|
||||||
(t.Weekday() == job.Weekday && t.Hour() < job.Hour) ||
|
currentWeekday := t.Weekday()
|
||||||
(t.Weekday() == job.Weekday && t.Hour() == job.Hour && t.Minute() < job.Minute) ||
|
if currentWeekday < job.Weekday {
|
||||||
(t.Weekday() == job.Weekday && t.Hour() == job.Hour && t.Minute() == job.Minute && t.Second() <= job.Second)
|
return true
|
||||||
|
}
|
||||||
|
if currentWeekday == job.Weekday {
|
||||||
|
return targetTime.After(t) || targetTime.Equal(t)
|
||||||
|
}
|
||||||
|
return false
|
||||||
case JobTypeEveryDay:
|
case JobTypeEveryDay:
|
||||||
return t.Hour() < job.Hour ||
|
return targetTime.After(t) || targetTime.Equal(t)
|
||||||
(t.Hour() == job.Hour && t.Minute() < job.Minute) ||
|
|
||||||
(t.Hour() == job.Hour && t.Minute() == job.Minute && t.Second() <= job.Second)
|
|
||||||
case JobTypeEveryHour:
|
case JobTypeEveryHour:
|
||||||
return t.Minute() < job.Minute ||
|
hourTarget := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), job.Minute, job.Second, 0, t.Location())
|
||||||
(t.Minute() == job.Minute && t.Second() <= job.Second)
|
return hourTarget.After(t) || hourTarget.Equal(t)
|
||||||
case JobTypeEveryMinute:
|
case JobTypeEveryMinute:
|
||||||
return t.Second() <= job.Second
|
minuteTarget := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), job.Second, 0, t.Location())
|
||||||
|
return minuteTarget.After(t) || minuteTarget.Equal(t)
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
+779
-28
@@ -1,88 +1,131 @@
|
|||||||
package timerx_test
|
package timerx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yuninks/timerx"
|
"github.com/robfig/cron/v3"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetNextTime(t *testing.T) {
|
func TestGetNextTime(t *testing.T) {
|
||||||
|
|
||||||
|
tt := time.Date(2025, 10, 16, 10, 30, 5, 0, time.Local)
|
||||||
|
|
||||||
|
cronExpression := "0 0 10 * * ?" // Every day at 10:00 AM
|
||||||
|
parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
|
||||||
|
sche, err := GetCronSche(cronExpression, &parser)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// ttt := (*sche).Next(tt)
|
||||||
|
// fmt.Println(ttt.Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
// Test cases
|
// Test cases
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
job timerx.JobData
|
job JobData
|
||||||
expectedTime time.Time
|
expectedTime time.Time
|
||||||
expectedError error
|
expectedError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test JobTypeEveryMonth",
|
name: "Test JobTypeEveryMonth",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeEveryMonth,
|
JobType: JobTypeEveryMonth,
|
||||||
Day: 15,
|
Day: 15,
|
||||||
Hour: 10,
|
Hour: 10,
|
||||||
Minute: 0,
|
Minute: 0,
|
||||||
Second: 0,
|
Second: 0,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 15, 10, 0, 0, 0, time.Local),
|
expectedTime: time.Date(tt.Year(), tt.Month()+1, 15, 10, 0, 0, 0, time.Local),
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test JobTypeEveryWeek",
|
name: "Test JobTypeEveryWeek",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeEveryWeek,
|
JobType: JobTypeEveryWeek,
|
||||||
Weekday: time.Tuesday,
|
Weekday: time.Tuesday,
|
||||||
Hour: 10,
|
Hour: 10,
|
||||||
Minute: 0,
|
Minute: 0,
|
||||||
Second: 0,
|
Second: 0,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 8, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022
|
expectedTime: time.Date(2025, 10, 21, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test JobTypeEveryDay",
|
name: "Test JobTypeEveryDay",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeEveryDay,
|
JobType: JobTypeEveryDay,
|
||||||
Hour: 10,
|
Hour: 10,
|
||||||
Minute: 0,
|
Minute: 0,
|
||||||
Second: 0,
|
Second: 0,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 8, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022
|
expectedTime: time.Date(2025, 10, 17, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test JobTypeEveryHour",
|
name: "Test JobTypeEveryHour",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeEveryHour,
|
JobType: JobTypeEveryHour,
|
||||||
Minute: 0,
|
Minute: 0,
|
||||||
Second: 0,
|
Second: 0,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 7, 11, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
expectedTime: time.Date(2025, 10, 16, 11, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test JobTypeEveryMinute",
|
name: "Test JobTypeEveryMinute",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeEveryMinute,
|
JobType: JobTypeEveryMinute,
|
||||||
Second: 0,
|
Second: 12,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 7, 10, 31, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
expectedTime: time.Date(tt.Year(), tt.Month(), tt.Day(), tt.Hour(), tt.Minute(), 12, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Test JobTypeInterval",
|
name: "Test JobTypeIntervalHour",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobTypeInterval,
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: tt,
|
||||||
IntervalTime: 1 * time.Hour,
|
IntervalTime: 1 * time.Hour,
|
||||||
},
|
},
|
||||||
expectedTime: time.Date(2022, 3, 7, 11, 30, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
expectedTime: time.Date(2025, 10, 16, 12, 00, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Test JobTypeIntervalMinute",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: tt,
|
||||||
|
IntervalTime: 1 * time.Minute,
|
||||||
|
},
|
||||||
|
expectedTime: time.Date(2025, 10, 16, 10, 31, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test JobTypeIntervalSecond",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: tt,
|
||||||
|
IntervalTime: 1 * time.Second,
|
||||||
|
},
|
||||||
|
expectedTime: tt.Add(1 * time.Second), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Test JobTypeCron",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeCron,
|
||||||
|
CronExpression: cronExpression,
|
||||||
|
CronSchedule: sche,
|
||||||
|
},
|
||||||
|
expectedTime: time.Date(2025, 10, 17, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Test unknown JobType",
|
name: "Test unknown JobType",
|
||||||
job: timerx.JobData{
|
job: JobData{
|
||||||
JobType: timerx.JobType(100),
|
JobType: JobType("100"),
|
||||||
},
|
},
|
||||||
expectedTime: time.Time{},
|
expectedTime: time.Time{},
|
||||||
expectedError: errors.New("未知的任务类型: 100"),
|
expectedError: errors.New("未知的任务类型: 100"),
|
||||||
@@ -91,18 +134,726 @@ func TestGetNextTime(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
now := time.Now()
|
|
||||||
// loc := time.FixedZone("CST", 8*3600)
|
// loc := time.FixedZone("CST", 8*3600)
|
||||||
nextTime, err := timerx.GetNextTime(now, test.job)
|
nextTime, err := GetNextTime(tt, test.job)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if test.expectedError == nil || err.Error() != test.expectedError.Error() {
|
if test.expectedError == nil || err.Error() != test.expectedError.Error() {
|
||||||
t.Errorf("Expected error: %v, Got error: %v", test.expectedError, err)
|
t.Errorf("Expected error: %v, Got error: %v", test.expectedError, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if nextTime.IsZero() != (test.expectedTime == time.Time{}) || (nextTime != &test.expectedTime) {
|
if nextTime.IsZero() != (test.expectedTime == time.Time{}) || (!nextTime.Equal(test.expectedTime)) {
|
||||||
t.Errorf("Expected time: %v, Got time: %v", test.expectedTime, nextTime)
|
t.Errorf("Expected time: %v, Got time: %v", test.expectedTime, nextTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 测试参数验证
|
||||||
|
func TestValidateJobData(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
expected error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "有效月任务",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 15,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效月任务-日期太小",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 0,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrMonthDay,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效月任务-日期太大",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 32,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrMonthDay,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "有效周任务",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Monday,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效周任务-星期超出范围",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Weekday(7), // 超出范围
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrWeekday,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "有效间隔任务",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: time.Now(),
|
||||||
|
IntervalTime: time.Minute,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效间隔任务-间隔时间为0",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: time.Now(),
|
||||||
|
IntervalTime: 0,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrIntervalTime,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效间隔任务-创建时间为空",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: time.Time{},
|
||||||
|
IntervalTime: time.Minute,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrBaseTime,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效小时",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 24, // 无效小时
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrHour,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效分钟",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 60, // 无效分钟
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrMinute,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效秒数",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 60, // 无效秒数
|
||||||
|
},
|
||||||
|
expected: ErrSecond,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
err := validateJobData(tt.job)
|
||||||
|
assert.Equal(t, tt.expected, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试间隔任务
|
||||||
|
func TestCalculateNextInterval(t *testing.T) {
|
||||||
|
now := time.Date(2023, 6, 15, 12, 0, 0, 0, time.UTC)
|
||||||
|
createTime := time.Date(2023, 6, 15, 10, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "间隔1小时-当前时间在创建时间之后",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: createTime,
|
||||||
|
IntervalTime: time.Hour,
|
||||||
|
},
|
||||||
|
currentTime: now,
|
||||||
|
expected: time.Date(2023, 6, 15, 13, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "间隔30分钟-刚好在间隔点上",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: createTime,
|
||||||
|
IntervalTime: 30 * time.Minute,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 15, 12, 30, 0, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 6, 15, 13, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "间隔1天-跨天",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: createTime,
|
||||||
|
IntervalTime: 24 * time.Hour,
|
||||||
|
},
|
||||||
|
currentTime: now,
|
||||||
|
expected: time.Date(2023, 6, 16, 0, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextInterval(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试月任务
|
||||||
|
func TestCalculateNextMonthTime(t *testing.T) {
|
||||||
|
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "本月还能执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 20,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 20, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "本月已过,下个月执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 10,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 7, 10, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "2月30日调整到2月28日",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 30,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 2, 15, 12, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "闰年2月29日",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 29,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2024, 2, 15, 12, 30, 45, 0, time.UTC), // 2024是闰年
|
||||||
|
expected: time.Date(2024, 2, 29, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "跨年",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 15,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 12, 20, 12, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2024, 1, 15, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextMonthTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateNextMonthTimeOnce(t *testing.T) {
|
||||||
|
// baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "2月30日调整到2月28日",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 30,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 1, 31, 12, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextMonthTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试周任务
|
||||||
|
func TestCalculateNextWeekTime(t *testing.T) {
|
||||||
|
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC) // 星期四
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "本周还能执行-周五",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Friday,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "本周已过,下周执行-周三",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Wednesday,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 21, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "同一天但时间已过",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Thursday,
|
||||||
|
Hour: 10, // 早于当前时间
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 22, 10, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "跨月",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Monday,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 30, 12, 30, 45, 0, time.UTC), // 周五
|
||||||
|
expected: time.Date(2023, 7, 3, 12, 30, 45, 0, time.UTC), // 下周一
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextWeekTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试日任务
|
||||||
|
func TestCalculateNextDayTime(t *testing.T) {
|
||||||
|
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "今天还能执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 14,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 15, 14, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "今天已过,明天执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 10,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 16, 10, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "跨月",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 10,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 30, 12, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 7, 1, 10, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextDayTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试小时任务
|
||||||
|
func TestCalculateNextHourTime(t *testing.T) {
|
||||||
|
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "本小时还能执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryHour,
|
||||||
|
Minute: 45,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 15, 12, 45, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "本小时已过,下小时执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryHour,
|
||||||
|
Minute: 15,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 15, 13, 15, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "跨天",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryHour,
|
||||||
|
Minute: 15,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 15, 23, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 6, 16, 0, 15, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextHourTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试分钟任务
|
||||||
|
func TestCalculateNextMinuteTime(t *testing.T) {
|
||||||
|
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "本分钟还能执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMinute,
|
||||||
|
Second: 50,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 15, 12, 30, 50, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "本分钟已过,下分钟执行",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMinute,
|
||||||
|
Second: 30,
|
||||||
|
},
|
||||||
|
currentTime: baseTime,
|
||||||
|
expected: time.Date(2023, 6, 15, 12, 31, 30, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "跨小时",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMinute,
|
||||||
|
Second: 30,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 15, 12, 59, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 6, 15, 13, 0, 30, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := calculateNextMinuteTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试GetNextTime集成函数
|
||||||
|
func TestGetNextTime_Integration(t *testing.T) {
|
||||||
|
now := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "月任务集成测试",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 20,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
expected: time.Date(2023, 6, 20, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "周任务集成测试",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryWeek,
|
||||||
|
Weekday: time.Friday,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "间隔任务集成测试",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeInterval,
|
||||||
|
BaseTime: time.Date(2023, 6, 15, 10, 0, 0, 0, time.UTC),
|
||||||
|
IntervalTime: time.Hour,
|
||||||
|
},
|
||||||
|
expected: time.Date(2023, 6, 15, 13, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := GetNextTime(now, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试错误情况
|
||||||
|
func TestGetNextTime_ErrorCases(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
expected error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "未知任务类型",
|
||||||
|
job: JobData{
|
||||||
|
JobType: "99", // 无效类型
|
||||||
|
},
|
||||||
|
expected: errors.New("未知的任务类型: 99"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "无效月任务日期",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 32, // 无效日期
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 0,
|
||||||
|
},
|
||||||
|
expected: ErrMonthDay,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := GetNextTime(now, tt.job)
|
||||||
|
assert.Nil(t, result)
|
||||||
|
assert.Equal(t, tt.expected, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试边界条件
|
||||||
|
func TestGetNextTime_EdgeCases(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
job JobData
|
||||||
|
currentTime time.Time
|
||||||
|
expected time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "刚好在执行时间点上-应该到下一个周期",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "刚好在执行时间点上-应该到下一个周期-秒",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 6, 16, 12, 30, 44, 0, time.UTC),
|
||||||
|
expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "闰年2月29日",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 29,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2024, 2, 15, 12, 30, 45, 0, time.UTC), // 闰年
|
||||||
|
expected: time.Date(2024, 2, 29, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "非闰年2月29日调整到28日",
|
||||||
|
job: JobData{
|
||||||
|
JobType: JobTypeEveryMonth,
|
||||||
|
Day: 29,
|
||||||
|
Hour: 12,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
},
|
||||||
|
currentTime: time.Date(2023, 2, 15, 12, 30, 45, 0, time.UTC), // 非闰年
|
||||||
|
expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := GetNextTime(tt.currentTime, tt.job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, tt.expected, *result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试时区处理
|
||||||
|
func TestGetNextTime_Timezone(t *testing.T) {
|
||||||
|
// 测试不同时区
|
||||||
|
locations := []*time.Location{
|
||||||
|
time.UTC,
|
||||||
|
time.FixedZone("TEST+8", 8*60*60),
|
||||||
|
time.FixedZone("TEST-5", -5*60*60),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, loc := range locations {
|
||||||
|
t.Run(loc.String(), func(t *testing.T) {
|
||||||
|
currentTime := time.Date(2023, 6, 15, 12, 30, 45, 0, loc)
|
||||||
|
|
||||||
|
job := JobData{
|
||||||
|
JobType: JobTypeEveryDay,
|
||||||
|
Hour: 14,
|
||||||
|
Minute: 30,
|
||||||
|
Second: 45,
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := GetNextTime(currentTime, job)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
expected := time.Date(2023, 6, 15, 14, 30, 45, 0, loc)
|
||||||
|
assert.Equal(t, expected, *result)
|
||||||
|
assert.Equal(t, loc, result.Location())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,14 +3,22 @@ package timerx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/google/uuid"
|
||||||
uuid "github.com/satori/go.uuid"
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/lockx"
|
||||||
|
"github.com/yuninks/timerx/heartbeat"
|
||||||
|
"github.com/yuninks/timerx/leader"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 功能描述
|
// 功能描述
|
||||||
@@ -20,20 +28,43 @@ import (
|
|||||||
|
|
||||||
// 单次的任务队列
|
// 单次的任务队列
|
||||||
type Once struct {
|
type Once struct {
|
||||||
ctx context.Context
|
ctx context.Context // ctx
|
||||||
logger Logger
|
cancel context.CancelFunc // cancel
|
||||||
zsetKey string
|
logger logger.Logger // 日志
|
||||||
listKey string
|
zsetKey string // 任务列表 有序集合
|
||||||
redis redis.UniversalClient
|
listKey string // 列表
|
||||||
worker Callback
|
globalLockPrefix string // 全局锁的前缀
|
||||||
|
redis redis.UniversalClient // Redis
|
||||||
|
worker Callback // 回调
|
||||||
|
keyPrefix string //
|
||||||
|
priority *priority.Priority // 全局优先级
|
||||||
|
usePriority bool //
|
||||||
|
batchSize int // 每批大小
|
||||||
|
wg sync.WaitGroup //
|
||||||
|
|
||||||
|
stopChan chan struct{} //
|
||||||
|
instanceId string // 实例ID
|
||||||
|
|
||||||
|
leader *leader.Leader // 领导
|
||||||
|
heartbeat *heartbeat.HeartBeat
|
||||||
|
|
||||||
|
executeInfoKey string // 执行情况的key 有序集合
|
||||||
|
keySeparator string // 分割符
|
||||||
|
timeout time.Duration // 任务执行超时时间
|
||||||
|
|
||||||
|
maxRunCount int // 最大重试次数 0代表不限
|
||||||
|
workerChan chan struct{} // worker
|
||||||
|
maxWorkers int // 最大worker数量
|
||||||
}
|
}
|
||||||
|
|
||||||
type OnceWorkerResp struct {
|
type OnceWorkerResp struct {
|
||||||
Retry bool // 是否重试 true
|
Retry bool // 是否重试 true
|
||||||
DelayTime time.Duration
|
DelayTime time.Duration // 等待时间
|
||||||
AttachData []byte
|
AttachData any // 附加数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OnceTaskType string
|
||||||
|
|
||||||
// 需要考虑执行失败重新放入队列的情况
|
// 需要考虑执行失败重新放入队列的情况
|
||||||
type Callback interface {
|
type Callback interface {
|
||||||
// 任务执行
|
// 任务执行
|
||||||
@@ -42,34 +73,273 @@ type Callback interface {
|
|||||||
// @param data interface{} 任务数据
|
// @param data interface{} 任务数据
|
||||||
// @return WorkerCode 任务执行结果
|
// @return WorkerCode 任务执行结果
|
||||||
// @return time.Duration 任务执行时间间隔
|
// @return time.Duration 任务执行时间间隔
|
||||||
Worker(ctx context.Context, taskType string, taskId string, attachData []byte) *OnceWorkerResp
|
Worker(ctx context.Context, taskType OnceTaskType, taskId string, attachData any) *OnceWorkerResp
|
||||||
}
|
}
|
||||||
|
|
||||||
var wo *Once = nil
|
|
||||||
var once sync.Once
|
|
||||||
|
|
||||||
type extendData struct {
|
type extendData struct {
|
||||||
Delay time.Duration
|
TaskTimes []time.Time
|
||||||
Data []byte
|
Data any
|
||||||
|
RunCount int // 运行次数
|
||||||
|
JobType jobType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type jobType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
jobTypeOnce = "once"
|
||||||
|
jobTypeList = "list"
|
||||||
|
)
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, call Callback, opts ...Option) *Once {
|
func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, call Callback, opts ...Option) (*Once, error) {
|
||||||
op := newOptions(opts...)
|
op := newOptions(opts...)
|
||||||
once.Do(func() {
|
|
||||||
wo = &Once{
|
if re == nil {
|
||||||
|
op.logger.Errorf(ctx, "redis client is nil")
|
||||||
|
return nil, errors.New("redis client is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
wo := &Once{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
logger: op.logger,
|
logger: op.logger,
|
||||||
zsetKey: "timer:once_zsetkey" + keyPrefix,
|
zsetKey: "timer:once_zsetkey" + keyPrefix,
|
||||||
listKey: "timer:once_listkey" + keyPrefix,
|
listKey: "timer:once_listkey" + keyPrefix,
|
||||||
|
executeInfoKey: "timer:once_executeInfoKey" + keyPrefix,
|
||||||
|
globalLockPrefix: "timer:once_globalLockPrefix" + keyPrefix,
|
||||||
|
usePriority: false,
|
||||||
redis: re,
|
redis: re,
|
||||||
worker: call,
|
worker: call,
|
||||||
|
keyPrefix: keyPrefix,
|
||||||
|
batchSize: op.batchSize,
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
instanceId: u.String(),
|
||||||
|
keySeparator: "[:]",
|
||||||
|
timeout: op.timeout,
|
||||||
|
maxRunCount: op.maxRunCount,
|
||||||
|
workerChan: make(chan struct{}, op.maxWorkers),
|
||||||
|
maxWorkers: op.maxWorkers,
|
||||||
}
|
}
|
||||||
go wo.getTask()
|
|
||||||
go wo.watch()
|
|
||||||
})
|
|
||||||
|
|
||||||
return wo
|
// 初始化优先级
|
||||||
|
if op.priorityType != priorityTypeNone {
|
||||||
|
|
||||||
|
wo.usePriority = true
|
||||||
|
|
||||||
|
if op.priorityType == priorityTypeVersion {
|
||||||
|
pVal, err := priority.PriorityByVersion(op.priorityVersion)
|
||||||
|
if err != nil {
|
||||||
|
wo.logger.Errorf(ctx, "PriorityByVersion version:%s err:%v", op.priorityVersion, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
op.priorityVal = pVal
|
||||||
|
}
|
||||||
|
|
||||||
|
pri, err := priority.InitPriority(
|
||||||
|
ctx,
|
||||||
|
re,
|
||||||
|
keyPrefix,
|
||||||
|
op.priorityVal,
|
||||||
|
priority.WithLogger(wo.logger),
|
||||||
|
priority.WithInstanceId(wo.instanceId),
|
||||||
|
priority.WithSource("once"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
wo.logger.Errorf(ctx, "InitPriority err:%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
wo.priority = pri
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化leader
|
||||||
|
le, err := leader.InitLeader(
|
||||||
|
ctx,
|
||||||
|
re,
|
||||||
|
wo.keyPrefix,
|
||||||
|
leader.WithLogger(wo.logger),
|
||||||
|
leader.WithPriority(wo.priority),
|
||||||
|
leader.WithInstanceId(wo.instanceId),
|
||||||
|
leader.WithSource("once"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
wo.logger.Infof(ctx, "InitLeader err:%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
wo.leader = le
|
||||||
|
|
||||||
|
// 初始化心跳
|
||||||
|
heart, err := heartbeat.InitHeartBeat(
|
||||||
|
ctx,
|
||||||
|
re,
|
||||||
|
wo.keyPrefix,
|
||||||
|
heartbeat.WithInstanceId(wo.instanceId),
|
||||||
|
heartbeat.WithLeader(wo.leader),
|
||||||
|
heartbeat.WithLogger(wo.logger),
|
||||||
|
heartbeat.WithPriority(wo.priority),
|
||||||
|
heartbeat.WithSource("once"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
wo.logger.Errorf(ctx, "InitHeartBeat err:%v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
wo.heartbeat = heart
|
||||||
|
|
||||||
|
wo.startDaemon()
|
||||||
|
|
||||||
|
return wo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 停止集群定时器
|
||||||
|
func (l *Once) Close() {
|
||||||
|
close(l.stopChan)
|
||||||
|
if l.usePriority && l.priority != nil {
|
||||||
|
l.priority.Close()
|
||||||
|
}
|
||||||
|
if l.leader != nil {
|
||||||
|
l.leader.Close()
|
||||||
|
}
|
||||||
|
if l.heartbeat != nil {
|
||||||
|
l.heartbeat.Close()
|
||||||
|
}
|
||||||
|
l.cancel()
|
||||||
|
l.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Once) startDaemon() {
|
||||||
|
|
||||||
|
// 任务调度
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.scheduleTasks()
|
||||||
|
|
||||||
|
// 任务执行
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.executeTasks()
|
||||||
|
|
||||||
|
// 清理过期任务
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.cleanExecuteInfoLoop()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Once) cleanExecuteInfoLoop() {
|
||||||
|
l.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Minute * 5)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-l.stopChan:
|
||||||
|
return
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
if l.leader.IsLeader() {
|
||||||
|
l.cleanExecuteInfo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除过期任务
|
||||||
|
func (l *Once) cleanExecuteInfo() error {
|
||||||
|
// 移除执行信息
|
||||||
|
l.redis.ZRemRangeByScore(l.ctx, l.executeInfoKey, "0", strconv.FormatInt(time.Now().Add(-15*time.Minute).UnixMilli(), 10)).Err()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 任务调度 领导
|
||||||
|
func (l *Once) scheduleTasks() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
timer := time.NewTicker(time.Millisecond * 200)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-l.stopChan:
|
||||||
|
return
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
case <-timer.C:
|
||||||
|
// 优先级
|
||||||
|
if l.usePriority {
|
||||||
|
if !l.priority.IsLatest(l.ctx) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 领导
|
||||||
|
if !l.leader.IsLeader() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
l.batchGetTasks()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 任务执行
|
||||||
|
func (l *Once) executeTasks() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
for {
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-l.stopChan:
|
||||||
|
return
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
case l.workerChan <- struct{}{}:
|
||||||
|
func() {
|
||||||
|
defer func() {
|
||||||
|
<-l.workerChan
|
||||||
|
}()
|
||||||
|
|
||||||
|
if l.usePriority && !l.priority.IsLatest(l.ctx) {
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
keys, err := l.redis.BLPop(l.ctx, time.Second*10, l.listKey).Result()
|
||||||
|
if err != nil {
|
||||||
|
if err != redis.Nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Failed to pop task: %v", err)
|
||||||
|
// Redis 异常,休眠一会儿再重试
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(keys) < 2 {
|
||||||
|
l.logger.Errorf(l.ctx, "Invalid task data: %v", keys)
|
||||||
|
// 数据异常,继续下一个
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 处理任务
|
||||||
|
go l.processTask(keys[1])
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建Redis key
|
||||||
|
func (l *Once) buildRedisKey(taskType OnceTaskType, taskId string) string {
|
||||||
|
return fmt.Sprintf("%s%s%s", taskType, l.keySeparator, taskId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析Redis Key
|
||||||
|
func (l *Once) parseRedisKey(key string) (OnceTaskType, string, error) {
|
||||||
|
parts := strings.Split(key, l.keySeparator)
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return "", "", fmt.Errorf("invalid key format: %s", key)
|
||||||
|
}
|
||||||
|
return OnceTaskType(parts[0]), parts[1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加任务(覆盖)
|
// 添加任务(覆盖)
|
||||||
@@ -78,157 +348,274 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
|
|||||||
// @param uniTaskId string 任务唯一标识
|
// @param uniTaskId string 任务唯一标识
|
||||||
// @param delayTime time.Duration 延迟时间
|
// @param delayTime time.Duration 延迟时间
|
||||||
// @param attachData interface{} 附加数据
|
// @param attachData interface{} 附加数据
|
||||||
func (w *Once) Save(taskType string, taskId string, delayTime time.Duration, attachData []byte) error {
|
func (l *Once) Save(ctx context.Context, taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any) error {
|
||||||
if delayTime.Abs() != delayTime {
|
if delayTime < 0 {
|
||||||
return fmt.Errorf("时间间隔不能为负数")
|
delayTime = 0
|
||||||
}
|
}
|
||||||
if delayTime == 0 {
|
execTime := time.Now().Add(delayTime)
|
||||||
return fmt.Errorf("时间间隔不能为0")
|
return l.save(ctx, jobTypeOnce, taskType, taskId, []time.Time{execTime}, attachData, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
redisKey := fmt.Sprintf("%s[:]%s", taskType, taskId)
|
// 指定时间添加任务(覆盖)
|
||||||
|
func (l *Once) SaveByTime(ctx context.Context, taskType OnceTaskType, taskId string, executeTime time.Time, attachData any) error {
|
||||||
|
return l.save(ctx, jobTypeOnce, taskType, taskId, []time.Time{executeTime}, attachData, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Once) SaveByList(ctx context.Context, taskType OnceTaskType, taskId string, executeTimes []time.Time, attachData any, runCount int) error {
|
||||||
|
return l.save(ctx, jobTypeList, taskType, taskId, executeTimes, attachData, runCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加任务(覆盖)
|
||||||
|
// 重复插入就代表覆盖
|
||||||
|
func (w *Once) save(ctx context.Context, jobType jobType, taskType OnceTaskType, taskId string, taskTimes []time.Time, attachData any, runCount int) error {
|
||||||
|
if len(taskTimes) == 0 {
|
||||||
|
w.logger.Errorf(ctx, "delay time must be positive taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
|
||||||
|
return ErrExecuteTime
|
||||||
|
}
|
||||||
|
|
||||||
|
if jobType == jobTypeList && len(taskTimes) <= runCount {
|
||||||
|
w.logger.Errorf(ctx, "delay time must be positive taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
|
||||||
|
return ErrRunCount
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据时间从小到大排序
|
||||||
|
sort.Slice(taskTimes, func(i, j int) bool {
|
||||||
|
return taskTimes[i].Before(taskTimes[j])
|
||||||
|
})
|
||||||
|
|
||||||
|
latestTime := taskTimes[len(taskTimes)-1]
|
||||||
|
if latestTime.Before(time.Now()) {
|
||||||
|
w.logger.Errorf(ctx, "delay time must be positive taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
|
||||||
|
return ErrDelayTime
|
||||||
|
}
|
||||||
|
|
||||||
|
nextTime := taskTimes[0]
|
||||||
|
if jobType == jobTypeList {
|
||||||
|
nextTime = taskTimes[runCount]
|
||||||
|
}
|
||||||
|
|
||||||
|
redisKey := w.buildRedisKey(taskType, taskId)
|
||||||
|
|
||||||
ed := extendData{
|
ed := extendData{
|
||||||
Delay: delayTime,
|
TaskTimes: taskTimes,
|
||||||
Data: attachData,
|
Data: attachData,
|
||||||
|
RunCount: runCount,
|
||||||
|
JobType: jobType,
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(ed)
|
b, _ := json.Marshal(ed)
|
||||||
|
|
||||||
// 写入附加数据
|
// 使用事务确保原子性
|
||||||
_, err := w.redis.SetEX(w.ctx, redisKey, b, delayTime+time.Second*5).Result()
|
pipe := w.redis.TxPipeline()
|
||||||
|
|
||||||
|
expiresTime := latestTime.Add(time.Minute * 30)
|
||||||
|
|
||||||
|
dataExpire := time.Until(expiresTime)
|
||||||
|
|
||||||
|
pipe.SetEx(w.ctx, w.keyPrefix+redisKey, b, dataExpire)
|
||||||
|
pipe.ZAdd(w.ctx, w.zsetKey, redis.Z{
|
||||||
|
Score: float64(nextTime.UnixMilli()),
|
||||||
|
Member: redisKey,
|
||||||
|
})
|
||||||
|
_, err := pipe.Exec(w.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.logger.Errorf(w.ctx, "save task failed:%v taskType:%v taskId:%v attachData:%v retryCount:%v", err, taskType, taskId, attachData, runCount)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入执行时间
|
return nil
|
||||||
_, err = w.redis.ZAdd(w.ctx, w.zsetKey, &redis.Z{
|
|
||||||
Score: float64(time.Now().Add(delayTime).UnixMilli()),
|
|
||||||
Member: redisKey,
|
|
||||||
}).Result()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加任务(不覆盖)
|
// 添加任务(不覆盖)
|
||||||
func (l *Once) Create(taskType string, taskId string, delayTime time.Duration, attachData []byte) error {
|
func (l *Once) Create(ctx context.Context, taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any) error {
|
||||||
|
if delayTime <= 0 {
|
||||||
// 判断有序集合Key是否存在,存在则报错,不存在则写入
|
delayTime = 0
|
||||||
if l.redis.Exists(l.ctx, l.zsetKey).Val() == 0 {
|
}
|
||||||
redisKey := fmt.Sprintf("%s[:]%s", taskType, taskId)
|
execTime := time.Now().Add(delayTime)
|
||||||
ed := extendData{
|
return l.create(ctx, jobTypeOnce, taskType, taskId, []time.Time{execTime}, attachData, 0)
|
||||||
Delay: delayTime,
|
|
||||||
Data: attachData,
|
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(ed)
|
|
||||||
|
|
||||||
// 写入附加数据
|
// 指定时间执行(不覆盖)
|
||||||
_, err := l.redis.SetEX(l.ctx, redisKey, b, delayTime+time.Second*5).Result()
|
func (l *Once) CreateByTime(ctx context.Context, taskType OnceTaskType, taskId string, executeTime time.Time, attachData any) error {
|
||||||
if err != nil {
|
return l.create(ctx, jobTypeOnce, taskType, taskId, []time.Time{executeTime}, attachData, 0)
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
_, err = l.redis.ZAdd(l.ctx, l.zsetKey, &redis.Z{
|
// 指定多个时间执行(不覆盖)
|
||||||
Score: float64(time.Now().Add(delayTime).UnixMilli()),
|
func (l *Once) CreateByList(ctx context.Context, taskType OnceTaskType, taskId string, taskTimes []time.Time, attachData any) error {
|
||||||
Member: redisKey,
|
return l.create(ctx, jobTypeList, taskType, taskId, taskTimes, attachData, 0)
|
||||||
}).Result()
|
}
|
||||||
|
|
||||||
|
func (l *Once) create(ctx context.Context, jobType jobType, taskType OnceTaskType, taskId string, taskTimes []time.Time, attachData any, runCount int) error {
|
||||||
|
if len(taskTimes) <= 0 {
|
||||||
|
l.logger.Errorf(ctx, "delay time must be positive taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
|
||||||
|
return ErrExecuteTime
|
||||||
|
}
|
||||||
|
|
||||||
|
redisKey := l.buildRedisKey(taskType, taskId)
|
||||||
|
|
||||||
|
score, err := l.redis.ZScore(l.ctx, l.zsetKey, redisKey).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, redis.Nil) {
|
||||||
|
return l.save(ctx, jobType, taskType, taskId, taskTimes, attachData, runCount)
|
||||||
|
}
|
||||||
|
l.logger.Errorf(l.ctx, "redis.ZScore err:%v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if score > 0 {
|
||||||
|
l.logger.Errorf(l.ctx, "task exists taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
|
||||||
|
return ErrTaskExists
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return l.save(ctx, jobType, taskType, taskId, taskTimes, attachData, runCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除任务
|
// 删除任务
|
||||||
func (w *Once) Delete(taskType string, taskId string) error {
|
func (w *Once) Delete(taskType OnceTaskType, taskId string) error {
|
||||||
redisKey := fmt.Sprintf("%s[:]%s", taskType, taskId)
|
redisKey := w.buildRedisKey(taskType, taskId)
|
||||||
|
|
||||||
w.redis.Del(w.ctx, redisKey).Result()
|
pipe := w.redis.TxPipeline()
|
||||||
|
pipe.Del(w.ctx, redisKey)
|
||||||
|
pipe.ZRem(w.ctx, w.zsetKey, redisKey)
|
||||||
|
|
||||||
w.redis.ZRem(w.ctx, w.zsetKey, redisKey).Result()
|
_, err := pipe.Exec(w.ctx)
|
||||||
|
if err != nil {
|
||||||
|
w.logger.Errorf(w.ctx, "delete task failed:%v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取任务
|
// 获取任务
|
||||||
func (l *Once) Get(taskType string, taskId string) {
|
func (l *Once) Get(taskType OnceTaskType, taskId string) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取任务
|
// 批量获取任务
|
||||||
func (w *Once) getTask() {
|
func (l *Once) batchGetTasks() {
|
||||||
timer := time.NewTicker(time.Millisecond * 200)
|
|
||||||
defer timer.Stop()
|
|
||||||
|
|
||||||
Loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-timer.C:
|
|
||||||
script := `
|
script := `
|
||||||
local token = redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])
|
local tasks = redis.call('zrangebyscore', KEYS[1], 0, ARGV[1], 'LIMIT', 0, ARGV[2])
|
||||||
for i,v in ipairs(token) do
|
if #tasks == 0 then return 0 end
|
||||||
redis.call('zrem',KEYS[1],v)
|
|
||||||
redis.call('lpush',KEYS[2],v)
|
for i, task in ipairs(tasks) do
|
||||||
|
redis.call('zrem', KEYS[1], task)
|
||||||
|
redis.call('lpush', KEYS[2], task)
|
||||||
end
|
end
|
||||||
return "OK"
|
return #tasks
|
||||||
`
|
`
|
||||||
w.redis.Eval(w.ctx, script, []string{w.zsetKey, w.listKey}, 0, time.Now().UnixMilli()).Result()
|
|
||||||
// fmt.Println(i, err)
|
|
||||||
|
|
||||||
case <-w.ctx.Done():
|
result, err := l.redis.Eval(
|
||||||
break Loop
|
l.ctx,
|
||||||
}
|
script,
|
||||||
}
|
[]string{l.zsetKey, l.listKey},
|
||||||
|
time.Now().UnixMilli(),
|
||||||
|
l.batchSize,
|
||||||
|
).Result()
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
l.logger.Errorf(l.ctx, "batch get tasks failed: %s", err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听任务
|
if count, ok := result.(int64); ok && count > 0 {
|
||||||
func (w *Once) watch() {
|
l.logger.Infof(l.ctx, "moved %d tasks to ready queue", count)
|
||||||
for {
|
|
||||||
keys, err := w.redis.BLPop(w.ctx, time.Second*10, w.listKey).Result()
|
|
||||||
if err != nil {
|
|
||||||
// fmt.Println("watch err:", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := context.WithValue(w.ctx, "trace_id", uuid.NewV4().String())
|
|
||||||
|
|
||||||
go w.doTask(ctx, keys[1])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行任务
|
// 执行任务
|
||||||
func (l *Once) doTask(ctx context.Context, key string) {
|
func (l *Once) processTask(key string) {
|
||||||
fmt.Println("任务时间:", time.Now().Format("2006-01-02 15:04:05"))
|
|
||||||
|
begin := time.Now()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(l.ctx, l.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
ctx = context.WithValue(ctx, "trace_id", u.String())
|
||||||
|
|
||||||
|
l.logger.Infof(ctx, "processTask start key:%s", key)
|
||||||
|
|
||||||
|
taskType, taskId, err := l.parseRedisKey(key)
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(ctx, "processTask parseRedisKey:%v key:%s", err, key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 这里加一个全局锁
|
||||||
|
lock, err := lockx.NewGlobalLock(ctx, l.redis, l.globalLockPrefix+key)
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(ctx, "processTask timer:获取锁失败:%s", taskId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if b, err := lock.Lock(); !b {
|
||||||
|
l.logger.Errorf(ctx, "processTask timer:获取锁失败:%s %+v", taskId, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
// 上报执行情况
|
||||||
|
executeVal := fmt.Sprintf("tid:%s|insId:%s|uuid:%s|time:%s", key, l.instanceId, u.String(), begin.Format(time.RFC3339Nano))
|
||||||
|
l.redis.ZAdd(ctx, l.executeInfoKey, redis.Z{
|
||||||
|
Score: float64(begin.UnixMilli()),
|
||||||
|
Member: executeVal,
|
||||||
|
})
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
l.logger.Errorf(ctx, "timer:回调任务panic:%s stack:%s", err, string(debug.Stack()))
|
l.logger.Errorf(ctx, "processTask panic:%s stack:%s", err, string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s := strings.Split(key, "[:]")
|
|
||||||
|
|
||||||
// 读取数据
|
// 读取数据
|
||||||
str, err := l.redis.Get(ctx, key).Result()
|
redisKey := l.keyPrefix + l.buildRedisKey(taskType, taskId)
|
||||||
|
str, err := l.redis.Get(ctx, redisKey).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Errorf(ctx, "获取数据失败 err:%s", err)
|
l.logger.Errorf(ctx, "processTask redis.Get key:%s err:%s", key, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ed := extendData{}
|
ed := extendData{}
|
||||||
json.Unmarshal([]byte(str), &ed)
|
json.Unmarshal([]byte(str), &ed)
|
||||||
|
|
||||||
resp := l.worker.Worker(ctx, s[0], s[1], ed.Data)
|
resp := l.worker.Worker(ctx, taskType, taskId, ed.Data)
|
||||||
if resp == nil {
|
l.logger.Infof(ctx, "processTask exec key:%s resp:%+v data:%s", key, resp, str)
|
||||||
|
|
||||||
|
if resp == nil || !resp.Retry {
|
||||||
|
// 完成 删除任务
|
||||||
|
// 删除任务
|
||||||
|
l.logger.Infof(ctx, "processTask delete key:%s", key)
|
||||||
|
if err := l.Delete(taskType, taskId); err != nil {
|
||||||
|
l.logger.Errorf(ctx, "processTask delete errprocessTask delete err:%v", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Retry {
|
|
||||||
// 重新放入队列
|
// 重新放入队列
|
||||||
if resp.DelayTime != 0 && resp.DelayTime == resp.DelayTime.Abs() {
|
if err := l.handleRetry(ctx, taskType, taskId, &ed, resp); err != nil {
|
||||||
ed.Delay = resp.DelayTime
|
l.logger.Errorf(ctx, "processTask handleRetry err:%v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Once) handleRetry(ctx context.Context, taskType OnceTaskType, taskId string,
|
||||||
|
ed *extendData, resp *OnceWorkerResp) error {
|
||||||
|
// 限制重试次数
|
||||||
|
ed.RunCount++
|
||||||
|
if l.maxRunCount > 0 && ed.RunCount > l.maxRunCount {
|
||||||
|
l.logger.Infof(ctx, "handleRetry task exceeded retry limit: %s %s %d", taskType, taskId, l.maxRunCount)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新延迟时间
|
||||||
|
if ed.JobType == jobTypeOnce {
|
||||||
|
ed.TaskTimes = []time.Time{time.Now().Add(resp.DelayTime)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.AttachData != nil {
|
||||||
ed.Data = resp.AttachData
|
ed.Data = resp.AttachData
|
||||||
l.logger.Infof(ctx, "任务重新放入队列:%s", key)
|
|
||||||
fmt.Println("重入时间:", time.Now().Format("2006-01-02 15:04:05"))
|
|
||||||
l.Create(s[0], s[1], ed.Delay, ed.Data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.logger.Infof(ctx, "handleRetry retrying task: %s:%s, retry count: %d",
|
||||||
|
taskType, taskId, ed.RunCount)
|
||||||
|
|
||||||
|
// 不覆盖的新建
|
||||||
|
return l.create(ctx, ed.JobType, taskType, taskId, ed.TaskTimes, ed.Data, ed.RunCount)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,7 @@
|
|||||||
package timerx
|
package timerx
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test2(t *testing.T) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,23 +1,54 @@
|
|||||||
package timerx
|
package timerx
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
logger Logger
|
logger logger.Logger
|
||||||
location *time.Location
|
location *time.Location
|
||||||
timeout time.Duration
|
timeout time.Duration // 任务最长执行时间
|
||||||
|
priorityType priorityType // 策略类型 0.不使用 1.优先级 2.版本
|
||||||
|
priorityVal int64 // 策略优先级
|
||||||
|
priorityVersion string // 策略版本的集
|
||||||
|
batchSize int
|
||||||
|
maxRunCount int // 单个任务最大运行次数 0代表不限
|
||||||
|
maxWorkers int // 最大工作协程数
|
||||||
|
cronParser *cron.Parser // cron表达式解析器
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type priorityType int8
|
||||||
|
|
||||||
|
const (
|
||||||
|
priorityTypeNone priorityType = 0 // 不使用优先级
|
||||||
|
priorityTypePriority priorityType = 1
|
||||||
|
priorityTypeVersion priorityType = 2 // 版本
|
||||||
|
)
|
||||||
|
|
||||||
func defaultOptions() Options {
|
func defaultOptions() Options {
|
||||||
|
|
||||||
|
// 默认使用Linux的定时任务兼容
|
||||||
|
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
|
||||||
|
|
||||||
return Options{
|
return Options{
|
||||||
logger: NewLogger(),
|
logger: logger.NewLogger(),
|
||||||
location: time.Local,
|
location: time.Local,
|
||||||
timeout: time.Hour,
|
timeout: time.Hour, //
|
||||||
|
priorityType: priorityTypeNone,
|
||||||
|
priorityVal: 0,
|
||||||
|
batchSize: 100,
|
||||||
|
maxRunCount: 0,
|
||||||
|
maxWorkers: 100,
|
||||||
|
cronParser: &parser,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Options)
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// 返回带默认值的配置
|
||||||
func newOptions(opts ...Option) Options {
|
func newOptions(opts ...Option) Options {
|
||||||
o := defaultOptions()
|
o := defaultOptions()
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -26,23 +57,127 @@ func newOptions(opts ...Option) Options {
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 返回空的配置
|
||||||
|
func newEmptyOptions(opts ...Option) Options {
|
||||||
|
o := Options{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&o)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
// 设置日志
|
// 设置日志
|
||||||
func SetLogger(log Logger) Option {
|
func WithLogger(log logger.Logger) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.logger = log
|
o.logger = log
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设定时区
|
// 设定时区
|
||||||
func SetTimeZone(zone *time.Location) Option {
|
func WithLocation(zone *time.Location) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.location = zone
|
o.location = zone
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置任务最长执行时间
|
// 设置任务最长执行时间
|
||||||
func SetTimeout(d time.Duration) Option {
|
func WithTimeout(d time.Duration) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.timeout = d
|
o.timeout = d
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置优先级
|
||||||
|
func WithPriority(priority int64) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.priorityType = priorityTypePriority
|
||||||
|
o.priorityVal = priority
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithPriorityByVersion(version string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.priorityType = priorityTypeVersion
|
||||||
|
o.priorityVersion = version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithBatchSize(size int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if size <= 1 {
|
||||||
|
size = 1
|
||||||
|
}
|
||||||
|
o.batchSize = size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithMaxRetryCount(count int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if count < 0 {
|
||||||
|
count = 0
|
||||||
|
}
|
||||||
|
o.maxRunCount = count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithMaxWorkers(count int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if count < 0 {
|
||||||
|
count = 10
|
||||||
|
}
|
||||||
|
o.maxWorkers = count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加cron表达式解析器
|
||||||
|
func WithCronParser(parser cron.Parser) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.cronParser = &parser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置cron表达式解析器 秒级
|
||||||
|
// "*/5 * * * * ?" => 每隔5秒执行一次
|
||||||
|
// "0 0 0 * * ?" => 每天零点执行一次
|
||||||
|
// "0 0 0 1 * ?" => 每月1日零点执行一次
|
||||||
|
// "0 */5 * * * ?" => 每隔5分钟执行一次
|
||||||
|
func WithCronParserSecond() Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
|
||||||
|
o.cronParser = &parser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置cron表达式解析器
|
||||||
|
// cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor
|
||||||
|
func WithCronParserOption(options cron.ParseOption) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
parser := cron.NewParser(options)
|
||||||
|
o.cronParser = &parser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cron表达式 与Linux的定时任务兼容
|
||||||
|
func WithCronParserLinux() Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
|
||||||
|
o.cronParser = &parser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cron表达式 符号
|
||||||
|
// @yearly @annually => 每年执行一次,等同于 "0 0 0 1 1 *"
|
||||||
|
// @monthly => 每月执行一次,等同于 "0 0 0 1 * *"
|
||||||
|
// @weekly => 每周执行一次,等同于 "0 0 0 * * 0"
|
||||||
|
// @daily @midnight => 每天执行一次,等同于 "0 0 0 * * *"
|
||||||
|
// @hourly => 每小时执行一次,等同于 "0 0 * * * *"
|
||||||
|
// @minutely => 每分钟执行一次,等同于 "0 * * * * *"
|
||||||
|
// @secondly => 每秒执行一次,等同于 "* * * * * *"
|
||||||
|
// @every(time.Duration) => 每隔指定时间执行一次,等同于 "@every 5s"
|
||||||
|
|
||||||
|
func WithCronParserDescriptor() Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
parser := cron.NewParser(cron.Descriptor)
|
||||||
|
o.cronParser = &parser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package priority
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
getInterval time.Duration // 查询周期
|
||||||
|
updateInterval time.Duration // 更新间隔
|
||||||
|
expireTime time.Duration // 有效时间
|
||||||
|
logger logger.Logger // 日志
|
||||||
|
source string // 来源服务
|
||||||
|
instanceId string // 实例ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() Options {
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
return Options{
|
||||||
|
getInterval: time.Second * 2,
|
||||||
|
updateInterval: time.Second * 4,
|
||||||
|
expireTime: time.Second * 8,
|
||||||
|
logger: logger.NewLogger(),
|
||||||
|
instanceId: u.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func newOptions(opts ...Option) Options {
|
||||||
|
o := defaultOptions()
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&o)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithLogger(log logger.Logger) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.logger = log
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新周期
|
||||||
|
func WithUpdateInterval(d time.Duration) Option {
|
||||||
|
if d.Abs() < time.Second {
|
||||||
|
d = time.Second * 5
|
||||||
|
}
|
||||||
|
return func(o *Options) {
|
||||||
|
o.updateInterval = d
|
||||||
|
o.expireTime = d*2 + time.Second
|
||||||
|
o.getInterval = d / 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithInstanceId(instanceId string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.instanceId = instanceId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSource(s string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.source = s
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,256 @@
|
|||||||
|
package priority
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 多版本场景判断当前是否最新版本
|
||||||
|
|
||||||
|
type Priority struct {
|
||||||
|
ctx context.Context // 上下文
|
||||||
|
cancel context.CancelFunc // 取消函数
|
||||||
|
priority int64 // 优先级
|
||||||
|
redis redis.UniversalClient // redis
|
||||||
|
redisKey string // redis key
|
||||||
|
logger logger.Logger // 日志
|
||||||
|
expireTime time.Duration // 过期时间
|
||||||
|
|
||||||
|
setInterval time.Duration // 尝试set的间隔
|
||||||
|
getInterval time.Duration // 尝试get的间隔
|
||||||
|
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
|
isLatest bool // 是否是最新版本
|
||||||
|
latestMux sync.RWMutex // 最新版本锁
|
||||||
|
|
||||||
|
instanceId string // 实例ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitPriority(ctx context.Context, re redis.UniversalClient, keyPrefix string, priority int64, opts ...Option) (*Priority, error) {
|
||||||
|
|
||||||
|
if re == nil {
|
||||||
|
return nil, errors.New("redis is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
conf := newOptions(opts...)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
pro := &Priority{
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
priority: priority,
|
||||||
|
redis: re,
|
||||||
|
logger: conf.logger,
|
||||||
|
redisKey: "timer:priority_" + conf.source + keyPrefix,
|
||||||
|
expireTime: conf.expireTime,
|
||||||
|
setInterval: conf.updateInterval,
|
||||||
|
getInterval: conf.getInterval,
|
||||||
|
instanceId: conf.instanceId,
|
||||||
|
}
|
||||||
|
|
||||||
|
pro.startDaemon()
|
||||||
|
|
||||||
|
return pro, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Priority) Close() {
|
||||||
|
if p.cancel != nil {
|
||||||
|
p.cancel()
|
||||||
|
}
|
||||||
|
p.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 守护进程
|
||||||
|
func (l *Priority) startDaemon() {
|
||||||
|
// 启动更新缓存
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.runUpdateLoop()
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.getLatestLoop()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Priority) runUpdateLoop() {
|
||||||
|
defer p.wg.Done()
|
||||||
|
|
||||||
|
// 立即尝试设置一次优先级
|
||||||
|
if _, err := p.setPriority(); err != nil {
|
||||||
|
p.logger.Errorf(p.ctx, "Initial priority set failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(p.setInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
if _, err := p.setPriority(); err != nil {
|
||||||
|
p.logger.Errorf(p.ctx, "Priority update failed: %v", err)
|
||||||
|
}
|
||||||
|
case <-p.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Priority) getLatestLoop() {
|
||||||
|
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
if err := l.getLatest(); err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Priority update failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(l.getInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := l.getLatest(); err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Priority update failed: %v", err)
|
||||||
|
}
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Priority) IsLatest(ctx context.Context) bool {
|
||||||
|
p.latestMux.RLock()
|
||||||
|
defer p.latestMux.RUnlock()
|
||||||
|
|
||||||
|
return p.isLatest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Priority) setPriority() (string, error) {
|
||||||
|
script := `
|
||||||
|
-- KEYS[1] 是全局优先级的key
|
||||||
|
local priorityKey = KEYS[1]
|
||||||
|
-- ARGV[1] 是新的优先级
|
||||||
|
local priority = ARGV[1]
|
||||||
|
-- ARGV[2] 是过期时间
|
||||||
|
local expireTime = ARGV[2]
|
||||||
|
|
||||||
|
-- 校验参数完整性
|
||||||
|
if not priorityKey or not priority or not expireTime then
|
||||||
|
return redis.error_reply("Missing required arguments")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 尝试将字符串转换为数字
|
||||||
|
local currentPriority = redis.call('get', priorityKey)
|
||||||
|
local currentPriorityNum = tonumber(currentPriority)
|
||||||
|
local newPriorityNum = tonumber(priority)
|
||||||
|
|
||||||
|
if not currentPriority then
|
||||||
|
-- 如果当前优先级不存在,则设置新优先级并设置TTL
|
||||||
|
redis.call('set', priorityKey, priority, 'ex', expireTime)
|
||||||
|
return { "SET" }
|
||||||
|
elseif currentPriorityNum < newPriorityNum then
|
||||||
|
-- 如果当前优先级小于新优先级,则更新优先级并更新TTL
|
||||||
|
redis.call('set', priorityKey, priority, 'ex', expireTime)
|
||||||
|
return { "RESET" }
|
||||||
|
elseif currentPriorityNum == newPriorityNum then
|
||||||
|
-- 优先级相同则更新TTL
|
||||||
|
redis.call('expire', priorityKey, expireTime)
|
||||||
|
return { "UPDATE" }
|
||||||
|
else
|
||||||
|
-- 如果当前优先级大于新优先级,则不更新
|
||||||
|
return { "NOAUCH" }
|
||||||
|
end
|
||||||
|
`
|
||||||
|
|
||||||
|
newPriorityStr := strconv.FormatInt(p.priority, 10)
|
||||||
|
|
||||||
|
result, err := p.redis.Eval(p.ctx, script, []string{p.redisKey}, newPriorityStr, p.expireTime.Seconds()).Result()
|
||||||
|
// p.logger.Infof(p.ctx, "Priority update result:%+v err:%+v", result, err)
|
||||||
|
if err != nil {
|
||||||
|
p.logger.Errorf(p.ctx, "Priority update err:%s", err.Error())
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析结果
|
||||||
|
if resultMap, ok := result.([]interface{}); ok && len(resultMap) == 1 {
|
||||||
|
resultStr := resultMap[0].(string)
|
||||||
|
return resultStr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("script error: %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Priority) getLatest() error {
|
||||||
|
// 查询Redis获取当前最高优先级
|
||||||
|
currentPriority, err := l.getCurrentPriority()
|
||||||
|
|
||||||
|
l.logger.Infof(l.ctx, "Priority getLatest currentPriority:%d l.priority:%d err:%+v", currentPriority, l.priority, err)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(l.ctx, "Priority getLatest getCurrentPriority err:%s", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if currentPriority > l.priority {
|
||||||
|
// 当前不是最新的
|
||||||
|
l.latestMux.Lock()
|
||||||
|
l.isLatest = false
|
||||||
|
l.latestMux.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
l.latestMux.Lock()
|
||||||
|
l.isLatest = true
|
||||||
|
l.latestMux.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Priority) getCurrentPriority() (int64, error) {
|
||||||
|
result, err := p.redis.Get(p.ctx, p.redisKey).Result()
|
||||||
|
if err != nil {
|
||||||
|
if err == redis.Nil {
|
||||||
|
// Key不存在,返回0作为默认值
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
priority, err := strconv.ParseInt(result, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return priority, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForceRefresh 强制刷新优先级,用于紧急情况
|
||||||
|
func (p *Priority) ForceRefresh() error {
|
||||||
|
|
||||||
|
_, err := p.setPriority()
|
||||||
|
if err != nil {
|
||||||
|
p.logger.Errorf(p.ctx, "Priority ForceRefresh setPriority err:%s", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = p.getLatest()
|
||||||
|
if err != nil {
|
||||||
|
p.logger.Errorf(p.ctx, "Priority ForceRefresh getLatest err:%s", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentMaxPriority 获取当前系统中的最大优先级
|
||||||
|
func (p *Priority) GetCurrentMaxPriority(ctx context.Context) (int64, error) {
|
||||||
|
return p.getCurrentPriority()
|
||||||
|
}
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
package priority
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getRedis() *redis.Client {
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "127.0.0.1" + ":" + "6379",
|
||||||
|
Password: "123456", // no password set
|
||||||
|
DB: 0, // use default DB
|
||||||
|
})
|
||||||
|
if client == nil {
|
||||||
|
panic("redis init error")
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPriority(t *testing.T) {
|
||||||
|
re := getRedis()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
fmt.Println("ff")
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
pro, _ := InitPriority(ctx, re, "test", 10, WithUpdateInterval(time.Second*1))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
bb := pro.IsLatest(ctx)
|
||||||
|
fmt.Println("cc:", bb)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
|
pro, _ := InitPriority(ctx, re, "test", 0, WithUpdateInterval(time.Second*1))
|
||||||
|
|
||||||
|
for i := 0; i < 25; i++ {
|
||||||
|
bb := pro.IsLatest(ctx)
|
||||||
|
fmt.Println("bb:", bb)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockRedisClient 模拟Redis客户端
|
||||||
|
type MockRedisClient struct {
|
||||||
|
redis.UniversalClient
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRedisClient) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
|
||||||
|
arguments := m.Called(ctx, script, keys, args)
|
||||||
|
return arguments.Get(0).(*redis.Cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRedisClient) Get(ctx context.Context, key string) *redis.StringCmd {
|
||||||
|
arguments := m.Called(ctx, key)
|
||||||
|
return arguments.Get(0).(*redis.StringCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRedisClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
|
||||||
|
arguments := m.Called(ctx, key, value, expiration)
|
||||||
|
return arguments.Get(0).(*redis.StatusCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitPriority(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 测试正常初始化
|
||||||
|
priority, _ := InitPriority(ctx, getRedis(), "test", 100)
|
||||||
|
assert.NotNil(t, priority)
|
||||||
|
assert.Equal(t, int64(100), priority.priority)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetPriorityScenarios(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
currentRedis interface{}
|
||||||
|
newPriority int64
|
||||||
|
expectedStatus string
|
||||||
|
expectedValue int64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "首次设置优先级",
|
||||||
|
currentRedis: nil, // Redis中不存在key
|
||||||
|
newPriority: 100,
|
||||||
|
expectedStatus: "SET",
|
||||||
|
expectedValue: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "更新更高优先级",
|
||||||
|
currentRedis: "50", // Redis中存在较低优先级
|
||||||
|
newPriority: 100,
|
||||||
|
expectedStatus: "UPDATED",
|
||||||
|
expectedValue: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "保持相同优先级",
|
||||||
|
currentRedis: "100", // Redis中存在相同优先级
|
||||||
|
newPriority: 100,
|
||||||
|
expectedStatus: "EXTENDED",
|
||||||
|
expectedValue: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "忽略较低优先级",
|
||||||
|
currentRedis: "150", // Redis中存在更高优先级
|
||||||
|
newPriority: 100,
|
||||||
|
expectedStatus: "IGNORED",
|
||||||
|
expectedValue: 150,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
redisConn := getRedis()
|
||||||
|
// 删除Key
|
||||||
|
redisConn.Del(ctx, "timer:priority_test22")
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
priority, _ := InitPriority(ctx, redisConn, "test22", tc.newPriority)
|
||||||
|
defer priority.Close()
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
|
||||||
|
_, err := priority.setPriority()
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
// assert.Equal(t, tc.expectedStatus, sta)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 并发安全测试
|
||||||
|
func TestConcurrentAccess(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
priority, _ := InitPriority(ctx, getRedis(), "testacc", 100)
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
|
||||||
|
// 并发读取IsLatest
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
results := make(chan bool, 100)
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
results <- priority.IsLatest(ctx)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
close(results)
|
||||||
|
|
||||||
|
// 所有结果应该相同
|
||||||
|
firstResult := <-results
|
||||||
|
for result := range results {
|
||||||
|
t.Log(result)
|
||||||
|
assert.Equal(t, firstResult, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 错误处理测试
|
||||||
|
func TestErrorScenarios(t *testing.T) {
|
||||||
|
t.Run("Redis连接失败", func(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
priority, _ := InitPriority(ctx, getRedis(), "test", 100)
|
||||||
|
_, err := priority.setPriority()
|
||||||
|
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Redis返回值解析错误", func(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
priority := &Priority{
|
||||||
|
redis: getRedis(),
|
||||||
|
redisKey: "timer:priority_test",
|
||||||
|
priority: 100,
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := priority.getCurrentPriority()
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package priority
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrVersionFormat = errors.New("version format error")
|
||||||
|
)
|
||||||
|
|
||||||
|
// 版本号转策略等级
|
||||||
|
func PriorityByVersion(version string) (priority int64, err error) {
|
||||||
|
// 版本不能为空
|
||||||
|
if version == "" {
|
||||||
|
return 0, ErrVersionFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// 除掉版本号中的v或V
|
||||||
|
if version[0] == 'v' || version[0] == 'V' {
|
||||||
|
version = version[1:]
|
||||||
|
}
|
||||||
|
// 用点号切割
|
||||||
|
vs := strings.Split(version, ".")
|
||||||
|
// 最多只支持5位
|
||||||
|
if len(vs) > 5 {
|
||||||
|
return 0, ErrVersionFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// base 16位
|
||||||
|
var baseNum float64 = 0
|
||||||
|
|
||||||
|
// 每一位转成数字&每一位不能大于999
|
||||||
|
for key, val := range vs {
|
||||||
|
if val == "" {
|
||||||
|
return 0, ErrVersionFormat
|
||||||
|
}
|
||||||
|
i, err := strconv.ParseInt(val, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, ErrVersionFormat
|
||||||
|
}
|
||||||
|
if i < 0 || i > 999 {
|
||||||
|
return 0, ErrVersionFormat
|
||||||
|
}
|
||||||
|
p := (4 - key) * 3
|
||||||
|
num := math.Pow10(p) * float64(i)
|
||||||
|
|
||||||
|
baseNum += num
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(baseNum), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package priority_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/yuninks/timerx/priority"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVersionToPriority(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
version string
|
||||||
|
want int64
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "standard version",
|
||||||
|
version: "1.2.3",
|
||||||
|
want: 1002003000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standard version0",
|
||||||
|
version: "0.0.0",
|
||||||
|
want: 0,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "standard version1",
|
||||||
|
version: "1.0.0",
|
||||||
|
want: 1000000000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "version with v prefix",
|
||||||
|
version: "v1.2.3",
|
||||||
|
want: 1002003000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "version with V prefix",
|
||||||
|
version: "V1.2.3",
|
||||||
|
want: 1002003000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single digit version",
|
||||||
|
version: "5",
|
||||||
|
want: 5000000000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "max digits version",
|
||||||
|
version: "999.999.999.999.999",
|
||||||
|
want: 999999999999999,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty version",
|
||||||
|
version: "",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid character",
|
||||||
|
version: "1.a.3",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero version part",
|
||||||
|
version: "1.0.3",
|
||||||
|
want: 1000003000000,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero version part 2",
|
||||||
|
version: "1.0.3.",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative version part",
|
||||||
|
version: "1.-2.3",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "version part too large",
|
||||||
|
version: "1.1000.3",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "too many parts",
|
||||||
|
version: "1.2.3.4.5.6",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty part",
|
||||||
|
version: "1..3",
|
||||||
|
want: 0,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := priority.PriorityByVersion(tt.version)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("VersionToPriority() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("VersionToPriority() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,29 @@
|
|||||||
# 功能支持
|
# 功能支持
|
||||||
|
|
||||||
1. 支持本地任务
|
1. [X] 支持本地任务
|
||||||
2. 支持集群任务
|
2. [X] 支持集群任务
|
||||||
3. 支持单次任务
|
3. [X] 支持单次任务
|
||||||
|
|
||||||
# 功能说明
|
# 功能说明
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 功能实现
|
# 功能实现
|
||||||
|
|
||||||
1. 集群间任务调度和任务的唯一依赖于redis进行实现
|
1. 集群间任务调度和任务的唯一依赖于redis进行实现
|
||||||
|
|
||||||
|
|
||||||
# 缺陷
|
# 缺陷
|
||||||
|
|
||||||
1. 集群部署时,存在新旧的代码混合问题,任务调度可能存在问题(需要根据实际需要进行版本上线/下线操作)
|
1. 针对月的任务,需要注意日期有效性,且在月末的最后一天,需要考虑月末的最后一天的下一个任务执行时间
|
||||||
|
2. 集群部署时,存在新旧的代码混合问题,任务调度可能存在问题(需要根据实际需要进行版本上线/下线操作)
|
||||||
|
3. 主从切换时也要做到平滑上下线
|
||||||
|
|
||||||
## 方案一
|
## 方案一
|
||||||
|
|
||||||
1. 启动的时候定时向redis注册任务项
|
1. 启动的时候定时向redis注册任务项
|
||||||
2. 每次计算执行时间的时候根据注册的任务项进行任务计算
|
2. 每次计算执行时间的时候根据注册的任务项进行任务计算
|
||||||
3. 注册任务项需要有下线机制,避免能运行它的节点下线了它还被执行
|
3. 注册任务项需要有下线机制,避免能运行它的节点下线了它还被执行
|
||||||
|
|
||||||
|
|
||||||
现在有根据要求根据系统时间整点运行任务的要求,这个比简单的定时重复更复杂,因为不但要按时执行,并且不能重复执行,需要全局记录任务执行的状态,由于任务的间隔时间不确定,这个任务执行状态的保存周期也是有变化的
|
现在有根据要求根据系统时间整点运行任务的要求,这个比简单的定时重复更复杂,因为不但要按时执行,并且不能重复执行,需要全局记录任务执行的状态,由于任务的间隔时间不确定,这个任务执行状态的保存周期也是有变化的
|
||||||
|
|
||||||
|
# 待实现
|
||||||
|
|
||||||
|
- [ ] 允许执行完重置任务倒计时
|
||||||
|
|||||||
@@ -4,73 +4,157 @@ package timerx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
uuid "github.com/satori/go.uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
|
"github.com/yuninks/timerx/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 简单定时器
|
// 简单定时器
|
||||||
// 1. 这个定时器的作用范围是本机
|
// 1. 这个定时器的作用范围是本机
|
||||||
// 2. 适用简单的时间间隔定时任务
|
// 2. 适用简单的时间间隔定时任务
|
||||||
|
|
||||||
// 定时器结构体
|
|
||||||
var singleWorkerList sync.Map
|
|
||||||
|
|
||||||
var singleTimerIndex int // 当前定时数目
|
|
||||||
|
|
||||||
var singleOnceLimit sync.Once // 实现单例
|
|
||||||
|
|
||||||
type Single struct {
|
type Single struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
logger Logger
|
cancel context.CancelFunc
|
||||||
|
logger logger.Logger
|
||||||
location *time.Location
|
location *time.Location
|
||||||
|
nextTime time.Time
|
||||||
|
nextTimeMux sync.RWMutex
|
||||||
|
wg sync.WaitGroup
|
||||||
|
workerList sync.Map // 任务列表,key为taskId,value为worker
|
||||||
|
timerIndex int64 // 任务索引,用于生成taskId
|
||||||
|
stopChan chan struct{} // 停止信号
|
||||||
|
hasRun sync.Map // 记录已经执行的任务,key为taskId,value为执行时间
|
||||||
|
timeout time.Duration // 单次任务超时时间
|
||||||
|
cronParser *cron.Parser // cron表达式解析器
|
||||||
}
|
}
|
||||||
|
|
||||||
var sin *Single = nil
|
|
||||||
|
|
||||||
var singleNextTime = time.Now() // 下一次执行的时间
|
|
||||||
|
|
||||||
// 定时器类
|
// 定时器类
|
||||||
// @param ctx context.Context 上下文
|
// @param ctx context.Context 上下文
|
||||||
// @param opts ...Option 配置项
|
// @param opts ...Option 配置项
|
||||||
func InitSingle(ctx context.Context, opts ...Option) *Single {
|
func InitSingle(ctx context.Context, opts ...Option) *Single {
|
||||||
singleOnceLimit.Do(func() {
|
|
||||||
op := newOptions(opts...)
|
op := newOptions(opts...)
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
sin = &Single{
|
sin := &Single{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
logger: op.logger,
|
logger: op.logger,
|
||||||
location: op.location,
|
location: op.location,
|
||||||
|
nextTime: time.Now(),
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
timeout: op.timeout,
|
||||||
|
cronParser: op.cronParser,
|
||||||
}
|
}
|
||||||
|
|
||||||
timer := time.NewTicker(time.Millisecond * 200)
|
sin.startDaemon()
|
||||||
go func(ctx context.Context) {
|
|
||||||
Loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case t := <-timer.C:
|
|
||||||
if t.Before(singleNextTime) {
|
|
||||||
// 当前时间小于下次发送时间:跳过
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// 迭代定时器
|
|
||||||
sin.iterator(ctx)
|
|
||||||
// fmt.Println("timer: 执行")
|
|
||||||
case <-ctx.Done():
|
|
||||||
// 跳出循环
|
|
||||||
break Loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sin.logger.Infof(ctx, "timer: initend")
|
|
||||||
}(ctx)
|
|
||||||
})
|
|
||||||
|
|
||||||
return sin
|
return sin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Single) startDaemon() {
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.timerLoop()
|
||||||
|
|
||||||
|
l.wg.Add(1)
|
||||||
|
go l.cleanupLoop()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止所有定时任务
|
||||||
|
func (l *Single) Stop() {
|
||||||
|
close(l.stopChan)
|
||||||
|
|
||||||
|
if l.cancel != nil {
|
||||||
|
l.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.wg.Wait()
|
||||||
|
l.logger.Infof(l.ctx, "timer single: stopped")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取任务数量
|
||||||
|
func (l *Single) TaskCount() int {
|
||||||
|
count := 0
|
||||||
|
l.workerList.Range(func(k, v interface{}) bool {
|
||||||
|
count++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Single) MaxIndex() int64 {
|
||||||
|
return atomic.LoadInt64(&l.timerIndex) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定时器主循环
|
||||||
|
func (l *Single) timerLoop() {
|
||||||
|
defer l.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(100 * time.Millisecond) // 提高精度到100ms
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case t := <-ticker.C:
|
||||||
|
l.nextTimeMux.RLock()
|
||||||
|
nextTime := l.nextTime
|
||||||
|
l.nextTimeMux.RUnlock()
|
||||||
|
|
||||||
|
if t.Before(nextTime) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
l.iterator(l.ctx)
|
||||||
|
|
||||||
|
case <-l.ctx.Done():
|
||||||
|
l.logger.Infof(l.ctx, "timer: context cancelled, stopping timer loop")
|
||||||
|
return
|
||||||
|
case <-l.stopChan:
|
||||||
|
l.logger.Infof(l.ctx, "timer: received stop signal, stopping timer loop")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理循环
|
||||||
|
func (s *Single) cleanupLoop() {
|
||||||
|
defer s.wg.Done()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
now := time.Now()
|
||||||
|
cleanupTime := now.Add(-2 * time.Minute) // 清理2分钟前的记录
|
||||||
|
|
||||||
|
s.hasRun.Range(func(k, v any) bool {
|
||||||
|
t, ok := v.(time.Time)
|
||||||
|
if !ok || t.Before(cleanupTime) {
|
||||||
|
s.hasRun.Delete(k)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
s.logger.Infof(s.ctx, "timer: context cancelled, stopping cleanup loop")
|
||||||
|
return
|
||||||
|
case <-s.stopChan:
|
||||||
|
s.logger.Infof(s.ctx, "timer: received stop signal, stopping cleanup loop")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 每月执行一次
|
// 每月执行一次
|
||||||
// @param ctx 上下文
|
// @param ctx 上下文
|
||||||
// @param taskId 任务ID
|
// @param taskId 任务ID
|
||||||
@@ -81,12 +165,14 @@ func InitSingle(ctx context.Context, opts ...Option) *Single {
|
|||||||
// @param callback 回调函数
|
// @param callback 回调函数
|
||||||
// @param extendData 扩展数据
|
// @param extendData 扩展数据
|
||||||
// @return error
|
// @return error
|
||||||
func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) EveryMonth(ctx context.Context, taskId string, day int, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
|
||||||
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryMonth,
|
JobType: JobTypeEveryMonth,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Day: day,
|
Day: day,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
@@ -103,12 +189,13 @@ func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int,
|
|||||||
// @param hour int 小时
|
// @param hour int 小时
|
||||||
// @param minute int 分钟
|
// @param minute int 分钟
|
||||||
// @param second int 秒
|
// @param second int 秒
|
||||||
func (c *Single) AddWeek(ctx context.Context, taskId string, week time.Weekday, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) 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{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryWeek,
|
JobType: JobTypeEveryWeek,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Weekday: week,
|
Weekday: week,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
@@ -119,12 +206,13 @@ func (c *Single) AddWeek(ctx context.Context, taskId string, week time.Weekday,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每天执行一次
|
// 每天执行一次
|
||||||
func (c *Single) AddDay(ctx context.Context, taskId string, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) EveryDay(ctx context.Context, taskId string, hour int, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryDay,
|
JobType: JobTypeEveryDay,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Hour: hour,
|
Hour: hour,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
@@ -134,12 +222,13 @@ func (c *Single) AddDay(ctx context.Context, taskId string, hour int, minute int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每小时执行一次
|
// 每小时执行一次
|
||||||
func (c *Single) AddHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) EveryHour(ctx context.Context, taskId string, minute int, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryHour,
|
JobType: JobTypeEveryHour,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Minute: minute,
|
Minute: minute,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
@@ -148,12 +237,13 @@ func (c *Single) AddHour(ctx context.Context, taskId string, minute int, second
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每分钟执行一次
|
// 每分钟执行一次
|
||||||
func (c *Single) AddMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) EveryMinute(ctx context.Context, taskId string, second int, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
// nowTime := time.Now().In(c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeEveryMinute,
|
JobType: JobTypeEveryMinute,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
Second: second,
|
Second: second,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,103 +251,176 @@ func (c *Single) AddMinute(ctx context.Context, taskId string, second int, callb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 特定时间间隔
|
// 特定时间间隔
|
||||||
func (c *Single) AddSpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int, error) {
|
func (c *Single) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
|
||||||
nowTime := time.Now()
|
|
||||||
|
|
||||||
if spaceTime < 0 {
|
if spaceTime < 0 {
|
||||||
c.logger.Errorf(ctx, "间隔时间不能小于0")
|
c.logger.Errorf(ctx, "间隔时间不能小于0")
|
||||||
return 0, errors.New("间隔时间不能小于0")
|
return 0, ErrIntervalTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
||||||
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, c.location)
|
||||||
|
|
||||||
jobData := JobData{
|
jobData := JobData{
|
||||||
JobType: JobTypeInterval,
|
JobType: JobTypeInterval,
|
||||||
CreateTime: nowTime,
|
TaskId: taskId,
|
||||||
|
// CreateTime: nowTime,
|
||||||
|
BaseTime: zeroTime,
|
||||||
IntervalTime: spaceTime,
|
IntervalTime: spaceTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.addJob(ctx, jobData, callback, extendData)
|
return c.addJob(ctx, jobData, callback, extendData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Single) Cron(ctx context.Context, taskId string, cronExpression string, callback func(ctx context.Context, extendData any) error, extendData any, opt ...Option) (int64, error) {
|
||||||
|
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
|
||||||
|
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, l.location)
|
||||||
|
|
||||||
|
options := Options{}
|
||||||
|
for _, o := range opt {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
cronParser := l.cronParser
|
||||||
|
if options.cronParser != nil {
|
||||||
|
cronParser = options.cronParser
|
||||||
|
}
|
||||||
|
|
||||||
|
sche, err := GetCronSche(cronExpression, cronParser)
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(ctx, "timer Single Cron cronExpression error:%s", err.Error())
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
jobData := JobData{
|
||||||
|
JobType: JobTypeCron,
|
||||||
|
TaskId: taskId,
|
||||||
|
BaseTime: zeroTime, // 默认当天的零点
|
||||||
|
CronExpression: cronExpression,
|
||||||
|
CronSchedule: sche,
|
||||||
|
}
|
||||||
|
return l.addJob(ctx, jobData, callback, extendData)
|
||||||
|
}
|
||||||
|
|
||||||
// 间隔定时器
|
// 间隔定时器
|
||||||
// @param space 间隔时间
|
// @param space 间隔时间
|
||||||
// @param call 回调函数
|
// @param call 回调函数
|
||||||
// @param extend 附加参数
|
// @param extend 附加参数
|
||||||
// @return int 定时器索引
|
// @return int 定时器索引
|
||||||
// @return error 错误
|
// @return error 错误
|
||||||
func (l *Single) addJob(ctx context.Context, jobData JobData, call func(ctx context.Context, extendData interface{}) error, extend interface{}) (int, error) {
|
func (l *Single) addJob(ctx context.Context, jobData JobData, call func(ctx context.Context, extendData interface{}) error, extend interface{}) (int64, error) {
|
||||||
singleTimerIndex += 1
|
if jobData.TaskId == "" {
|
||||||
|
l.logger.Errorf(ctx, "任务ID不能为空")
|
||||||
|
return 0, ErrTaskIdEmpty
|
||||||
|
}
|
||||||
|
if jobData.Day < 0 || jobData.Day > 31 {
|
||||||
|
l.logger.Errorf(ctx, "每月的天数必须在0-31之间")
|
||||||
|
return 0, ErrMonthDay
|
||||||
|
}
|
||||||
|
if jobData.Hour < 0 || jobData.Hour > 23 {
|
||||||
|
l.logger.Errorf(ctx, "小时必须在0-23之间")
|
||||||
|
return 0, ErrHour
|
||||||
|
}
|
||||||
|
if jobData.Minute < 0 || jobData.Minute > 59 {
|
||||||
|
l.logger.Errorf(ctx, "分钟必须在0-59之间")
|
||||||
|
return 0, ErrMinute
|
||||||
|
}
|
||||||
|
if jobData.Second < 0 || jobData.Second > 59 {
|
||||||
|
l.logger.Errorf(ctx, "秒必须在0-59之间")
|
||||||
|
return 0, ErrSecond
|
||||||
|
}
|
||||||
|
if call == nil {
|
||||||
|
l.logger.Errorf(ctx, "回调函数不能为空")
|
||||||
|
return 0, ErrCallbackEmpty
|
||||||
|
}
|
||||||
|
|
||||||
_, err := GetNextTime(time.Now().In(l.location), jobData)
|
nextTime, err := GetNextTime(time.Now().In(l.location), jobData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Errorf(ctx, "获取下次执行时间失败:%s", err.Error())
|
l.logger.Errorf(ctx, "获取下次执行时间失败:%s", err.Error())
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
jobData.NextTime = *nextTime
|
||||||
|
|
||||||
|
// 生成唯一索引
|
||||||
|
index := atomic.AddInt64(&l.timerIndex, 1)
|
||||||
|
|
||||||
t := timerStr{
|
t := timerStr{
|
||||||
Callback: call,
|
Callback: call,
|
||||||
CanRunning: make(chan struct{}, 1),
|
CanRunning: make(chan struct{}, 1),
|
||||||
ExtendData: extend,
|
ExtendData: extend,
|
||||||
|
TaskId: jobData.TaskId,
|
||||||
JobData: &jobData,
|
JobData: &jobData,
|
||||||
}
|
}
|
||||||
|
|
||||||
singleWorkerList.Store(singleTimerIndex, t)
|
l.workerList.Store(index, t)
|
||||||
|
|
||||||
return singleTimerIndex, nil
|
// 计算下次执行时间(全局)
|
||||||
|
l.updateNextTimeIfEarlier(*nextTime)
|
||||||
|
|
||||||
|
return index, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果更早则更新下次执行时间(全局)
|
||||||
|
func (s *Single) updateNextTimeIfEarlier(candidate time.Time) {
|
||||||
|
s.nextTimeMux.Lock()
|
||||||
|
defer s.nextTimeMux.Unlock()
|
||||||
|
|
||||||
|
if candidate.Before(s.nextTime) {
|
||||||
|
s.nextTime = candidate
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除定时器
|
// 删除定时器
|
||||||
func (s *Single) Del(index int) {
|
func (l *Single) Del(index int64) {
|
||||||
singleWorkerList.Delete(index)
|
if _, ok := l.workerList.Load(index); ok {
|
||||||
|
l.workerList.Delete(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Single) DelByTaskId(taskId string) {
|
||||||
|
l.workerList.Range(func(k, v interface{}) bool {
|
||||||
|
timeStr, ok := v.(timerStr)
|
||||||
|
if ok && timeStr.TaskId == taskId {
|
||||||
|
l.workerList.Delete(k)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 迭代定时器列表
|
// 迭代定时器列表
|
||||||
func (s *Single) iterator(ctx context.Context) {
|
func (l *Single) iterator(ctx context.Context) {
|
||||||
|
// 当前时间
|
||||||
nowTime := time.Now().In(s.location)
|
nowTime := time.Now().In(l.location)
|
||||||
|
|
||||||
// 默认5秒后(如果没有值就暂停进来5秒)
|
// 默认5秒后(如果没有值就暂停进来5秒)
|
||||||
newNextTime := nowTime.Add(time.Second * 5)
|
newNextTime := nowTime.Add(time.Second * 5)
|
||||||
|
|
||||||
index := 0
|
l.workerList.Range(func(k, v interface{}) bool {
|
||||||
singleWorkerList.Range(func(k, v interface{}) bool {
|
timeStr, ok := v.(timerStr)
|
||||||
index++
|
if !ok {
|
||||||
timeStr := v.(timerStr)
|
l.logger.Errorf(ctx, "timer: 类型断言失败,跳过该任务")
|
||||||
|
l.workerList.Delete(k)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
if timeStr.JobData.NextTime.Before(nowTime) || timeStr.JobData.NextTime.Equal(nowTime) {
|
if timeStr.JobData.NextTime.Before(nowTime) || timeStr.JobData.NextTime.Equal(nowTime) {
|
||||||
// 可执行
|
|
||||||
nextTime, _ := GetNextTime(nowTime, *timeStr.JobData)
|
|
||||||
timeStr.JobData.NextTime = *nextTime
|
|
||||||
|
|
||||||
if index == 1 {
|
originTime := timeStr.JobData.NextTime
|
||||||
// 循环的第一个需要替换默认值
|
|
||||||
newNextTime = timeStr.JobData.NextTime
|
// 计算下次执行时间
|
||||||
|
nextTime, err := GetNextTime(nowTime, *timeStr.JobData)
|
||||||
|
if err != nil {
|
||||||
|
l.logger.Errorf(ctx, "timer: 计算下次执行时间失败:%s", err.Error())
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
// 更新下次执行时间
|
||||||
|
timeStr.JobData.NextTime = *nextTime
|
||||||
|
|
||||||
if nextTime.Before(newNextTime) {
|
if nextTime.Before(newNextTime) {
|
||||||
// 本规则下次发送时间小于系统下次需要执行的时间:替换
|
// 本规则下次发送时间小于系统下次需要执行的时间:替换
|
||||||
newNextTime = *nextTime
|
newNextTime = *nextTime
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理中就跳过本次
|
go l.executeTask(ctx, timeStr, originTime)
|
||||||
go func(ctx context.Context, v timerStr) {
|
|
||||||
select {
|
|
||||||
case v.CanRunning <- struct{}{}:
|
|
||||||
defer func() {
|
|
||||||
// fmt.Printf("timer: 执行完成 %v %v \n", k, v.Tag)
|
|
||||||
select {
|
|
||||||
case <-v.CanRunning:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
// fmt.Printf("timer: 准备执行 %v %v \n", k, v.Tag)
|
|
||||||
s.doTask(ctx, v.Callback, v.ExtendData)
|
|
||||||
default:
|
|
||||||
// fmt.Printf("timer: 已在执行 %v %v \n", k, v.Tag)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}(ctx, timeStr)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,30 +428,77 @@ func (s *Single) iterator(ctx context.Context) {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// 实际下次时间小于预期下次时间:替换
|
l.updateNextTime(newNextTime)
|
||||||
if singleNextTime.Before(newNextTime) {
|
|
||||||
// 判断一下避免异常
|
|
||||||
if newNextTime.Before(nowTime) {
|
|
||||||
// 比当前时间小
|
|
||||||
singleNextTime = nowTime
|
|
||||||
} else {
|
|
||||||
singleNextTime = newNextTime
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fmt.Println("timer: one finish")
|
// 执行任务
|
||||||
|
func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime time.Time) {
|
||||||
|
// 创建带追踪ID的上下文
|
||||||
|
|
||||||
|
u, _ := uuid.NewV7()
|
||||||
|
|
||||||
|
traceCtx := context.WithValue(ctx, "trace_id", u.String())
|
||||||
|
s.logger.Infof(traceCtx, "timer Single begin taskId:%s originTime:%d", timer.TaskId, originTime.UnixMilli())
|
||||||
|
traceCtx, cancel := context.WithTimeout(traceCtx, s.timeout) // 设置执行超时
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case timer.CanRunning <- struct{}{}:
|
||||||
|
defer func() {
|
||||||
|
select {
|
||||||
|
case <-timer.CanRunning:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 执行回调
|
||||||
|
begin := time.Now()
|
||||||
|
if err := s.doTask(traceCtx, timer, originTime); err != nil {
|
||||||
|
s.logger.Errorf(traceCtx, "timer: 任务执行失败: %s", err.Error())
|
||||||
|
}
|
||||||
|
s.logger.Infof(traceCtx, "timer Single end taskId:%s originTime:%d cost:%dms", timer.TaskId, originTime.UnixMilli(), time.Since(begin).Milliseconds())
|
||||||
|
case <-traceCtx.Done():
|
||||||
|
s.logger.Errorf(traceCtx, "timer: 任务执行超时: %s", timer.TaskId)
|
||||||
|
default:
|
||||||
|
// 任务正在执行中,跳过本次
|
||||||
|
s.logger.Infof(traceCtx, "timer: 任务正在执行中,跳过本次 %s", timer.TaskId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定时器操作类
|
// 定时器操作类
|
||||||
// 这里不应painc
|
// 这里不应painc
|
||||||
func (s *Single) doTask(ctx context.Context, call func(ctx context.Context, extendData interface{}) error, extend interface{}) error {
|
func (l *Single) doTask(ctx context.Context, timeStr timerStr, originTime time.Time) error {
|
||||||
|
// 检查任务是否已执行
|
||||||
|
taskKey := fmt.Sprintf("%s:%d", timeStr.TaskId, originTime.UnixMilli())
|
||||||
|
if _, loaded := l.hasRun.LoadOrStore(taskKey, time.Now()); loaded {
|
||||||
|
l.logger.Errorf(ctx, "timer: 任务已执行,跳过本次执行 %s", timeStr.TaskId)
|
||||||
|
return ErrTaskExecuted
|
||||||
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
s.logger.Errorf(ctx, "timer:回调任务panic err:%+v stack:%s", err, string(debug.Stack()))
|
l.logger.Errorf(ctx, "timer Single call panic err:%+v stack:%s", err, string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, "trace_id", uuid.NewV4().String)
|
err := timeStr.Callback(ctx, timeStr.ExtendData)
|
||||||
|
if err != nil {
|
||||||
return call(ctx, extend)
|
l.logger.Errorf(ctx, "timer Single call back %s, err: %v", timeStr.TaskId, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新下次执行时间
|
||||||
|
func (s *Single) updateNextTime(newTime time.Time) {
|
||||||
|
s.nextTimeMux.Lock()
|
||||||
|
defer s.nextTimeMux.Unlock()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
if newTime.Before(now) {
|
||||||
|
s.nextTime = now
|
||||||
|
} else {
|
||||||
|
s.nextTime = newTime
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+495
-12
@@ -1,26 +1,509 @@
|
|||||||
package timerx_test
|
package timerx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/yuninks/timerx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 单元测试
|
// MockLogger 用于测试的日志记录器
|
||||||
|
type MockLogger struct {
|
||||||
|
Infos []string
|
||||||
|
Errors []string
|
||||||
|
Warns []string
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
func TestHelloWorld(t *testing.T) {
|
func (m *MockLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||||
// 日志
|
m.mu.Lock()
|
||||||
// t.Log("hello world")
|
defer m.mu.Unlock()
|
||||||
|
m.Infos = append(m.Infos, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("hello world")
|
func (m *MockLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
m.Errors = append(m.Errors, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
// s := "ddd"
|
func (m *MockLogger) Warnf(ctx context.Context, format string, args ...interface{}) {
|
||||||
// t.Logf("Log测试%s", s)
|
m.mu.Lock()
|
||||||
// t.Errorf("ErrorF %s", s)
|
defer m.mu.Unlock()
|
||||||
|
m.Warns = append(m.Warns, fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
// 标记错误(继续运行)
|
func (m *MockLogger) Clear() {
|
||||||
// t.Fail()
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
m.Infos = nil
|
||||||
|
m.Errors = nil
|
||||||
|
m.Warns = nil
|
||||||
|
}
|
||||||
|
|
||||||
// 终止运行
|
// 测试基础功能
|
||||||
// t.FailNow()
|
func TestSingleTimer_Basic(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx,
|
||||||
|
timerx.WithLogger(mockLogger),
|
||||||
|
timerx.WithLocation(time.UTC))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
// 测试任务计数
|
||||||
|
assert.Equal(t, 0, timer.TaskCount())
|
||||||
|
|
||||||
|
var executionCount int32
|
||||||
|
taskFunc := func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加间隔任务
|
||||||
|
index, err := timer.EverySpace(ctx, "test-task", 100*time.Millisecond, taskFunc, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Greater(t, index, int64(0))
|
||||||
|
assert.Equal(t, 1, timer.TaskCount())
|
||||||
|
|
||||||
|
// 等待任务执行
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
assert.GreaterOrEqual(t, atomic.LoadInt32(&executionCount), int32(2))
|
||||||
|
|
||||||
|
// 删除任务
|
||||||
|
timer.Del(index)
|
||||||
|
assert.Equal(t, 0, timer.TaskCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试错误参数
|
||||||
|
func TestSingleTimer_InvalidParams(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
validFunc := func(ctx context.Context, data interface{}) error { return nil }
|
||||||
|
|
||||||
|
// 测试空taskId
|
||||||
|
_, err := timer.EverySpace(ctx, "", time.Second, validFunc, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// 测试nil回调函数
|
||||||
|
_, err = timer.EverySpace(ctx, "test", time.Second, nil, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// 测试无效间隔时间
|
||||||
|
_, err = timer.EverySpace(ctx, "test", -time.Second, validFunc, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
_, err = timer.EverySpace(ctx, "test", 0, validFunc, nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试任务去重
|
||||||
|
func TestSingleTimer_Deduplication(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
var executionCount int32
|
||||||
|
taskFunc := func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
time.Sleep(100 * time.Millisecond) // 模拟耗时任务
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加短间隔任务
|
||||||
|
_, err := timer.EverySpace(ctx, "dedup-test", 50*time.Millisecond, taskFunc, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// 等待一段时间,检查去重是否生效
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
|
||||||
|
// 应该只有1次执行(因为任务执行需要100ms,50ms的间隔会被去重)
|
||||||
|
assert.Equal(t, int32(1), atomic.LoadInt32(&executionCount))
|
||||||
|
|
||||||
|
// t.Logf("warn: %+v", mockLogger.Warns)
|
||||||
|
// t.Logf("info: %+v", mockLogger.Infos)
|
||||||
|
fmt.Println("info:", mockLogger.Infos)
|
||||||
|
fmt.Println("warn:", mockLogger.Warns)
|
||||||
|
|
||||||
|
// 检查是否有去重日志
|
||||||
|
assert.Contains(t, mockLogger.Infos, "timer: 任务正在执行中,跳过本次 dedup-test")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试并发安全
|
||||||
|
func TestSingleTimer_Concurrency(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var executionCount int32
|
||||||
|
|
||||||
|
// 并发添加任务
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
taskFunc := func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := timer.EverySpace(ctx, fmt.Sprintf("concurrent-%d", i),
|
||||||
|
time.Duration(i+1)*100*time.Millisecond, taskFunc, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
assert.Equal(t, 10, timer.TaskCount())
|
||||||
|
|
||||||
|
// 等待任务执行
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
assert.Greater(t, atomic.LoadInt32(&executionCount), int32(0))
|
||||||
|
|
||||||
|
// 并发删除任务
|
||||||
|
timer.TaskCount()
|
||||||
|
maxIndex := timer.MaxIndex()
|
||||||
|
for i := int64(1); i < maxIndex; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(index int64) {
|
||||||
|
defer wg.Done()
|
||||||
|
timer.Del(index)
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
assert.Equal(t, 0, timer.TaskCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试任务超时
|
||||||
|
func TestSingleTimer_Timeout(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger), timerx.WithTimeout(1*time.Second))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
// 长时间运行的任务
|
||||||
|
longTask := func(ctx context.Context, data interface{}) error {
|
||||||
|
fmt.Println("long task start")
|
||||||
|
select {
|
||||||
|
case <-time.After(2 * time.Second): // 超过超时时间
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := timer.EverySpace(ctx, "timeout-test", 100*time.Millisecond, longTask, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
|
// 检查是否有超时相关的错误日志
|
||||||
|
if len(mockLogger.Errors) == 0 {
|
||||||
|
t.Fatalf("expected timeout error log, got none")
|
||||||
|
}
|
||||||
|
isTimeout := false
|
||||||
|
for _, err := range mockLogger.Errors {
|
||||||
|
isTimeout = strings.Contains(err, "context deadline exceeded")
|
||||||
|
if isTimeout {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, isTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试panic恢复
|
||||||
|
func TestSingleTimer_PanicRecovery(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
panicTask := func(ctx context.Context, data interface{}) error {
|
||||||
|
panic("test panic")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := timer.EverySpace(ctx, "panic-test", 100*time.Millisecond, panicTask, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
|
// 检查是否有panic恢复日志
|
||||||
|
if len(mockLogger.Errors) == 0 {
|
||||||
|
t.Fatalf("expected panic recovery log, got none")
|
||||||
|
}
|
||||||
|
isPanic := false
|
||||||
|
for _, err := range mockLogger.Errors {
|
||||||
|
isPanic = strings.Contains(err, "timer Single call panic err")
|
||||||
|
if isPanic {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, isPanic)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 测试不同时间类型的任务
|
||||||
|
func TestSingleTimer_DifferentJobTypes(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLocation(time.UTC))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
var counts struct {
|
||||||
|
month int32
|
||||||
|
week int32
|
||||||
|
day int32
|
||||||
|
hour int32
|
||||||
|
minute int32
|
||||||
|
space int32
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UTC()
|
||||||
|
|
||||||
|
// 月任务(下个月同一天)
|
||||||
|
_, err := timer.EveryMonth(ctx, "month-job", now.Day(), now.Hour(), now.Minute(), now.Second()+1,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&counts.month, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// 周任务(下周同一天)
|
||||||
|
_, err = timer.EveryWeek(ctx, "week-job", now.Weekday(), now.Hour(), now.Minute(), now.Second()+1,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&counts.week, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// 间隔任务(立即执行)
|
||||||
|
_, err = timer.EverySpace(ctx, "space-job", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&counts.space, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
// 只有间隔任务应该执行
|
||||||
|
assert.Equal(t, int32(9), atomic.LoadInt32(&counts.space))
|
||||||
|
assert.Equal(t, int32(1), atomic.LoadInt32(&counts.month))
|
||||||
|
assert.Equal(t, int32(1), atomic.LoadInt32(&counts.week))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试上下文取消
|
||||||
|
func TestSingleTimer_ContextCancellation(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger))
|
||||||
|
|
||||||
|
var executionCount int32
|
||||||
|
_, err := timer.EverySpace(ctx, "cancel-test", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// 让任务执行一次
|
||||||
|
time.Sleep(150 * time.Millisecond)
|
||||||
|
initialCount := atomic.LoadInt32(&executionCount)
|
||||||
|
|
||||||
|
// 取消上下文
|
||||||
|
cancel()
|
||||||
|
time.Sleep(100 * time.Millisecond) // 等待停止
|
||||||
|
|
||||||
|
// 检查是否停止了执行
|
||||||
|
finalCount := atomic.LoadInt32(&executionCount)
|
||||||
|
assert.Equal(t, initialCount, finalCount) // 计数不应该再增加
|
||||||
|
|
||||||
|
// 检查是否有停止日志
|
||||||
|
assert.Contains(t, mockLogger.Infos, "timer: context cancelled, stopping timer loop")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试扩展数据传递
|
||||||
|
func TestSingleTimer_ExtendData(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
type TestData struct {
|
||||||
|
Message string
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
testData := &TestData{Message: "hello", Count: 42}
|
||||||
|
var receivedData *TestData
|
||||||
|
|
||||||
|
_, err := timer.EverySpace(ctx, "data-test", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
fmt.Println("data:", data)
|
||||||
|
if data != nil {
|
||||||
|
receivedData = data.(*TestData)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}, testData)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
t.Logf("receivedData: %+v", receivedData)
|
||||||
|
|
||||||
|
assert.NotNil(t, receivedData)
|
||||||
|
assert.Equal(t, "hello", receivedData.Message)
|
||||||
|
assert.Equal(t, 42, receivedData.Count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试任务删除
|
||||||
|
func TestSingleTimer_TaskDeletion(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
var executionCount int32
|
||||||
|
|
||||||
|
// 添加多个任务
|
||||||
|
index1, err := timer.EverySpace(ctx, "task-1", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
index2, err := timer.EverySpace(ctx, "task-2", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
atomic.AddInt32(&executionCount, 1)
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, timer.TaskCount())
|
||||||
|
|
||||||
|
// 删除一个任务
|
||||||
|
timer.Del(index1)
|
||||||
|
assert.Equal(t, 1, timer.TaskCount())
|
||||||
|
|
||||||
|
// 等待执行
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
count := atomic.LoadInt32(&executionCount)
|
||||||
|
|
||||||
|
// 应该只有task-2执行
|
||||||
|
assert.True(t, count >= 1 && count <= 2)
|
||||||
|
|
||||||
|
// 删除另一个任务
|
||||||
|
timer.Del(index2)
|
||||||
|
assert.Equal(t, 0, timer.TaskCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试GetNextTime函数(需要根据实际实现调整)
|
||||||
|
func TestGetNextTime2(t *testing.T) {
|
||||||
|
now := time.Now().UTC()
|
||||||
|
|
||||||
|
// 测试间隔任务
|
||||||
|
jobData := timerx.JobData{
|
||||||
|
JobType: timerx.JobTypeInterval,
|
||||||
|
IntervalTime: time.Minute,
|
||||||
|
// CreateTime: now,
|
||||||
|
BaseTime: now,
|
||||||
|
}
|
||||||
|
|
||||||
|
tt := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC)
|
||||||
|
|
||||||
|
nextTime, err := timerx.GetNextTime(now, jobData)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.WithinDuration(t, tt.Add(time.Minute), *nextTime, time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基准测试
|
||||||
|
func BenchmarkSingleTimer_AddAndExecute(b *testing.B) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
timer.EverySpace(ctx, fmt.Sprintf("bench-%d", i), time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试日志记录
|
||||||
|
func TestSingleTimer_Logging(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
mockLogger := &MockLogger{}
|
||||||
|
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
// 添加会panic的任务
|
||||||
|
_, err := timer.EverySpace(ctx, "logging-test", 100*time.Millisecond,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
panic("test panic for logging")
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
|
// 检查日志记录
|
||||||
|
assert.NotEmpty(t, mockLogger.Errors)
|
||||||
|
|
||||||
|
if len(mockLogger.Errors) == 0 {
|
||||||
|
t.Fatalf("expected panic recovery log, got none")
|
||||||
|
}
|
||||||
|
isPanic := false
|
||||||
|
for _, err := range mockLogger.Errors {
|
||||||
|
isPanic = strings.Contains(err, "test panic for logging")
|
||||||
|
}
|
||||||
|
assert.True(t, isPanic)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试时区处理
|
||||||
|
func TestSingleTimer_Timezone(t *testing.T) {
|
||||||
|
// 测试不同时区
|
||||||
|
locations := []*time.Location{
|
||||||
|
time.UTC,
|
||||||
|
time.FixedZone("TEST+8", 8*60*60),
|
||||||
|
time.FixedZone("TEST-5", -5*60*60),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, loc := range locations {
|
||||||
|
t.Run(loc.String(), func(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
timer := timerx.InitSingle(ctx, timerx.WithLocation(loc))
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
|
var executed bool
|
||||||
|
// now := time.Now().In(loc)
|
||||||
|
|
||||||
|
// 添加下一秒执行的任务
|
||||||
|
_, err := timer.EverySpace(ctx, "tz-test", time.Second,
|
||||||
|
func(ctx context.Context, data interface{}) error {
|
||||||
|
fmt.Println("executed in location:", loc)
|
||||||
|
executed = true
|
||||||
|
return nil
|
||||||
|
}, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
assert.True(t, executed)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,13 +3,15 @@ package timerx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type timerStr struct {
|
type timerStr struct {
|
||||||
Callback func(ctx context.Context, extendData interface{}) error // 需要回调的方法
|
Callback func(ctx context.Context, extendData any) error // 需要回调的方法
|
||||||
CanRunning chan (struct{}) // 是否允许执行(only single)
|
CanRunning chan (struct{}) // 是否允许执行(only single)
|
||||||
TaskId string // 任务ID 全局唯一键(only cluster)
|
TaskId string // 任务ID 全局唯一键
|
||||||
ExtendData interface{} // 附加参数
|
ExtendData any // 附加参数
|
||||||
JobData *JobData // 任务时间数据
|
JobData *JobData // 任务时间数据
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,13 +25,14 @@ const (
|
|||||||
JobTypeEveryMinute JobType = "every_minute" // 每分钟
|
JobTypeEveryMinute JobType = "every_minute" // 每分钟
|
||||||
JobTypeEverySecond JobType = "every_second" // 每秒
|
JobTypeEverySecond JobType = "every_second" // 每秒
|
||||||
JobTypeInterval JobType = "interval" // 指定时间间隔
|
JobTypeInterval JobType = "interval" // 指定时间间隔
|
||||||
|
JobTypeCron JobType = "cron" // cron表达式
|
||||||
)
|
)
|
||||||
|
|
||||||
type JobData struct {
|
type JobData struct {
|
||||||
JobType JobType // 任务类型
|
JobType JobType // 任务类型
|
||||||
|
TaskId string // 任务ID 全局唯一键(only cluster)
|
||||||
NextTime time.Time // 下次执行时间
|
NextTime time.Time // 下次执行时间
|
||||||
BaseTime time.Time // 基准时间(间隔的基准时间)
|
BaseTime time.Time // 基准时间(间隔的基准时间)
|
||||||
CreateTime time.Time // 任务创建时间
|
|
||||||
IntervalTime time.Duration // 任务间隔时间
|
IntervalTime time.Duration // 任务间隔时间
|
||||||
Month time.Month // 每年的第几个月
|
Month time.Month // 每年的第几个月
|
||||||
Weekday time.Weekday // 每周的周几
|
Weekday time.Weekday // 每周的周几
|
||||||
@@ -37,6 +40,8 @@ type JobData struct {
|
|||||||
Hour int // 每天的第几个小时
|
Hour int // 每天的第几个小时
|
||||||
Minute int // 每小时的第几分钟
|
Minute int // 每小时的第几分钟
|
||||||
Second int // 每分钟的第几秒
|
Second int // 每分钟的第几秒
|
||||||
|
CronExpression string // cron表达式
|
||||||
|
CronSchedule *cron.Schedule // cron表达式解析后的数据
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定义各个回调函数
|
// 定义各个回调函数
|
||||||
|
|||||||
Reference in New Issue
Block a user