-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
145 lines (128 loc) · 3.21 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package jobq
import (
"time"
)
// JobOptions contains all job options
type JobOptions struct {
timeoutEnabled bool
timeout time.Duration
retries int
requeuing bool
workerPoolSize int
ttl time.Duration
}
func (opts JobOptions) with(args ...JobOption) (JobOptions, error) {
for _, opt := range args {
if err := opt(&opts); err != nil {
return opts, err
}
}
return opts, nil
}
var defaultJobOptions = JobOptions{
timeoutEnabled: true,
timeout: time.Second * 5,
retries: 5,
requeuing: true,
workerPoolSize: 1,
ttl: time.Second * 20,
}
// JobOption configures job
type JobOption func(*JobOptions) error
// WithJobTimeout sets timeout duration that will be used
// if job task fails and has no more retries(default: 5s)
func WithJobTimeout(timeout time.Duration) JobOption {
return func(opts *JobOptions) error {
if err := validateTimeout(timeout); err != nil {
return err
}
opts.timeoutEnabled = true
opts.timeout = timeout
return nil
}
}
// WithJobTimeoutDisabled disables timeout duration
func WithJobTimeoutDisabled() JobOption {
return func(opts *JobOptions) error {
opts.timeoutEnabled = false
return nil
}
}
// WithJobRequeueRetries number of retries that will be set
// if job task is requeued (default: 5)
func WithJobRequeueRetries(retries int) JobOption {
return func(opts *JobOptions) error {
if err := validateRetries(retries); err != nil {
return err
}
opts.retries = retries
return nil
}
}
// WithJobRequeuing enables or disables requeuing if
// job task fails (default: true)
func WithJobRequeuing(enabled bool) JobOption {
return func(opts *JobOptions) error {
opts.requeuing = enabled
return nil
}
}
// WithJobWorkerPoolSize sets how many workers should
// handle this job(default: 1)
func WithJobWorkerPoolSize(size int) JobOption {
return func(opts *JobOptions) error {
if err := validatePoolSize(size); err != nil {
return err
}
opts.workerPoolSize = size
return nil
}
}
// TaskOptions contains all task options
type TaskOptions struct {
startAt time.Time
startAtEnabled bool
retries int
}
var defaultTaskOptions = TaskOptions{
startAtEnabled: false,
retries: 5,
}
// TaskOption configres task
type TaskOption func(*TaskOptions) error
func (opts TaskOptions) with(args ...TaskOption) (TaskOptions, error) {
for _, opt := range args {
if err := opt(&opts); err != nil {
return opts, err
}
}
return opts, nil
}
// WithTaskStartTime enables and sets time when task should be executed (default: disabled)
func WithTaskStartTime(t time.Time) TaskOption {
return func(opts *TaskOptions) error {
if err := validateStartTime(t); err != nil {
return err
}
opts.startAt = t
opts.startAtEnabled = true
return nil
}
}
// WithTaskStartTimeDisabled disables task start time
func WithTaskStartTimeDisabled() TaskOption {
return func(opts *TaskOptions) error {
opts.startAtEnabled = false
return nil
}
}
// WithTaskRetries sets initial retry number for failed tasks (default: 5)
func WithTaskRetries(retries int) TaskOption {
return func(opts *TaskOptions) error {
if err := validateRetries(retries); err != nil {
return err
}
opts.retries = retries
return nil
}
}