-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
83 lines (66 loc) · 1.66 KB
/
Copy pathoptions.go
File metadata and controls
83 lines (66 loc) · 1.66 KB
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
package relay
import "time"
const (
defaultQueue = "default"
defaultMaxRetry = 25
defaultTimeout = 30 * time.Minute
)
type Option interface {
apply(*taskOptions)
}
type taskOptions struct {
queue string
taskID string
maxRetry int
timeout time.Duration
deadline time.Time
processAt time.Time
uniqueTTL time.Duration
retention time.Duration
}
func defaultOptions() taskOptions {
return taskOptions{
queue: defaultQueue,
maxRetry: defaultMaxRetry,
timeout: defaultTimeout,
processAt: time.Now(),
}
}
func composeOptions(base taskOptions, opts ...Option) taskOptions {
for _, o := range opts {
o.apply(&base)
}
return base
}
type optionFunc func(*taskOptions)
func (f optionFunc) apply(o *taskOptions) { f(o) }
func Queue(name string) Option {
return optionFunc(func(o *taskOptions) { o.queue = name })
}
func TaskID(id string) Option {
return optionFunc(func(o *taskOptions) { o.taskID = id })
}
func MaxRetry(n int) Option {
if n < 0 {
n = 0
}
return optionFunc(func(o *taskOptions) { o.maxRetry = n })
}
func Timeout(d time.Duration) Option {
return optionFunc(func(o *taskOptions) { o.timeout = d })
}
func Deadline(t time.Time) Option {
return optionFunc(func(o *taskOptions) { o.deadline = t })
}
func ProcessAt(t time.Time) Option {
return optionFunc(func(o *taskOptions) { o.processAt = t })
}
func ProcessIn(d time.Duration) Option {
return optionFunc(func(o *taskOptions) { o.processAt = time.Now().Add(d) })
}
func Unique(ttl time.Duration) Option {
return optionFunc(func(o *taskOptions) { o.uniqueTTL = ttl })
}
func Retention(d time.Duration) Option {
return optionFunc(func(o *taskOptions) { o.retention = d })
}