Files
timerx/option.go
T

87 lines
1.4 KiB
Go
Raw Normal View History

2024-04-04 10:58:57 +08:00
package timerx
2025-07-24 17:13:17 +08:00
import (
"time"
"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
timeout time.Duration
usePriority bool
priorityVal int64
batchSize int
maxRetryCount int
2024-04-04 10:58:57 +08:00
}
func defaultOptions() Options {
return Options{
2025-09-24 14:50:30 +08:00
logger: logger.NewLogger(),
location: time.Local,
timeout: time.Hour,
usePriority: false,
priorityVal: 0,
batchSize: 100,
2025-09-24 17:26:33 +08:00
maxRetryCount: 0,
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
}
}