34 Commits

Author SHA1 Message Date
yun 56c479b7b2 添加一些描述 2026-02-08 10:00:39 +08:00
Yun 01f1d60ab1 添加版本号测试用例 2025-12-01 14:51:20 +08:00
Yun d07f4aae64 优化一个测试项 2025-12-01 14:40:14 +08:00
Yun 092771ebd8 间隔的base为固定的2025年开始 2025-12-01 13:40:31 +08:00
yun f873130a20 传入间隔时间小于等于0就立马执行 2025-11-29 22:52:07 +08:00
Yun 058c1b8bec 修改测试代码 2025-11-29 22:41:06 +08:00
Yun 53fe8e4a8a 更新三个测试程序 2025-11-29 19:23:19 +08:00
Yun feaab7f585 修改一下依赖包版本 2025-11-29 17:56:05 +08:00
Yun c3c6fd05cb 修复一个任务调度BUG 2025-10-20 18:18:22 +08:00
Yun a9f37bc4e7 支持直接option注明版本号 2025-10-14 15:07:53 +08:00
Yun 459911a579 修改一些问题 2025-10-14 12:13:28 +08:00
Yun bcd18b698a 完善once的example 2025-10-14 12:07:47 +08:00
Yun 49f9e8bde6 修改任务没有执行的问题 2025-10-14 12:07:31 +08:00
Yun de3568de42 限制并发的goroutine数量 2025-10-14 11:13:12 +08:00
yun 1b9e9b757b 添加支持列表的时间 2025-10-10 21:15:12 +08:00
yun 2c762dee2a 修改Option与once的实现 2025-10-10 21:03:00 +08:00
yun bae669a1d9 优化部分测试用例 2025-10-06 00:13:42 +08:00
yun 9caa984846 添加example&修改集群模式调用信息 2025-10-05 21:43:41 +08:00
yun 1f9684080d 完善部分Test 2025-10-05 20:12:58 +08:00
yun 3ace37f16d 本地定时任务添加Cron 2025-10-05 19:21:39 +08:00
yun 063370380a 修改默认表达式 2025-10-05 16:33:23 +08:00
yun 372033cfa3 添加支持cron表达式 2025-10-05 16:30:41 +08:00
yun 27717c26be 处理Leader的key为空问题 2025-10-04 22:47:54 +08:00
yun 7ab9ac48c2 Redis库升级到V9 2025-10-04 22:00:08 +08:00
yun 793a8da1af 调整一下任务的关闭 2025-10-04 21:33:57 +08:00
yun 62e0d03c9e 上报执行情况 2025-10-04 20:48:08 +08:00
yun 14eb90bf7d 修改集群模式使用封装的方法 2025-10-04 20:44:16 +08:00
yun 81ce4f67d3 添加停止需要释放的资源 2025-10-04 19:00:44 +08:00
yun 737eef2157 优化时间的筛选 2025-10-04 18:51:22 +08:00
yun 2d6e77352f 本地定时任务 2025-10-02 17:54:38 +08:00
yun 304d27e0ac 去掉一个没有用到的库 2025-10-02 16:44:02 +08:00
yun fffe60f975 修改测试文件 2025-09-28 22:59:38 +08:00
yun 62bdf4fcd2 优化部分错误 2025-09-28 22:57:15 +08:00
yun e14305f66c 优化本地运行的定时任务日志打印 2025-09-28 19:49:23 +08:00
26 changed files with 1934 additions and 798 deletions
+1
View File
@@ -1,4 +1,5 @@
log
cache
test.txt
+187 -224
View File
@@ -9,10 +9,13 @@ import (
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/robfig/cron/v3"
"github.com/yuninks/cachex"
"github.com/yuninks/lockx"
"github.com/yuninks/timerx/heartbeat"
"github.com/yuninks/timerx/leader"
"github.com/yuninks/timerx/logger"
"github.com/yuninks/timerx/priority"
)
@@ -26,7 +29,6 @@ type Cluster struct {
ctx context.Context // context
cancel context.CancelFunc // 取消函数
redis redis.UniversalClient // redis
cache *cachex.Cache // 本地缓存
timeout time.Duration // job执行超时时间
logger logger.Logger // 日志
keyPrefix string // key前缀
@@ -36,21 +38,24 @@ type Cluster struct {
zsetKey string // 有序集合的key
listKey string // 可执行的任务列表的key
setKey string // 重入集合的key
heartbeatKey string // 心跳的Key
leaderKey string // 上报当前的Leader
executeInfoKey string // 执行情况的key
wg sync.WaitGroup // 等待组
workerList sync.Map // 注册的任务列表
stopChan chan struct{} //
instanceId string // 实例ID
priority *priority.Priority // 全局优先级
priorityKey string // 全局优先级的key
usePriority bool // 是否使用优先级
wg sync.WaitGroup // 等待组
isLeader bool // 是否是领导
leaderLock sync.RWMutex // 领导锁
leaderUniLockKey string // 领导唯一锁
workerList sync.Map // 注册的任务列表
stopChan chan struct{} //
instanceId string // 实例ID
leader *leader.Leader // Leader
heartbeat *heartbeat.HeartBeat // 心跳
cache *cachex.Cache // 本地缓存
cronParser *cron.Parser // cron表达式解析器
batchSize int // 批量获取任务的数量
workerChan chan struct{} // worker
maxWorkers int // 最大worker数量
}
// 初始化定时器
@@ -81,20 +86,40 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
listKey: "timer:cluster_listKey" + keyPrefix, // 列表
setKey: "timer:cluster_setKey" + keyPrefix, // 重入集合
priorityKey: "timer:cluster_priorityKey" + keyPrefix, // 全局优先级的key
leaderUniLockKey: "timer:cluster_leaderUniLockKey" + keyPrefix, // 领导唯一锁
leaderKey: "timer:cluster_leaderKey" + keyPrefix, // 上报当前Leader
heartbeatKey: "timer:cluster_heartbeatKey" + keyPrefix, // 心跳 有序集合
executeInfoKey: "timer:cluster_executeInfoKey" + keyPrefix, // 执行情况的key 有序集合
usePriority: op.usePriority,
usePriority: false,
stopChan: make(chan struct{}),
instanceId: U.String(),
cronParser: op.cronParser,
batchSize: op.batchSize,
workerChan: make(chan struct{}, op.maxWorkers),
maxWorkers: op.maxWorkers,
}
// 初始化优先级
if clu.usePriority {
if op.priorityType != priorityTypeNone {
pri, err := priority.InitPriority(ctx, red, clu.priorityKey, op.priorityVal, priority.WithLogger(clu.logger))
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
@@ -102,6 +127,39 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
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()
@@ -113,25 +171,25 @@ func InitCluster(ctx context.Context, red redis.UniversalClient, keyPrefix strin
// Stop 停止集群定时器
func (l *Cluster) Stop() {
close(l.stopChan)
l.cleanHeartbeat(true)
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.leaderElection()
// 心跳上报
l.wg.Add(1)
go l.heartbeatLoop()
// 心跳过期清理
l.wg.Add(1)
go l.cleanHeartbeatLoop()
// 任务调度
l.wg.Add(1)
go l.scheduleTasks()
@@ -140,17 +198,15 @@ func (l *Cluster) startDaemon() {
l.wg.Add(1)
go l.executeTasks()
l.wg.Add(1)
go l.cleanExecuteInfoLoop()
}
// 心跳上报
// 需要确定当前存活的实例&当前实例是否是领导
func (l *Cluster) heartbeatLoop() {
defer l.wg.Done()
func (l *Cluster) cleanExecuteInfoLoop() {
l.wg.Done()
// 先执行一次
l.heartbeat()
ticker := time.NewTicker(5 * time.Second)
ticker := time.NewTicker(time.Minute * 5)
defer ticker.Stop()
for {
@@ -160,164 +216,20 @@ func (l *Cluster) heartbeatLoop() {
case <-l.ctx.Done():
return
case <-ticker.C:
l.heartbeat()
if l.leader.IsLeader() {
l.cleanExecuteInfo()
}
}
}
}
// 单次心跳
func (l *Cluster) 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 *Cluster) cleanHeartbeatLoop() {
defer l.wg.Done()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-l.stopChan:
return
case <-l.ctx.Done():
return
case <-ticker.C:
if l.isCurrentLeader() {
l.cleanHeartbeat(false)
}
}
}
}
// 单次清理
func (l *Cluster) 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()
// 清除过期任务
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
}
// 领导选举
// 领导作用:全局推选一个人计算执行时间&移入队列,避免每个都进行计算浪费资源
func (l *Cluster) 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.stopChan:
return
case <-l.ctx.Done():
return
}
}
}
// 成为领导
func (l *Cluster) getLeaderLock() error {
// 非当前优先级不用抢leader
if l.usePriority && !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.usePriority || l.priority == nil {
return
}
for {
select {
case <-ctx.Done():
return
case <-l.stopChan:
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 *Cluster) isCurrentLeader() bool {
c.leaderLock.RLock()
defer c.leaderLock.RUnlock()
return c.isLeader
}
// scheduleTasks 调度任务(只有leader执行)
func (c *Cluster) scheduleTasks() {
defer c.wg.Done()
@@ -328,7 +240,7 @@ func (c *Cluster) scheduleTasks() {
for {
select {
case <-ticker.C:
if !c.isCurrentLeader() {
if !c.leader.IsLeader() {
continue
}
if c.usePriority && !c.priority.IsLatest(c.ctx) {
@@ -355,18 +267,19 @@ func (c *Cluster) scheduleTasks() {
// @param extendData 扩展数据
// @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 {
nowTime := time.Now().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryMonth,
CreateTime: nowTime,
TaskId: taskId,
// CreateTime: nowTime,
Day: day,
Hour: hour,
Minute: minute,
Second: second,
}
return c.addJob(ctx, taskId, jobData, callback, extendData)
return c.addJob(ctx, jobData, callback, extendData)
}
// 每周执行一次
@@ -377,82 +290,120 @@ func (c *Cluster) EveryMonth(ctx context.Context, taskId string, day int, hour i
// @param minute 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 {
nowTime := time.Now().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryWeek,
CreateTime: nowTime,
TaskId: taskId,
// CreateTime: nowTime,
Weekday: week,
Hour: hour,
Minute: minute,
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 {
nowTime := time.Now().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryDay,
CreateTime: nowTime,
TaskId: taskId,
// CreateTime: nowTime,
Hour: hour,
Minute: minute,
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 {
nowTime := time.Now().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryHour,
CreateTime: nowTime,
TaskId: taskId,
// CreateTime: nowTime,
Minute: minute,
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 {
nowTime := time.Now().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryMinute,
CreateTime: nowTime,
TaskId: taskId,
// CreateTime: nowTime,
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 {
nowTime := time.Now().In(c.location)
if spaceTime < 0 {
c.logger.Errorf(ctx, "间隔时间不能小于0")
return errors.New("间隔时间不能小于0")
}
// 获取当天的零点时间
zeroTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
// 固定时间点为20250101 00:00:00,便于计算下一次执行时间
zeroTime := time.Date(2025, 1, 1, 0, 0, 0, 0, c.location)
jobData := JobData{
JobType: JobTypeInterval,
TaskId: taskId,
BaseTime: zeroTime, // 默认当天的零点
CreateTime: nowTime,
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)
}
// 统一添加任务
@@ -462,11 +413,11 @@ func (c *Cluster) EverySpace(ctx context.Context, taskId string, spaceTime time.
// @param callback callback 回调函数
// @param extendData interface{} 扩展数据
// @return error
func (l *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 := l.workerList.Load(taskId)
_, ok := l.workerList.Load(jobData.TaskId)
if ok {
l.logger.Errorf(ctx, "Cluster addJob taskId exits:%s", taskId)
l.logger.Errorf(ctx, "Cluster addJob taskId exits:%s", jobData.TaskId)
return ErrTaskIdExists
}
@@ -480,13 +431,13 @@ func (l *Cluster) addJob(ctx context.Context, taskId string, jobData JobData, ca
t := timerStr{
Callback: callback,
ExtendData: extendData,
TaskId: taskId,
TaskId: jobData.TaskId,
JobData: &jobData,
}
l.workerList.Store(taskId, t)
l.workerList.Store(jobData.TaskId, t)
l.logger.Infof(ctx, "Cluster addJob taskId:%s", taskId)
l.logger.Infof(ctx, "Cluster addJob taskId:%s", jobData.TaskId)
return nil
}
@@ -511,10 +462,10 @@ func (l *Cluster) calculateNextTimes() {
// 使用Lua脚本原子性添加任务
script := `
local zsetKey = KEYS[1]
local lockKey = KEYS[2]
local score = ARGV[1]
local taskID = ARGV[2]
local expireTime = ARGV[3]
local lockKey = ARGV[4]
-- 检查是否已存在
local existing = redis.call('zscore', zsetKey, taskID)
@@ -533,8 +484,8 @@ func (l *Cluster) calculateNextTimes() {
`
lockKey := fmt.Sprintf("%s_%s_%d", l.keyPrefix, val.TaskId, nextTime.UnixMilli())
_, err = pipe.Eval(l.ctx, script, []string{l.zsetKey, lockKey},
nextTime.UnixMilli(), val.TaskId, 60).Result()
_, err = pipe.Eval(l.ctx, script, []string{l.zsetKey},
nextTime.UnixMilli(), val.TaskId, 60, lockKey).Result()
if err != nil {
l.logger.Errorf(l.ctx, "Failed to schedule task: %v", err)
}
@@ -565,7 +516,7 @@ func (c *Cluster) moveReadyTasks() {
`
result, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey, c.listKey},
time.Now().UnixMilli(), 100).Result()
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
@@ -581,30 +532,41 @@ func (c *Cluster) executeTasks() {
defer c.wg.Done()
for {
select {
case <-c.stopChan:
return
case <-c.ctx.Done():
return
default:
case c.workerChan <- struct{}{}:
func() {
defer func() {
<-c.workerChan
}()
if c.usePriority && !c.priority.IsLatest(c.ctx) {
time.Sleep(5 * time.Second)
continue
return
}
taskID, err := c.redis.BLPop(c.ctx, 10*time.Second, c.listKey).Result()
if err != nil {
if err != redis.Nil {
c.logger.Errorf(c.ctx, "Failed to pop task: %v", err)
// Redis 异常,休眠一会儿
time.Sleep(5 * time.Second)
}
continue
return
}
if len(taskID) < 2 {
continue
c.logger.Errorf(c.ctx, "Invalid BLPop result: %v", taskID)
// 数据异常,继续下一个
return
}
go c.processTask(taskID[1])
}()
}
}
@@ -617,6 +579,7 @@ type ReJobData struct {
// 执行任务
func (l *Cluster) processTask(taskId string) {
begin := time.Now()
ctx, cancel := context.WithTimeout(l.ctx, l.timeout)
@@ -629,8 +592,8 @@ func (l *Cluster) processTask(taskId string) {
l.logger.Infof(ctx, "doTask timer begin taskId:%s", taskId)
// 上报执行情况
executeVal := fmt.Sprintf("%s|%s|%s|%s", taskId, l.instanceId, u.String(), begin.Format(time.RFC3339Nano))
l.redis.ZAdd(ctx, l.executeInfoKey, &redis.Z{
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,
})
+25 -54
View File
@@ -6,20 +6,29 @@ import (
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"github.com/yuninks/timerx"
)
func TestCluster_AddEveryMonth(t *testing.T) {
ctx := context.Background()
redis := redis.NewClient(&redis.Options{
func redisInit() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "123456",
DB: 0,
})
}
func TestCluster_AddEveryMonth(t *testing.T) {
ctx := context.Background()
redis := redisInit()
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"
hour := 2
@@ -32,7 +41,7 @@ func TestCluster_AddEveryMonth(t *testing.T) {
}
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 {
t.Errorf("AddEveryMonth failed, err: %v", err)
}
@@ -44,12 +53,10 @@ func TestCluster_AddEveryMonth(t *testing.T) {
func TestCluster_AddEveryWeek(t *testing.T) {
ctx := context.Background()
redis := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
redis := redisInit()
defer redis.Close()
cluster,_ := timerx.InitCluster(ctx, redis, "test")
cluster, _ := timerx.InitCluster(ctx, redis, "test")
taskId := "testTask"
week := time.Sunday
@@ -73,12 +80,10 @@ func TestCluster_AddEveryWeek(t *testing.T) {
func TestCluster_AddEveryDay(t *testing.T) {
ctx := context.Background()
redis := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
redis := redisInit()
defer redis.Close()
cluster,_ := timerx.InitCluster(ctx, redis, "test")
cluster, _ := timerx.InitCluster(ctx, redis, "test")
taskId := "testTask"
hour := 2
@@ -101,12 +106,10 @@ func TestCluster_AddEveryDay(t *testing.T) {
func TestCluster_AddEveryHour(t *testing.T) {
ctx := context.Background()
redis := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
redis := redisInit()
defer redis.Close()
cluster,_ := timerx.InitCluster(ctx, redis, "test")
cluster, _ := timerx.InitCluster(ctx, redis, "test")
taskId := "testTask"
minute := 3
@@ -128,12 +131,10 @@ func TestCluster_AddEveryHour(t *testing.T) {
func TestCluster_AddEveryMinute(t *testing.T) {
ctx := context.Background()
redis := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
redis := redisInit()
defer redis.Close()
cluster,_ := timerx.InitCluster(ctx, redis, "test")
cluster, _ := timerx.InitCluster(ctx, redis, "test")
taskId := "testTask"
second := 4
@@ -156,16 +157,12 @@ func TestCluster_Add(t *testing.T) {
fmt.Println("66666")
ctx := context.Background()
fmt.Println("66666")
redis := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "123456",
DB: 0,
})
redis := redisInit()
defer redis.Close()
t.Log("6666")
cluster,_ := timerx.InitCluster(ctx, redis, "test")
cluster, _ := timerx.InitCluster(ctx, redis, "test")
taskId := "testTask"
dur := time.Second
@@ -185,29 +182,3 @@ func TestCluster_Add(t *testing.T) {
// 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()
// }
+54 -22
View File
@@ -6,7 +6,8 @@ import (
"os"
"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/priority"
)
@@ -27,13 +28,29 @@ func main() {
// re()
// d()
// cluster()
once()
// once()
single()
// prioritys()
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()
@@ -101,7 +118,7 @@ type OnceWorker struct{}
func (l OnceWorker) Worker(ctx context.Context, taskType timerx.OnceTaskType, taskId string, attachData interface{}) *timerx.OnceWorkerResp {
// 追加写入文件
file, err := os.OpenFile("./test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
file, err := os.OpenFile("./test3.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
@@ -142,30 +159,45 @@ func cluster() {
// log := loggerx.NewLogger(ctx,loggerx.SetToConsole(),loggerx.SetEscapeHTML(false))
// _ = log
cluster, _ := timerx.InitCluster(ctx, client, "test", timerx.WithPriority(103))
err := cluster.EverySpace(ctx, "test_space1", 1*time.Second, aa, "这是秒任务1")
cluster, _ := timerx.InitCluster(ctx, client, "test2", timerx.WithPriority(104))
err := cluster.EverySpace(ctx, "test_space1", 1*time.Second, callback, "这是秒任务1")
fmt.Println(err)
err = cluster.EverySpace(ctx, "test_space2", 2*time.Second, aa, "这是秒任务2")
err = cluster.EverySpace(ctx, "test_space2", 2*time.Second, callback, "这是秒任务2")
fmt.Println(err)
err = cluster.EverySpace(ctx, "test_space3", 5*time.Second, aa, "这是秒任务3")
err = cluster.EverySpace(ctx, "test_space3", 5*time.Second, callback, "这是秒任务3")
fmt.Println(err)
err = cluster.EveryMinute(ctx, "test_min1", 15, aa, "这是分钟任务1")
err = cluster.EveryMinute(ctx, "test_min1", 15, callback, "这是分钟任务1")
fmt.Println(err)
err = cluster.EveryMinute(ctx, "test_min2", 30, aa, "这是分钟任务2")
err = cluster.EveryMinute(ctx, "test_min2", 30, callback, "这是分钟任务2")
fmt.Println(err)
err = cluster.EveryHour(ctx, "test_hour1", 30, 0, aa, "这是小时任务1")
err = cluster.EveryHour(ctx, "test_hour1", 30, 0, callback, "这是小时任务1")
fmt.Println(err)
err = cluster.EveryHour(ctx, "test_hour2", 30, 15, aa, "这是小时任务2")
err = cluster.EveryHour(ctx, "test_hour2", 30, 15, callback, "这是小时任务2")
fmt.Println(err)
err = cluster.EveryDay(ctx, "test_day1", 5, 0, 0, aa, "这是天任务1")
err = cluster.EveryDay(ctx, "test_day1", 5, 0, 0, callback, "这是天任务1")
fmt.Println(err)
err = cluster.EveryDay(ctx, "test_day2", 9, 20, 0, aa, "这是天任务2")
err = cluster.EveryDay(ctx, "test_day2", 9, 20, 0, callback, "这是天任务2")
fmt.Println(err)
err = cluster.EveryDay(ctx, "test_day3", 10, 30, 30, aa, "这是天任务3")
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() {
@@ -208,17 +240,17 @@ func re() {
ctx := context.Background()
cl, _ := timerx.InitCluster(ctx, client, "kkkk")
cl.EverySpace(ctx, "test1", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test2", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test3", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test4", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test5", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test6", 1*time.Millisecond, aa, "data")
cl.EverySpace(ctx, "test1", 1*time.Millisecond, callback, "data")
cl.EverySpace(ctx, "test2", 1*time.Millisecond, callback, "data")
cl.EverySpace(ctx, "test3", 1*time.Millisecond, callback, "data")
cl.EverySpace(ctx, "test4", 1*time.Millisecond, callback, "data")
cl.EverySpace(ctx, "test5", 1*time.Millisecond, callback, "data")
cl.EverySpace(ctx, "test6", 1*time.Millisecond, callback, "data")
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)
@@ -252,7 +284,7 @@ func d() {
return
}
client.ZAdd(context.Background(), "lockx:test2", &redis.Z{
client.ZAdd(context.Background(), "lockx:test2", redis.Z{
Score: 50,
Member: "test",
})
+17
View File
@@ -3,6 +3,7 @@ package timerx
import "errors"
var (
// 定时器不存在
ErrTimerNotFound = errors.New("timer not found")
// 任务ID不能为空
ErrTaskIdEmpty = errors.New("taskId can not be empty")
@@ -20,8 +21,24 @@ var (
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")
)
+299
View File
@@ -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
}
+257
View File
@@ -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
}
+297
View File
@@ -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
}
+5 -30
View File
@@ -3,45 +3,20 @@ module github.com/yuninks/timerx
go 1.24
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/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.11.1
github.com/yuninks/cachex v1.0.5
github.com/yuninks/lockx v1.1.2
github.com/yuninks/loggerx v1.0.13
github.com/yuninks/cachex v1.0.6
github.com/yuninks/lockx v1.1.4
)
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // 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/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+14 -101
View File
@@ -1,123 +1,36 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
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/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
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/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/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/redis/go-redis/v9 v9.14.0 h1:u4tNCjXOyzfgeLN+vAZaW1xUooqWDqVEsZN0U01jfAE=
github.com/redis/go-redis/v9 v9.14.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/yuninks/cachex v1.0.5 h1:Y2NmTsuEgwEVYb7FVFh5tUN67kmrUioeksQqLbOAwsM=
github.com/yuninks/cachex v1.0.5/go.mod h1:5357qz18UvHTJSgZzkMamUzZoFzGeKG9+4tIUBXRSVM=
github.com/yuninks/lockx v1.1.2 h1:QEI68IHMHgekBu1QtPza9AS70QptMn2ShhMz7Sam35w=
github.com/yuninks/lockx v1.1.2/go.mod h1:tM46x/fp9046YnTIeddmUhkxFjJ/f0g4J+1zdaGKfd8=
github.com/yuninks/loggerx v1.0.13 h1:NVb0oHoZeJ59ZAaU5HqcpY7TfZlLaXQEzkRhdOTthiA=
github.com/yuninks/loggerx v1.0.13/go.mod h1:+QFoywQ1ICh4v40zj6OHM8GBZHEqV0yvRbkZjZUe2o4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
github.com/yuninks/cachex v1.0.6 h1:G5dJ8O0gLAGnLwydEHVCSSGv+p1z8KfZdjb5NdxxsVA=
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/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
+5 -5
View File
@@ -7,14 +7,14 @@ import (
"sync"
"time"
"github.com/go-redis/redis/v8"
"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 {
@@ -47,7 +47,7 @@ func InitHeartBeat(ctx context.Context, ref redis.UniversalClient, keyPrefix str
ctx: ctx,
cancel: cancel,
heartbeatKey: "timer:heartbeat_key" + keyPrefix,
heartbeatKey: "timer:heartbeat_key" + op.source + keyPrefix,
priority: op.priority,
redis: ref,
@@ -64,8 +64,8 @@ func InitHeartBeat(ctx context.Context, ref redis.UniversalClient, keyPrefix str
}
func (l *HeartBeat) Close() {
l.cancel()
l.cleanHeartbeat(true)
l.cancel()
l.wg.Wait()
}
@@ -103,7 +103,7 @@ func (l *HeartBeat) heartbeatLoop() {
// 单次心跳
func (l *HeartBeat) heartbeat() error {
err := l.redis.ZAdd(l.ctx, l.heartbeatKey, &redis.Z{
err := l.redis.ZAdd(l.ctx, l.heartbeatKey, redis.Z{
Score: float64(time.Now().UnixMilli()),
Member: l.instanceId,
}).Err()
+10 -3
View File
@@ -8,10 +8,11 @@ import (
)
type Options struct {
logger logger.Logger
instanceId string
logger logger.Logger // 日志
instanceId string // 实例ID
priority *priority.Priority // 全局优先级
leader *leader.Leader
leader *leader.Leader // Leader
source string // 来源服务
}
func defaultOptions() Options {
@@ -57,3 +58,9 @@ func WithInstanceId(instanceId string) Option {
o.instanceId = instanceId
}
}
func WithSource(source string) Option {
return func(o *Options) {
o.source = source
}
}
+4 -3
View File
@@ -8,7 +8,7 @@ import (
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"github.com/yuninks/lockx"
"github.com/yuninks/timerx/logger"
"github.com/yuninks/timerx/priority"
@@ -52,7 +52,8 @@ func InitLeader(ctx context.Context, ref redis.UniversalClient, keyPrefix string
ctx: ctx,
cancel: cancel,
redis: ref,
leaderUniLockKey: "timer:leader_lockKey" + keyPrefix,
leaderUniLockKey: "timer:leader_lockKey" + op.source + keyPrefix,
leaderKey: "timer:leader" + op.source + keyPrefix,
priority: op.priority,
instanceId: op.instanceId,
logger: op.logger,
@@ -61,7 +62,7 @@ func InitLeader(ctx context.Context, ref redis.UniversalClient, keyPrefix string
l.wg.Add(1)
go l.leaderElection()
l.logger.Infof(l.ctx, "InitLeader InstanceId %s lockKey:%s", l.instanceId, l.leaderUniLockKey)
l.logger.Infof(l.ctx, "InitLeader InstanceId %s lockKey:%s leaderKey:%s", l.instanceId, l.leaderUniLockKey, l.leaderKey)
return l, nil
}
+7
View File
@@ -10,6 +10,7 @@ type Options struct {
logger logger.Logger
instanceId string
priority *priority.Priority // 全局优先级
source string // 来源服务
}
func defaultOptions() Options {
@@ -49,3 +50,9 @@ func WithInstanceId(instanceId string) Option {
o.instanceId = instanceId
}
}
func WithSource(source string) Option {
return func(o *Options) {
o.source = source
}
}
+53 -7
View File
@@ -3,6 +3,8 @@ package timerx
import (
"errors"
"time"
"github.com/robfig/cron/v3"
)
// 计算该任务下次执行时间
@@ -32,6 +34,8 @@ func GetNextTime(t time.Time, job JobData) (*time.Time, error) {
next, err = calculateNextMinuteTime(t, job)
case JobTypeInterval:
next, err = calculateNextInterval(t, job)
case JobTypeCron:
next, err = calculateNextCronTime(t, job)
default:
return nil, errors.New("未知的任务类型: " + string(job.JobType))
}
@@ -70,8 +74,19 @@ func validateJobData(job JobData) error {
if job.IntervalTime <= 0 {
return ErrIntervalTime
}
if job.CreateTime.IsZero() {
return ErrCreateTime
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
}
}
@@ -88,19 +103,21 @@ func validateJobData(job JobData) error {
return nil
}
// 计算间隔任务下一次执行时间
// 固定基准时间,因为在不同的实例中需要对齐基准点
func calculateNextInterval(t time.Time, job JobData) (*time.Time, error) {
if job.CreateTime.IsZero() {
return nil, ErrCreateTime
if job.BaseTime.IsZero() {
return nil, ErrBaseTime
}
if job.IntervalTime <= 0 {
return nil, ErrIntervalTime
}
// 计算从创建时间到当前时间经过了多少个间隔
elapsed := t.Sub(job.CreateTime)
// 计算从基准时间到当前时间经过了多少个间隔
elapsed := t.Sub(job.BaseTime)
intervals := elapsed / job.IntervalTime
// 计算下一个执行时间
next := job.CreateTime.Add((intervals + 1) * job.IntervalTime)
next := job.BaseTime.Add((intervals + 1) * job.IntervalTime)
// 需要整的
next = next.Round(job.IntervalTime)
@@ -214,6 +231,35 @@ func calculateNextMinuteTime(t time.Time, job JobData) (*time.Time, error) {
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
}
// 检查是否本周期可以运行
// 检查是否本周期可以运行(已弃用,使用新的时间比较逻辑)
// 保留此函数用于向后兼容,但建议使用新的时间计算逻辑
+58 -22
View File
@@ -5,12 +5,22 @@ import (
"testing"
"time"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"
)
func TestGetNextTime(t *testing.T) {
tt := time.Now()
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
tests := []struct {
@@ -40,7 +50,7 @@ func TestGetNextTime(t *testing.T) {
Minute: 0,
Second: 0,
},
expectedTime: time.Date(2025, 9, 23, 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,
},
{
@@ -51,7 +61,7 @@ func TestGetNextTime(t *testing.T) {
Minute: 0,
Second: 0,
},
expectedTime: time.Date(2025, 9, 18, 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,
},
{
@@ -61,7 +71,7 @@ func TestGetNextTime(t *testing.T) {
Minute: 0,
Second: 0,
},
expectedTime: time.Date(2025, 9, 17, 16, 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,
},
{
@@ -70,19 +80,48 @@ func TestGetNextTime(t *testing.T) {
JobType: JobTypeEveryMinute,
Second: 12,
},
expectedTime: time.Date(tt.Year(), tt.Month(), tt.Day(), tt.Hour(), tt.Minute()+1, 12, 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,
},
{
name: "Test JobTypeInterval",
name: "Test JobTypeIntervalHour",
job: JobData{
JobType: JobTypeInterval,
CreateTime: tt,
BaseTime: tt,
IntervalTime: 1 * time.Hour,
},
expectedTime: tt.Add(1 * time.Hour), // 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,
},
{
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",
job: JobData{
@@ -95,9 +134,8 @@ func TestGetNextTime(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
now := time.Now()
// loc := time.FixedZone("CST", 8*3600)
nextTime, err := GetNextTime(now, test.job)
nextTime, err := GetNextTime(tt, test.job)
if err != nil {
if test.expectedError == nil || err.Error() != test.expectedError.Error() {
t.Errorf("Expected error: %v, Got error: %v", test.expectedError, err)
@@ -177,7 +215,7 @@ func TestValidateJobData(t *testing.T) {
name: "有效间隔任务",
job: JobData{
JobType: JobTypeInterval,
CreateTime: time.Now(),
BaseTime: time.Now(),
IntervalTime: time.Minute,
Hour: 12,
Minute: 30,
@@ -189,7 +227,7 @@ func TestValidateJobData(t *testing.T) {
name: "无效间隔任务-间隔时间为0",
job: JobData{
JobType: JobTypeInterval,
CreateTime: time.Now(),
BaseTime: time.Now(),
IntervalTime: 0,
Hour: 12,
Minute: 30,
@@ -201,13 +239,13 @@ func TestValidateJobData(t *testing.T) {
name: "无效间隔任务-创建时间为空",
job: JobData{
JobType: JobTypeInterval,
CreateTime: time.Time{},
BaseTime: time.Time{},
IntervalTime: time.Minute,
Hour: 12,
Minute: 30,
Second: 0,
},
expected: ErrCreateTime,
expected: ErrBaseTime,
},
{
name: "无效小时",
@@ -264,7 +302,7 @@ func TestCalculateNextInterval(t *testing.T) {
name: "间隔1小时-当前时间在创建时间之后",
job: JobData{
JobType: JobTypeInterval,
CreateTime: createTime,
BaseTime: createTime,
IntervalTime: time.Hour,
},
currentTime: now,
@@ -274,7 +312,7 @@ func TestCalculateNextInterval(t *testing.T) {
name: "间隔30分钟-刚好在间隔点上",
job: JobData{
JobType: JobTypeInterval,
CreateTime: createTime,
BaseTime: createTime,
IntervalTime: 30 * time.Minute,
},
currentTime: time.Date(2023, 6, 15, 12, 30, 0, 0, time.UTC),
@@ -284,11 +322,11 @@ func TestCalculateNextInterval(t *testing.T) {
name: "间隔1天-跨天",
job: JobData{
JobType: JobTypeInterval,
CreateTime: createTime,
BaseTime: createTime,
IntervalTime: 24 * time.Hour,
},
currentTime: now,
expected: time.Date(2023, 6, 16, 10, 0, 0, 0, time.UTC),
expected: time.Date(2023, 6, 16, 0, 0, 0, 0, time.UTC),
},
}
@@ -301,8 +339,6 @@ func TestCalculateNextInterval(t *testing.T) {
}
}
// 测试月任务
func TestCalculateNextMonthTime(t *testing.T) {
baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
@@ -384,7 +420,7 @@ func TestCalculateNextMonthTime(t *testing.T) {
}
}
func TestCalculateNextMonthTimeOnce(t *testing.T){
func TestCalculateNextMonthTimeOnce(t *testing.T) {
// baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC)
tests := []struct {
@@ -673,7 +709,7 @@ func TestGetNextTime_Integration(t *testing.T) {
name: "间隔任务集成测试",
job: JobData{
JobType: JobTypeInterval,
CreateTime: time.Date(2023, 6, 15, 10, 0, 0, 0, time.UTC),
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),
+158 -52
View File
@@ -6,13 +6,14 @@ import (
"errors"
"fmt"
"runtime/debug"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/yuninks/lockx"
"github.com/yuninks/timerx/heartbeat"
"github.com/yuninks/timerx/leader"
@@ -51,7 +52,9 @@ type Once struct {
keySeparator string // 分割符
timeout time.Duration // 任务执行超时时间
maxRetryCount int // 最大重试次数 0代表不限
maxRunCount int // 最大重试次数 0代表不限
workerChan chan struct{} // worker
maxWorkers int // 最大worker数量
}
type OnceWorkerResp struct {
@@ -74,11 +77,19 @@ type Callback interface {
}
type extendData struct {
Delay time.Duration
TaskTimes []time.Time
Data any
RetryCount int // 重试次数
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, error) {
op := newOptions(opts...)
@@ -100,7 +111,7 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
listKey: "timer:once_listkey" + keyPrefix,
executeInfoKey: "timer:once_executeInfoKey" + keyPrefix,
globalLockPrefix: "timer:once_globalLockPrefix" + keyPrefix,
usePriority: op.usePriority,
usePriority: false,
redis: re,
worker: call,
keyPrefix: keyPrefix,
@@ -109,16 +120,33 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
instanceId: u.String(),
keySeparator: "[:]",
timeout: op.timeout,
maxRetryCount: op.maxRetryCount,
maxRunCount: op.maxRunCount,
workerChan: make(chan struct{}, op.maxWorkers),
maxWorkers: op.maxWorkers,
}
// 初始化优先级
if wo.usePriority {
pri, err := priority.InitPriority(ctx,
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)
@@ -135,6 +163,7 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
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)
@@ -151,6 +180,7 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
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)
@@ -166,7 +196,15 @@ func InitOnce(ctx context.Context, re redis.UniversalClient, keyPrefix string, c
// 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()
}
@@ -251,27 +289,40 @@ func (l *Once) executeTasks() {
for {
if l.usePriority {
if !l.priority.IsLatest(l.ctx) {
time.Sleep(time.Second * 5)
continue
}
}
select {
case <-l.stopChan:
return
case <-l.ctx.Done():
return
default:
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 {
continue
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])
}()
}
}
@@ -297,40 +348,77 @@ func (l *Once) parseRedisKey(key string) (OnceTaskType, string, error) {
// @param uniTaskId string 任务唯一标识
// @param delayTime time.Duration 延迟时间
// @param attachData interface{} 附加数据
func (l *Once) Save(taskType OnceTaskType, taskId string, delayTime time.Duration, attachData interface{}) error {
return l.save(taskType, taskId, delayTime, attachData, 0)
func (l *Once) Save(ctx context.Context, taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any) error {
if delayTime < 0 {
delayTime = 0
}
execTime := time.Now().Add(delayTime)
return l.save(ctx, jobTypeOnce, taskType, taskId, []time.Time{execTime}, attachData, 0)
}
// 指定时间添加任务(覆盖)
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(taskType OnceTaskType, taskId string, delayTime time.Duration, attachData interface{}, retryCount int) error {
if delayTime <= 0 {
return fmt.Errorf("delay time must be positive")
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)
executeTime := time.Now().Add(delayTime)
ed := extendData{
Delay: delayTime,
TaskTimes: taskTimes,
Data: attachData,
RetryCount: retryCount,
RunCount: runCount,
JobType: jobType,
}
b, _ := json.Marshal(ed)
// 使用事务确保原子性
pipe := w.redis.TxPipeline()
dataExpire := delayTime + time.Minute*30
expiresTime := latestTime.Add(time.Minute * 30)
pipe.SetEX(w.ctx, w.keyPrefix+redisKey, b, dataExpire)
pipe.ZAdd(w.ctx, w.zsetKey, &redis.Z{
Score: float64(executeTime.UnixMilli()),
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 {
w.logger.Errorf(w.ctx, "save task failed:%w", err)
w.logger.Errorf(w.ctx, "save task failed:%v taskType:%v taskId:%v attachData:%v retryCount:%v", err, taskType, taskId, attachData, runCount)
return err
}
@@ -338,13 +426,28 @@ func (w *Once) save(taskType OnceTaskType, taskId string, delayTime time.Duratio
}
// 添加任务(不覆盖)
func (l *Once) Create(taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any) error {
return l.create(taskType, taskId, delayTime, attachData, 0)
func (l *Once) Create(ctx context.Context, taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any) error {
if delayTime <= 0 {
delayTime = 0
}
execTime := time.Now().Add(delayTime)
return l.create(ctx, jobTypeOnce, taskType, taskId, []time.Time{execTime}, attachData, 0)
}
func (l *Once) create(taskType OnceTaskType, taskId string, delayTime time.Duration, attachData any, retryCount int) error {
if delayTime <= 0 {
return fmt.Errorf("delay time must be positive")
// 指定时间执行(不覆盖)
func (l *Once) CreateByTime(ctx context.Context, taskType OnceTaskType, taskId string, executeTime time.Time, attachData any) error {
return l.create(ctx, jobTypeOnce, taskType, taskId, []time.Time{executeTime}, attachData, 0)
}
// 指定多个时间执行(不覆盖)
func (l *Once) CreateByList(ctx context.Context, taskType OnceTaskType, taskId string, taskTimes []time.Time, attachData any) error {
return l.create(ctx, jobTypeList, taskType, taskId, taskTimes, attachData, 0)
}
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)
@@ -352,16 +455,17 @@ func (l *Once) create(taskType OnceTaskType, taskId string, delayTime time.Durat
score, err := l.redis.ZScore(l.ctx, l.zsetKey, redisKey).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return l.Save(taskType, taskId, delayTime, attachData)
return l.save(ctx, jobType, taskType, taskId, taskTimes, attachData, runCount)
}
l.logger.Errorf(l.ctx, "redis.ZScore err:%v", err)
return err
}
if score > 0 {
return fmt.Errorf("task already exists")
l.logger.Errorf(l.ctx, "task exists taskType:%v taskId:%v attachData:%v runCount:%v", taskType, taskId, attachData, runCount)
return ErrTaskExists
}
return l.save(taskType, taskId, delayTime, attachData, retryCount)
return l.save(ctx, jobType, taskType, taskId, taskTimes, attachData, runCount)
}
// 删除任务
@@ -374,7 +478,7 @@ func (w *Once) Delete(taskType OnceTaskType, taskId string) error {
_, err := pipe.Exec(w.ctx)
if err != nil {
w.logger.Errorf(w.ctx, "delete task failed:%w", err)
w.logger.Errorf(w.ctx, "delete task failed:%v", err)
return err
}
@@ -418,6 +522,7 @@ func (l *Once) batchGetTasks() {
// 执行任务
func (l *Once) processTask(key string) {
begin := time.Now()
ctx, cancel := context.WithTimeout(l.ctx, l.timeout)
@@ -431,7 +536,7 @@ func (l *Once) processTask(key string) {
taskType, taskId, err := l.parseRedisKey(key)
if err != nil {
l.logger.Errorf(ctx, "processTask parseRedisKey:%w key:%s", err, key)
l.logger.Errorf(ctx, "processTask parseRedisKey:%v key:%s", err, key)
return
}
@@ -448,8 +553,8 @@ func (l *Once) processTask(key string) {
defer lock.Unlock()
// 上报执行情况
executeVal := fmt.Sprintf("%s|%s|%s|%s", key, l.instanceId, u.String(), begin.Format(time.RFC3339Nano))
l.redis.ZAdd(ctx, l.executeInfoKey, &redis.Z{
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,
})
@@ -479,37 +584,38 @@ func (l *Once) processTask(key string) {
// 删除任务
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:%w", err)
l.logger.Errorf(ctx, "processTask delete errprocessTask delete err:%v", err)
}
return
}
// 重新放入队列
if err := l.handleRetry(ctx, taskType, taskId, &ed, resp); err != nil {
l.logger.Errorf(ctx, "processTask handleRetry err:%w", err)
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.RetryCount++
if l.maxRetryCount > 0 && ed.RetryCount > l.maxRetryCount {
l.logger.Infof(ctx, "handleRetry task exceeded retry limit: %s %s %d", taskType, taskId, l.maxRetryCount)
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 resp.DelayTime > 0 {
ed.Delay = resp.DelayTime
if ed.JobType == jobTypeOnce {
ed.TaskTimes = []time.Time{time.Now().Add(resp.DelayTime)}
}
if resp.AttachData != nil {
ed.Data = resp.AttachData
}
l.logger.Infof(ctx, "handleRetry retrying task: %s:%s, retry count: %d",
taskType, taskId, ed.RetryCount)
taskType, taskId, ed.RunCount)
// 不覆盖的新建
return l.create(taskType, taskId, ed.Delay, ed.Data, ed.RetryCount)
return l.create(ctx, ed.JobType, taskType, taskId, ed.TaskTimes, ed.Data, ed.RunCount)
}
+106 -9
View File
@@ -3,33 +3,52 @@ package timerx
import (
"time"
"github.com/robfig/cron/v3"
"github.com/yuninks/timerx/logger"
)
type Options struct {
logger logger.Logger
location *time.Location
timeout time.Duration
usePriority bool
priorityVal int64
timeout time.Duration // 任务最长执行时间
priorityType priorityType // 策略类型 0.不使用 1.优先级 2.版本
priorityVal int64 // 策略优先级
priorityVersion string // 策略版本的集
batchSize int
maxRetryCount 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 {
// 默认使用Linux的定时任务兼容
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
return Options{
logger: logger.NewLogger(),
location: time.Local,
timeout: time.Hour,
usePriority: false,
timeout: time.Hour, //
priorityType: priorityTypeNone,
priorityVal: 0,
batchSize: 100,
maxRetryCount: 0,
maxRunCount: 0,
maxWorkers: 100,
cronParser: &parser,
}
}
type Option func(*Options)
// 返回带默认值的配置
func newOptions(opts ...Option) Options {
o := defaultOptions()
for _, opt := range opts {
@@ -38,6 +57,15 @@ func newOptions(opts ...Option) Options {
return o
}
// 返回空的配置
func newEmptyOptions(opts ...Option) Options {
o := Options{}
for _, opt := range opts {
opt(&o)
}
return o
}
// 设置日志
func WithLogger(log logger.Logger) Option {
return func(o *Options) {
@@ -62,11 +90,18 @@ func WithTimeout(d time.Duration) Option {
// 设置优先级
func WithPriority(priority int64) Option {
return func(o *Options) {
o.usePriority = true
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 {
@@ -81,6 +116,68 @@ func WithMaxRetryCount(count int) Option {
if count < 0 {
count = 0
}
o.maxRetryCount = count
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
}
}
+20 -1
View File
@@ -3,6 +3,7 @@ package priority
import (
"time"
"github.com/google/uuid"
"github.com/yuninks/timerx/logger"
)
@@ -10,15 +11,21 @@ type Options struct {
getInterval time.Duration // 查询周期
updateInterval time.Duration // 更新间隔
expireTime time.Duration // 有效时间
logger logger.Logger
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(),
}
}
@@ -49,3 +56,15 @@ func WithUpdateInterval(d time.Duration) Option {
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
}
}
+13 -10
View File
@@ -8,28 +8,30 @@ import (
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"github.com/yuninks/timerx/logger"
)
// 多版本场景判断当前是否最新版本
type Priority struct {
ctx context.Context
cancel context.CancelFunc
ctx context.Context // 上下文
cancel context.CancelFunc // 取消函数
priority int64 // 优先级
redis redis.UniversalClient
redisKey string
logger logger.Logger
expireTime time.Duration
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
isLatest bool // 是否是最新版本
latestMux sync.RWMutex // 最新版本锁
instanceId string // 实例ID
}
func InitPriority(ctx context.Context, re redis.UniversalClient, keyPrefix string, priority int64, opts ...Option) (*Priority, error) {
@@ -48,10 +50,11 @@ func InitPriority(ctx context.Context, re redis.UniversalClient, keyPrefix strin
priority: priority,
redis: re,
logger: conf.logger,
redisKey: "timer:priority_" + keyPrefix,
redisKey: "timer:priority_" + conf.source + keyPrefix,
expireTime: conf.expireTime,
setInterval: conf.updateInterval,
getInterval: conf.getInterval,
instanceId: conf.instanceId,
}
pro.startDaemon()
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
+12 -1
View File
@@ -6,7 +6,6 @@ import (
"github.com/yuninks/timerx/priority"
)
func TestVersionToPriority(t *testing.T) {
tests := []struct {
name string
@@ -20,6 +19,18 @@ func TestVersionToPriority(t *testing.T) {
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",
+8 -15
View File
@@ -1,36 +1,29 @@
# 功能支持
1. 支持本地任务
2. 支持集群任务
3. 支持单次任务
1. [X] 支持本地任务
2. [X] 支持集群任务
3. [X] 支持单次任务
# 功能说明
# 功能实现
1. 集群间任务调度和任务的唯一依赖于redis进行实现
# 缺陷
1. 针对月的任务,需要注意日期有效性,且在月末的最后一天,需要考虑月末的最后一天的下一个任务执行时间
2. 集群部署时,存在新旧的代码混合问题,任务调度可能存在问题(需要根据实际需要进行版本上线/下线操作)
3. 主从切换时也要做到平滑上下线
1. 集群部署时,存在新旧的代码混合问题,任务调度可能存在问题(需要根据实际需要进行版本上线/下线操作)
## 方案一
1. 启动的时候定时向redis注册任务项
2. 每次计算执行时间的时候根据注册的任务项进行任务计算
3. 注册任务项需要有下线机制,避免能运行它的节点下线了它还被执行
现在有根据要求根据系统时间整点运行任务的要求,这个比简单的定时重复更复杂,因为不但要按时执行,并且不能重复执行,需要全局记录任务执行的状态,由于任务的间隔时间不确定,这个任务执行状态的保存周期也是有变化的
# 待实现
- [ ] 允许执行完重置任务倒计时
+135 -94
View File
@@ -4,14 +4,14 @@ package timerx
import (
"context"
"errors"
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
"time"
uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/robfig/cron/v3"
"github.com/yuninks/timerx/logger"
)
@@ -19,9 +19,6 @@ import (
// 1. 这个定时器的作用范围是本机
// 2. 适用简单的时间间隔定时任务
// 避免执行重复
var singleHasRun sync.Map
type Single struct {
ctx context.Context
cancel context.CancelFunc
@@ -30,10 +27,12 @@ type Single struct {
nextTime time.Time
nextTimeMux sync.RWMutex
wg sync.WaitGroup
workerList sync.Map
timerIndex int64
stopChan chan struct{}
hasRun sync.Map
workerList sync.Map // 任务列表,key为taskIdvalue为worker
timerIndex int64 // 任务索引,用于生成taskId
stopChan chan struct{} // 停止信号
hasRun sync.Map // 记录已经执行的任务,key为taskId,value为执行时间
timeout time.Duration // 单次任务超时时间
cronParser *cron.Parser // cron表达式解析器
}
// 定时器类
@@ -50,39 +49,41 @@ func InitSingle(ctx context.Context, opts ...Option) *Single {
location: op.location,
nextTime: time.Now(),
stopChan: make(chan struct{}),
timeout: op.timeout,
cronParser: op.cronParser,
}
sin.wg.Add(1)
go sin.timerLoop(ctx)
sin.wg.Add(1)
go sin.cleanupLoop(ctx)
sin.startDaemon()
return sin
}
// 停止所有定时任务
func (s *Single) Stop() {
if s.cancel != nil {
s.cancel()
}
close(s.stopChan)
s.wg.Wait()
func (l *Single) startDaemon() {
// 清理所有资源
s.workerList.Range(func(k, v interface{}) bool {
if timer, ok := v.(timerStr); ok {
close(timer.CanRunning)
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()
}
s.workerList.Delete(k)
return true
})
l.wg.Wait()
l.logger.Infof(l.ctx, "timer single: stopped")
}
// 获取任务数量
func (s *Single) TaskCount() int {
func (l *Single) TaskCount() int {
count := 0
s.workerList.Range(func(k, v interface{}) bool {
l.workerList.Range(func(k, v interface{}) bool {
count++
return true
})
@@ -94,8 +95,8 @@ func (l *Single) MaxIndex() int64 {
}
// 定时器主循环
func (s *Single) timerLoop(ctx context.Context) {
defer s.wg.Done()
func (l *Single) timerLoop() {
defer l.wg.Done()
ticker := time.NewTicker(100 * time.Millisecond) // 提高精度到100ms
defer ticker.Stop()
@@ -103,28 +104,28 @@ func (s *Single) timerLoop(ctx context.Context) {
for {
select {
case t := <-ticker.C:
s.nextTimeMux.RLock()
nextTime := s.nextTime
s.nextTimeMux.RUnlock()
l.nextTimeMux.RLock()
nextTime := l.nextTime
l.nextTimeMux.RUnlock()
if t.Before(nextTime) {
continue
}
s.iterator(ctx)
l.iterator(l.ctx)
case <-ctx.Done():
s.logger.Infof(ctx, "timer: context cancelled, stopping timer loop")
case <-l.ctx.Done():
l.logger.Infof(l.ctx, "timer: context cancelled, stopping timer loop")
return
case <-s.stopChan:
s.logger.Infof(ctx, "timer: received stop signal, stopping timer loop")
case <-l.stopChan:
l.logger.Infof(l.ctx, "timer: received stop signal, stopping timer loop")
return
}
}
}
// 清理循环
func (s *Single) cleanupLoop(ctx context.Context) {
func (s *Single) cleanupLoop() {
defer s.wg.Done()
ticker := time.NewTicker(time.Minute)
@@ -136,7 +137,7 @@ func (s *Single) cleanupLoop(ctx context.Context) {
now := time.Now()
cleanupTime := now.Add(-2 * time.Minute) // 清理2分钟前的记录
s.hasRun.Range(func(k, v interface{}) bool {
s.hasRun.Range(func(k, v any) bool {
t, ok := v.(time.Time)
if !ok || t.Before(cleanupTime) {
s.hasRun.Delete(k)
@@ -144,11 +145,11 @@ func (s *Single) cleanupLoop(ctx context.Context) {
return true
})
case <-ctx.Done():
s.logger.Infof(ctx, "timer: context cancelled, stopping cleanup loop")
case <-s.ctx.Done():
s.logger.Infof(s.ctx, "timer: context cancelled, stopping cleanup loop")
return
case <-s.stopChan:
s.logger.Infof(ctx, "timer: received stop signal, stopping cleanup loop")
s.logger.Infof(s.ctx, "timer: received stop signal, stopping cleanup loop")
return
}
}
@@ -164,14 +165,14 @@ func (s *Single) cleanupLoop(ctx context.Context) {
// @param callback 回调函数
// @param extendData 扩展数据
// @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{}) (int64, 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().In(c.location)
// nowTime := time.Now().In(c.location)
jobData := JobData{
JobType: JobTypeEveryMonth,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
Day: day,
Hour: hour,
Minute: minute,
@@ -188,13 +189,13 @@ func (c *Single) AddMonth(ctx context.Context, taskId string, day int, hour int,
// @param hour int 小时
// @param minute 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{}) (int64, error) {
nowTime := time.Now().In(c.location)
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().In(c.location)
jobData := JobData{
JobType: JobTypeEveryWeek,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
Weekday: week,
Hour: hour,
Minute: minute,
@@ -205,13 +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{}) (int64, error) {
nowTime := time.Now().In(c.location)
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().In(c.location)
jobData := JobData{
JobType: JobTypeEveryDay,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
Hour: hour,
Minute: minute,
Second: second,
@@ -221,13 +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{}) (int64, error) {
nowTime := time.Now().In(c.location)
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().In(c.location)
jobData := JobData{
JobType: JobTypeEveryHour,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
Minute: minute,
Second: second,
}
@@ -236,13 +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{}) (int64, error) {
nowTime := time.Now().In(c.location)
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().In(c.location)
jobData := JobData{
JobType: JobTypeEveryMinute,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
Second: second,
}
@@ -250,24 +251,56 @@ 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{}) (int64, error) {
nowTime := time.Now().In(c.location)
func (c *Single) EverySpace(ctx context.Context, taskId string, spaceTime time.Duration, callback func(ctx context.Context, extendData interface{}) error, extendData interface{}) (int64, error) {
if spaceTime < 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{
JobType: JobTypeInterval,
TaskId: taskId,
CreateTime: nowTime,
// CreateTime: nowTime,
BaseTime: zeroTime,
IntervalTime: spaceTime,
}
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 call 回调函数
@@ -338,14 +371,21 @@ func (s *Single) updateNextTimeIfEarlier(candidate time.Time) {
// 删除定时器
func (l *Single) Del(index int64) {
if val, ok := l.workerList.Load(index); ok {
if timer, ok := val.(timerStr); ok {
close(timer.CanRunning)
}
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 (l *Single) iterator(ctx context.Context) {
// 当前时间
@@ -393,6 +433,15 @@ func (l *Single) iterator(ctx context.Context) {
// 执行任务
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() {
@@ -402,51 +451,43 @@ func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime tim
}
}()
// 检查任务是否已执行
taskKey := fmt.Sprintf("%s:%d", timer.TaskId, originTime.UnixNano())
if _, loaded := s.hasRun.LoadOrStore(taskKey, time.Now()); loaded {
s.logger.Errorf(ctx, "timer: 任务已执行,跳过本次执行 %s", timer.TaskId)
return
}
// 创建带追踪ID的上下文
traceCtx := context.WithValue(ctx, "trace_id", uuid.NewV4().String())
traceCtx, cancel := context.WithTimeout(traceCtx, 30*time.Second) // 设置执行超时
defer cancel()
// 执行回调
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
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() {
if err := recover(); err != nil {
l.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()))
}
}()
nuiKey := timeStr.TaskId + originTime.String()
// timeStr.TaskId
_, loaded := singleHasRun.LoadOrStore(nuiKey, time.Now())
if loaded {
// 已经存在,说明已经执行过了
l.logger.Errorf(ctx, "timer: 任务已执行,跳过本次执行 %s", nuiKey)
return nil
err := timeStr.Callback(ctx, timeStr.ExtendData)
if err != nil {
l.logger.Errorf(ctx, "timer Single call back %s, err: %v", timeStr.TaskId, err)
return err
}
ctx = context.WithValue(ctx, "trace_id", uuid.NewV4().String())
return timeStr.Callback(ctx, timeStr.ExtendData)
return nil
}
// 更新下次执行时间
+75 -35
View File
@@ -3,6 +3,7 @@ package timerx_test
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"testing"
@@ -66,7 +67,7 @@ func TestSingleTimer_Basic(t *testing.T) {
}
// 添加间隔任务
index, err := timer.AddSpace(ctx, "test-task", 100*time.Millisecond, taskFunc, 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())
@@ -89,17 +90,17 @@ func TestSingleTimer_InvalidParams(t *testing.T) {
validFunc := func(ctx context.Context, data interface{}) error { return nil }
// 测试空taskId
_, err := timer.AddSpace(ctx, "", time.Second, validFunc, nil)
_, err := timer.EverySpace(ctx, "", time.Second, validFunc, nil)
assert.Error(t, err)
// 测试nil回调函数
_, err = timer.AddSpace(ctx, "test", time.Second, nil, nil)
_, err = timer.EverySpace(ctx, "test", time.Second, nil, nil)
assert.Error(t, err)
// 测试无效间隔时间
_, err = timer.AddSpace(ctx, "test", -time.Second, validFunc, nil)
_, err = timer.EverySpace(ctx, "test", -time.Second, validFunc, nil)
assert.Error(t, err)
_, err = timer.AddSpace(ctx, "test", 0, validFunc, nil)
_, err = timer.EverySpace(ctx, "test", 0, validFunc, nil)
assert.Error(t, err)
}
@@ -119,22 +120,22 @@ func TestSingleTimer_Deduplication(t *testing.T) {
}
// 添加短间隔任务
_, err := timer.AddSpace(ctx, "dedup-test", 50*time.Millisecond, taskFunc, nil)
_, err := timer.EverySpace(ctx, "dedup-test", 50*time.Millisecond, taskFunc, nil)
assert.NoError(t, err)
// 等待一段时间,检查去重是否生效
time.Sleep(200 * time.Millisecond)
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)
// 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.Warns, "timer: 任务执行,跳过本次执行 dedup-test")
assert.Contains(t, mockLogger.Infos, "timer: 任务正在执行,跳过本次 dedup-test")
}
// 测试并发安全
@@ -157,7 +158,7 @@ func TestSingleTimer_Concurrency(t *testing.T) {
return nil
}
_, err := timer.AddSpace(ctx, fmt.Sprintf("concurrent-%d", i),
_, err := timer.EverySpace(ctx, fmt.Sprintf("concurrent-%d", i),
time.Duration(i+1)*100*time.Millisecond, taskFunc, nil)
assert.NoError(t, err)
}(i)
@@ -190,11 +191,12 @@ func TestSingleTimer_Timeout(t *testing.T) {
ctx := context.Background()
mockLogger := &MockLogger{}
timer := timerx.InitSingle(ctx, timerx.WithLogger(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():
@@ -203,13 +205,23 @@ func TestSingleTimer_Timeout(t *testing.T) {
return nil
}
_, err := timer.AddSpace(ctx, "timeout-test", 100*time.Millisecond, longTask, nil)
_, err := timer.EverySpace(ctx, "timeout-test", 100*time.Millisecond, longTask, nil)
assert.NoError(t, err)
time.Sleep(500 * time.Millisecond)
time.Sleep(time.Second * 5)
// 检查是否有超时相关的错误日志
assert.Contains(t, mockLogger.Errors, "context deadline exceeded")
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恢复
@@ -224,13 +236,24 @@ func TestSingleTimer_PanicRecovery(t *testing.T) {
panic("test panic")
}
_, err := timer.AddSpace(ctx, "panic-test", 100*time.Millisecond, panicTask, nil)
_, err := timer.EverySpace(ctx, "panic-test", 100*time.Millisecond, panicTask, nil)
assert.NoError(t, err)
time.Sleep(200 * time.Millisecond)
// 检查是否有panic恢复日志
assert.Contains(t, mockLogger.Errors, "timer: 回调任务panic err")
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)
}
// 测试不同时间类型的任务
@@ -251,7 +274,7 @@ func TestSingleTimer_DifferentJobTypes(t *testing.T) {
now := time.Now().UTC()
// 月任务(下个月同一天)
_, err := timer.AddMonth(ctx, "month-job", now.Day(), now.Hour(), now.Minute(), now.Second()+1,
_, 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
@@ -259,7 +282,7 @@ func TestSingleTimer_DifferentJobTypes(t *testing.T) {
assert.NoError(t, err)
// 周任务(下周同一天)
_, err = timer.AddWeek(ctx, "week-job", now.Weekday(), now.Hour(), now.Minute(), now.Second()+1,
_, 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
@@ -267,19 +290,19 @@ func TestSingleTimer_DifferentJobTypes(t *testing.T) {
assert.NoError(t, err)
// 间隔任务(立即执行)
_, err = timer.AddSpace(ctx, "space-job", 100*time.Millisecond,
_, 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(300 * time.Millisecond)
time.Sleep(time.Second)
// 只有间隔任务应该执行
assert.Equal(t, int32(1), atomic.LoadInt32(&counts.space))
assert.Equal(t, int32(0), atomic.LoadInt32(&counts.month))
assert.Equal(t, int32(0), atomic.LoadInt32(&counts.week))
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))
}
// 测试上下文取消
@@ -290,7 +313,7 @@ func TestSingleTimer_ContextCancellation(t *testing.T) {
timer := timerx.InitSingle(ctx, timerx.WithLogger(mockLogger))
var executionCount int32
_, err := timer.AddSpace(ctx, "cancel-test", 100*time.Millisecond,
_, err := timer.EverySpace(ctx, "cancel-test", 100*time.Millisecond,
func(ctx context.Context, data interface{}) error {
atomic.AddInt32(&executionCount, 1)
return nil
@@ -327,8 +350,9 @@ func TestSingleTimer_ExtendData(t *testing.T) {
testData := &TestData{Message: "hello", Count: 42}
var receivedData *TestData
_, err := timer.AddSpace(ctx, "data-test", 100*time.Millisecond,
_, 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)
}
@@ -336,7 +360,9 @@ func TestSingleTimer_ExtendData(t *testing.T) {
}, testData)
assert.NoError(t, err)
time.Sleep(150 * time.Millisecond)
time.Sleep(time.Second)
t.Logf("receivedData: %+v", receivedData)
assert.NotNil(t, receivedData)
assert.Equal(t, "hello", receivedData.Message)
@@ -352,14 +378,14 @@ func TestSingleTimer_TaskDeletion(t *testing.T) {
var executionCount int32
// 添加多个任务
index1, err := timer.AddSpace(ctx, "task-1", 100*time.Millisecond,
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.AddSpace(ctx, "task-2", 100*time.Millisecond,
index2, err := timer.EverySpace(ctx, "task-2", 100*time.Millisecond,
func(ctx context.Context, data interface{}) error {
atomic.AddInt32(&executionCount, 1)
return nil
@@ -392,11 +418,15 @@ func TestGetNextTime2(t *testing.T) {
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, now.Add(time.Minute), *nextTime, time.Second)
assert.WithinDuration(t, tt.Add(time.Minute), *nextTime, time.Second)
}
// 基准测试
@@ -407,7 +437,7 @@ func BenchmarkSingleTimer_AddAndExecute(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
timer.AddSpace(ctx, fmt.Sprintf("bench-%d", i), time.Millisecond,
timer.EverySpace(ctx, fmt.Sprintf("bench-%d", i), time.Millisecond,
func(ctx context.Context, data interface{}) error {
return nil
}, nil)
@@ -423,7 +453,7 @@ func TestSingleTimer_Logging(t *testing.T) {
defer timer.Stop()
// 添加会panic的任务
_, err := timer.AddSpace(ctx, "logging-test", 100*time.Millisecond,
_, err := timer.EverySpace(ctx, "logging-test", 100*time.Millisecond,
func(ctx context.Context, data interface{}) error {
panic("test panic for logging")
}, nil)
@@ -433,7 +463,16 @@ func TestSingleTimer_Logging(t *testing.T) {
// 检查日志记录
assert.NotEmpty(t, mockLogger.Errors)
assert.Contains(t, mockLogger.Errors[0], "timer: 回调任务panic err")
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)
}
// 测试时区处理
@@ -455,14 +494,15 @@ func TestSingleTimer_Timezone(t *testing.T) {
// now := time.Now().In(loc)
// 添加下一秒执行的任务
_, err := timer.AddSpace(ctx, "tz-test", time.Second,
_, 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(1500 * time.Millisecond)
time.Sleep(5 * time.Second)
assert.True(t, executed)
})
}
+8 -4
View File
@@ -3,13 +3,15 @@ package timerx
import (
"context"
"time"
"github.com/robfig/cron/v3"
)
type timerStr struct {
Callback func(ctx context.Context, extendData interface{}) error // 需要回调的方法
Callback func(ctx context.Context, extendData any) error // 需要回调的方法
CanRunning chan (struct{}) // 是否允许执行(only single)
TaskId string // 任务ID 全局唯一键(only cluster)
ExtendData interface{} // 附加参数
TaskId string // 任务ID 全局唯一键
ExtendData any // 附加参数
JobData *JobData // 任务时间数据
}
@@ -23,6 +25,7 @@ const (
JobTypeEveryMinute JobType = "every_minute" // 每分钟
JobTypeEverySecond JobType = "every_second" // 每秒
JobTypeInterval JobType = "interval" // 指定时间间隔
JobTypeCron JobType = "cron" // cron表达式
)
type JobData struct {
@@ -30,7 +33,6 @@ type JobData struct {
TaskId string // 任务ID 全局唯一键(only cluster)
NextTime time.Time // 下次执行时间
BaseTime time.Time // 基准时间(间隔的基准时间)
CreateTime time.Time // 任务创建时间
IntervalTime time.Duration // 任务间隔时间
Month time.Month // 每年的第几个月
Weekday time.Weekday // 每周的周几
@@ -38,6 +40,8 @@ type JobData struct {
Hour int // 每天的第几个小时
Minute int // 每小时的第几分钟
Second int // 每分钟的第几秒
CronExpression string // cron表达式
CronSchedule *cron.Schedule // cron表达式解析后的数据
}
// 定义各个回调函数