-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
114 lines (92 loc) · 2.38 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package goworker
import "github.com/google/uuid"
const (
NoCronExpress = ""
)
type WorkerModel int
const (
Local WorkerModel = 1
Cluster WorkerModel = 2
)
type workerOptions struct {
ID string
ServiceName string
// Retry 重试策略
Retry Retrier
Model WorkerModel
// CronSchedule cron表达式
//
// https://en.wikipedia.org/wiki/Cron
// ┌───────────── minute (0 - 59)
// │ ┌───────────── hour (0 - 23)
// │ │ ┌───────────── day of the month (1 - 31)
// │ │ │ ┌───────────── month (1 - 12)
// │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
// │ │ │ │ │
// │ │ │ │ │
// │ │ │ │ │
// * * * * *
CronSchedule string
Params string
ShardingTotalCount int
}
type WorkerOption interface {
apply(*workerOptions)
}
type funcWorkerOption struct {
applyFunc func(*workerOptions)
}
func (fdo *funcWorkerOption) apply(do *workerOptions) {
fdo.applyFunc(do)
}
func newFuncWorkerOption(f func(*workerOptions)) *funcWorkerOption {
return &funcWorkerOption{
applyFunc: f,
}
}
func defaultWorkerOptions() *workerOptions {
wo := &workerOptions{
ID: uuid.New().String(),
Retry: newRetrier(GetDefaultRetryPolicy()),
Model: Local,
}
return wo
}
//WithRetryPolicy 自定义重试规则
func WithRetryPolicy(rp RetryPolicy) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
if rp != nil {
o.Retry = newRetrier(rp)
}
})
}
//WithCronSchedule cron表达式
func WithCronSchedule(cron string) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
o.CronSchedule = cron
})
}
//WithModel worker执行模式
func WithModel(model WorkerModel) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
o.Model = model
})
}
//WithModel worker执行模式
func WithServiceName(sn string) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
o.ServiceName = sn
})
}
//WithParams handle执行时可传递的参数
func WithParams(params string) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
o.Params = params
})
}
//WithShardingTotalCount worker分片数
func WithShardingTotalCount(count int) WorkerOption {
return newFuncWorkerOption(func(o *workerOptions) {
o.ShardingTotalCount = count
})
}