-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathworker_option.go
281 lines (244 loc) · 9.72 KB
/
worker_option.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package gue
import (
"context"
"time"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"github.com/vgarvardt/gue/v5/adapter"
)
// WorkerOption defines a type that allows to set worker properties during the build-time.
type WorkerOption func(*Worker)
// WorkerPoolOption defines a type that allows to set worker pool properties during the build-time.
type WorkerPoolOption func(pool *WorkerPool)
// WithWorkerPollInterval overrides default poll interval with the given value.
// Poll interval is the "sleep" duration if there were no jobs found in the DB.
func WithWorkerPollInterval(d time.Duration) WorkerOption {
return func(w *Worker) {
w.interval = d
}
}
// WithWorkerQueue overrides default worker queue name with the given value.
func WithWorkerQueue(queue string) WorkerOption {
return func(w *Worker) {
w.queue = queue
}
}
// WithWorkerID sets worker ID for easier identification in logs
func WithWorkerID(id string) WorkerOption {
return func(w *Worker) {
w.id = id
}
}
// WithWorkerLogger sets Logger implementation to worker
func WithWorkerLogger(logger adapter.Logger) WorkerOption {
return func(w *Worker) {
w.logger = logger
}
}
// WithWorkerTracer sets trace.Tracer instance to the worker.
func WithWorkerTracer(tracer trace.Tracer) WorkerOption {
return func(w *Worker) {
w.tracer = tracer
}
}
// WithWorkerMeter sets metric.Meter instance to the worker.
func WithWorkerMeter(meter metric.Meter) WorkerOption {
return func(w *Worker) {
w.meter = meter
}
}
// WithWorkerPanicStackBufSize sets max size for the stacktrace buffer for panicking jobs.
// Default value is 1024 that is enough for most of the cases. Be careful setting buffer suze to the big values
// as this may affect overall performance.
func WithWorkerPanicStackBufSize(size int) WorkerOption {
return func(w *Worker) {
w.panicStackBufSize = size
}
}
// WithWorkerHooksJobLocked sets hooks that are called right after the job was polled from the DB.
// Depending on the polling results hook will have either error or job set, but not both.
// If the error field is set - no other lifecycle hooks will be called for the job.
func WithWorkerHooksJobLocked(hooks ...HookFunc) WorkerOption {
return func(w *Worker) {
w.hooksJobLocked = hooks
}
}
// WithWorkerHooksUnknownJobType sets hooks that are called when worker finds a job with unknown type.
// Error field for this event type is always set since this is an error situation.
// If this hook is called - no other lifecycle hooks will be called for the job.
// When the handler for unknown job types is set with WithWorkerUnknownJobWorkFunc - these hooks are never called
// as the job is handled in the regular way using that handler.
func WithWorkerHooksUnknownJobType(hooks ...HookFunc) WorkerOption {
return func(w *Worker) {
w.hooksUnknownJobType = hooks
}
}
// WithWorkerHooksJobDone sets hooks that are called when worker finished working the job,
// right before the successfully executed job will be removed or errored job handler will be called to decide
// if the Job will be re-queued or discarded.
// Error field is set for the cases when the job was worked with an error.
func WithWorkerHooksJobDone(hooks ...HookFunc) WorkerOption {
return func(w *Worker) {
w.hooksJobDone = hooks
}
}
// WithWorkerHooksJobUndone sets hooks that are called when worker fails to mark the job as done.
// This is an exceptional situation, most likely caused by transaction failed to be committed.
// Hook implementation MUST NOT rely on the transaction provided by the job as it may already be marked as failed.
func WithWorkerHooksJobUndone(hooks ...HookFunc) WorkerOption {
return func(w *Worker) {
w.hooksJobUndone = hooks
}
}
// WithWorkerPollStrategy overrides default poll strategy with given value
func WithWorkerPollStrategy(s PollStrategy) WorkerOption {
return func(w *Worker) {
w.pollStrategy = s
}
}
// WithWorkerGracefulShutdown enables graceful shutdown mode in the worker.
// When graceful shutdown is enabled - worker does not propagate cancel context to Job,
// as a result worker is waiting for the Job being currently executed and only then shuts down.
// Use this mode carefully, as Job handler is not aware anymore of the worker context state and
// dependencies may already be cancelled/closed, so it is up to the job to ensure everything is
// still working. Values of the original context are not propagated to the handler context as well
// when the graceful mode is enabled.
//
// Use "handlerCtx" to set up custom handler context. When set to nil - defaults to context.Background().
func WithWorkerGracefulShutdown(handlerCtx func() context.Context) WorkerOption {
return func(w *Worker) {
w.graceful = true
w.gracefulCtx = handlerCtx
}
}
// WithWorkerSpanWorkOneNoJob enables tracing span generation for every try to get one.
// When set to true - generates a span for every DB poll, even when no job was acquired. This may
// generate a lot of empty spans, but may help with some debugging, so use carefully.
func WithWorkerSpanWorkOneNoJob(spanWorkOneNoJob bool) WorkerOption {
return func(w *Worker) {
w.spanWorkOneNoJob = spanWorkOneNoJob
}
}
// WithWorkerJobTTL sets max time a job can run. Implementation-wise the job runs with the timeout context,
// so it is up to the job implementation to handle context cancellation properly.
func WithWorkerJobTTL(d time.Duration) WorkerOption {
return func(w *Worker) {
w.jobTTL = d
}
}
// WithWorkerUnknownJobWorkFunc sets the handler for unknown job types.
// When the handler is set - hooks set with WithWorkerHooksUnknownJobType are never called as the job is
// handled in the regular way.
func WithWorkerUnknownJobWorkFunc(wf WorkFunc) WorkerOption {
return func(w *Worker) {
w.unknownJobTypeWF = wf
}
}
// WithPoolPollInterval overrides default poll interval with the given value.
// Poll interval is the "sleep" duration if there were no jobs found in the DB.
func WithPoolPollInterval(d time.Duration) WorkerPoolOption {
return func(w *WorkerPool) {
w.interval = d
}
}
// WithPoolQueue overrides default worker queue name with the given value.
func WithPoolQueue(queue string) WorkerPoolOption {
return func(w *WorkerPool) {
w.queue = queue
}
}
// WithPoolID sets worker pool ID for easier identification in logs
func WithPoolID(id string) WorkerPoolOption {
return func(w *WorkerPool) {
w.id = id
}
}
// WithPoolLogger sets Logger implementation to worker pool
func WithPoolLogger(logger adapter.Logger) WorkerPoolOption {
return func(w *WorkerPool) {
w.logger = logger
}
}
// WithPoolPollStrategy overrides default poll strategy with given value
func WithPoolPollStrategy(s PollStrategy) WorkerPoolOption {
return func(w *WorkerPool) {
w.pollStrategy = s
}
}
// WithPoolTracer sets trace.Tracer instance to every worker in the pool.
func WithPoolTracer(tracer trace.Tracer) WorkerPoolOption {
return func(w *WorkerPool) {
w.tracer = tracer
}
}
// WithPoolMeter sets metric.Meter instance to every worker in the pool.
func WithPoolMeter(meter metric.Meter) WorkerPoolOption {
return func(w *WorkerPool) {
w.meter = meter
}
}
// WithPoolHooksJobLocked calls WithWorkerHooksJobLocked for every worker in the pool.
func WithPoolHooksJobLocked(hooks ...HookFunc) WorkerPoolOption {
return func(w *WorkerPool) {
w.hooksJobLocked = hooks
}
}
// WithPoolHooksUnknownJobType calls WithWorkerHooksUnknownJobType for every worker in the pool.
// When the handler for unknown job types is set with WithPoolUnknownJobWorkFunc - these hooks are never called
// as the job is handled in the regular way using that handler.
func WithPoolHooksUnknownJobType(hooks ...HookFunc) WorkerPoolOption {
return func(w *WorkerPool) {
w.hooksUnknownJobType = hooks
}
}
// WithPoolHooksJobDone calls WithWorkerHooksJobDone for every worker in the pool.
func WithPoolHooksJobDone(hooks ...HookFunc) WorkerPoolOption {
return func(w *WorkerPool) {
w.hooksJobDone = hooks
}
}
// WithPoolHooksJobUndone calls WithWorkerHooksJobUndone for every worker in the pool.
func WithPoolHooksJobUndone(hooks ...HookFunc) WorkerPoolOption {
return func(w *WorkerPool) {
w.hooksJobUndone = hooks
}
}
// WithPoolGracefulShutdown enables graceful shutdown mode for all workers in the pool.
// See WithWorkerGracefulShutdown for details.
func WithPoolGracefulShutdown(handlerCtx func() context.Context) WorkerPoolOption {
return func(w *WorkerPool) {
w.graceful = true
w.gracefulCtx = handlerCtx
}
}
// WithPoolPanicStackBufSize sets max size for the stacktrace buffer for panicking jobs.
// Default value is 1024 that is enough for most of the cases. Be careful setting buffer suze to the big values
// as this may affect overall performance.
func WithPoolPanicStackBufSize(size int) WorkerPoolOption {
return func(w *WorkerPool) {
w.panicStackBufSize = size
}
}
// WithPoolSpanWorkOneNoJob enables tracing span generation for every try to get one.
// When set to true - generates a span for every DB poll, even when no job was acquired. This may
// generate a lot of empty spans, but may help with some debugging, so use carefully.
func WithPoolSpanWorkOneNoJob(spanWorkOneNoJob bool) WorkerPoolOption {
return func(w *WorkerPool) {
w.spanWorkOneNoJob = spanWorkOneNoJob
}
}
// WithPoolJobTTL sets max time a job can run. Implementation-wise the job runs with the timeout context,
// so it is up to the job implementation to handle context cancellation properly.
func WithPoolJobTTL(d time.Duration) WorkerPoolOption {
return func(w *WorkerPool) {
w.jobTTL = d
}
}
// WithPoolUnknownJobWorkFunc sets the handler for unknown job types.
// When the handler is set - hooks set with WithPoolHooksUnknownJobType are never called as the job is
// handled in the regular way.
func WithPoolUnknownJobWorkFunc(wf WorkFunc) WorkerPoolOption {
return func(w *WorkerPool) {
w.unknownJobTypeWF = wf
}
}