Files

49 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-11-27 22:37:33 +08:00
package timerx
2023-08-27 23:39:58 +08:00
2023-11-11 17:26:46 +08:00
import (
"context"
"time"
2025-10-05 16:30:41 +08:00
"github.com/robfig/cron/v3"
2023-11-11 17:26:46 +08:00
)
2023-08-27 23:39:58 +08:00
type timerStr struct {
2025-10-02 17:54:38 +08:00
Callback func(ctx context.Context, extendData any) error // 需要回调的方法
CanRunning chan (struct{}) // 是否允许执行(only single)
TaskId string // 任务ID 全局唯一键
ExtendData any // 附加参数
JobData *JobData // 任务时间数据
2023-08-27 23:39:58 +08:00
}
2024-04-04 10:58:57 +08:00
type JobType string
2023-11-27 22:37:33 +08:00
2024-04-04 10:58:57 +08:00
const (
2024-05-20 09:35:12 +08:00
JobTypeEveryMonth JobType = "every_month" // 每月
JobTypeEveryWeek JobType = "every_week" // 每周
JobTypeEveryDay JobType = "every_day" // 每天
JobTypeEveryHour JobType = "every_hour" // 每小时
JobTypeEveryMinute JobType = "every_minute" // 每分钟
JobTypeEverySecond JobType = "every_second" // 每秒
JobTypeInterval JobType = "interval" // 指定时间间隔
2025-10-05 16:30:41 +08:00
JobTypeCron JobType = "cron" // cron表达式
2024-04-04 10:58:57 +08:00
)
2023-11-11 17:26:46 +08:00
2024-04-04 10:58:57 +08:00
type JobData struct {
2025-10-05 16:30:41 +08:00
JobType JobType // 任务类型
TaskId string // 任务ID 全局唯一键(only cluster)
NextTime time.Time // 下次执行时间
BaseTime time.Time // 基准时间(间隔的基准时间)
IntervalTime time.Duration // 任务间隔时间
Month time.Month // 每年的第几个月
Weekday time.Weekday // 每周的周几
Day int // 每月的第几天
Hour int // 每天的第几个小时
Minute int // 每小时的第几分钟
Second int // 每分钟的第几秒
CronExpression string // cron表达式
CronSchedule *cron.Schedule // cron表达式解析后的数据
2024-04-04 10:58:57 +08:00
}
2023-11-11 17:26:46 +08:00
// 定义各个回调函数
2024-05-31 13:05:51 +08:00
// type callback func(ctx context.Context, extendData interface{}) error