forked from vgarvardt/gue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_option.go
209 lines (180 loc) · 6.71 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
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.
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
}
}
// 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
}
}
// 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.
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
}
}
// 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
}
}