2024-04-04 10:58:57 +08:00
|
|
|
package timerx
|
|
|
|
|
|
2025-07-24 17:13:17 +08:00
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-10-05 16:30:41 +08:00
|
|
|
"github.com/robfig/cron/v3"
|
2025-07-24 17:13:17 +08:00
|
|
|
"github.com/yuninks/timerx/logger"
|
|
|
|
|
)
|
2024-05-30 11:02:44 +08:00
|
|
|
|
2024-04-04 10:58:57 +08:00
|
|
|
type Options struct {
|
2025-09-24 14:50:30 +08:00
|
|
|
logger logger.Logger
|
|
|
|
|
location *time.Location
|
2025-09-28 19:49:23 +08:00
|
|
|
timeout time.Duration // 任务最长执行时间
|
2025-09-24 14:50:30 +08:00
|
|
|
usePriority bool
|
|
|
|
|
priorityVal int64
|
|
|
|
|
batchSize int
|
|
|
|
|
maxRetryCount int
|
2025-10-05 16:30:41 +08:00
|
|
|
cronParser *cron.Parser // cron表达式解析器
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defaultOptions() Options {
|
2025-10-05 16:30:41 +08:00
|
|
|
|
|
|
|
|
parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
|
|
|
|
|
|
2024-04-04 10:58:57 +08:00
|
|
|
return Options{
|
2025-09-24 14:50:30 +08:00
|
|
|
logger: logger.NewLogger(),
|
|
|
|
|
location: time.Local,
|
2025-09-28 19:49:23 +08:00
|
|
|
timeout: time.Hour, //
|
2025-09-24 14:50:30 +08:00
|
|
|
usePriority: false,
|
|
|
|
|
priorityVal: 0,
|
|
|
|
|
batchSize: 100,
|
2025-09-24 17:26:33 +08:00
|
|
|
maxRetryCount: 0,
|
2025-10-05 16:30:41 +08:00
|
|
|
cronParser: &parser,
|
2024-04-04 10:58:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
|
|
func newOptions(opts ...Option) Options {
|
|
|
|
|
o := defaultOptions()
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(&o)
|
|
|
|
|
}
|
|
|
|
|
return o
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 20:26:13 +08:00
|
|
|
// 设置日志
|
2025-09-24 14:50:30 +08:00
|
|
|
func WithLogger(log logger.Logger) Option {
|
2024-04-04 10:58:57 +08:00
|
|
|
return func(o *Options) {
|
|
|
|
|
o.logger = log
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-07 20:26:13 +08:00
|
|
|
|
|
|
|
|
// 设定时区
|
2025-09-24 14:50:30 +08:00
|
|
|
func WithLocation(zone *time.Location) Option {
|
2024-05-07 20:26:13 +08:00
|
|
|
return func(o *Options) {
|
2024-05-30 11:02:44 +08:00
|
|
|
o.location = zone
|
2024-05-07 20:26:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-22 15:34:49 +08:00
|
|
|
|
|
|
|
|
// 设置任务最长执行时间
|
2025-09-24 14:50:30 +08:00
|
|
|
func WithTimeout(d time.Duration) Option {
|
2024-06-22 15:34:49 +08:00
|
|
|
return func(o *Options) {
|
|
|
|
|
o.timeout = d
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-11 15:12:08 +08:00
|
|
|
|
|
|
|
|
// 设置优先级
|
2025-09-24 14:50:30 +08:00
|
|
|
func WithPriority(priority int64) Option {
|
2025-06-11 15:12:08 +08:00
|
|
|
return func(o *Options) {
|
2025-08-27 15:52:09 +08:00
|
|
|
o.usePriority = true
|
|
|
|
|
o.priorityVal = priority
|
2025-06-11 15:12:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-24 14:50:30 +08:00
|
|
|
|
|
|
|
|
func WithBatchSize(size int) Option {
|
|
|
|
|
return func(o *Options) {
|
|
|
|
|
if size <= 1 {
|
|
|
|
|
size = 1
|
|
|
|
|
}
|
|
|
|
|
o.batchSize = size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithMaxRetryCount(count int) Option {
|
|
|
|
|
return func(o *Options) {
|
2025-09-24 17:26:33 +08:00
|
|
|
if count < 0 {
|
|
|
|
|
count = 0
|
2025-09-24 14:50:30 +08:00
|
|
|
}
|
|
|
|
|
o.maxRetryCount = count
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-05 16:30:41 +08:00
|
|
|
|
|
|
|
|
// 添加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
|
|
|
|
|
}
|
|
|
|
|
}
|