forked from vgarvardt/gue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
299 lines (249 loc) · 7.39 KB
/
worker.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package guex
import (
"bytes"
"context"
"errors"
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
var (
Meter = otel.Meter("guex")
EnqueueMeter metric.Int64Counter
)
func init() {
var err error
EnqueueMeter, err = Meter.Int64Counter("enqueue",
metric.WithUnit("1"),
metric.WithDescription("tasks insertion metric"),
)
if err != nil {
panic("error create metric")
}
}
// PollStrategy determines how the DB is queried for the next job to work on
type PollStrategy string
const (
defaultPollInterval = 5 * time.Second
defaultQueueName = ""
defaultPanicStackBufSize = 1024
)
// WorkFunc is the handler function that performs the Job. If an error is returned, the Job
// is either re-enqueued with the given backoff or is discarded based on the worker backoff strategy
// and returned error.
//
// Modifying Job fields and calling any methods that are modifying its state within the handler may lead to undefined
// behaviour. Please never do this.
type WorkFunc func(ctx context.Context, j *Job) error
// HookFunc is a function that may react to a Job lifecycle events. All the callbacks are being executed synchronously,
// so be careful with the long-running locking operations. Hooks do not return an error, therefore they can not and
// must not be used to affect the Job execution flow, e.g. cancel it - this is the WorkFunc responsibility.
// Modifying Job fields and calling any methods that are modifying its state within hooks may lead to undefined
// behaviour. Please never do this.
//
// Depending on the event err parameter may be empty or not - check the event description for its meaning.
type HookFunc func(ctx context.Context, j *Job, err error)
// WorkMap is a map of Job names to WorkFuncs that are used to perform Jobs of a
// given type.
type WorkMap map[string]WorkFunc
// WorkerPool is a pool of Workers, each working jobs from the queue
// at the specified interval using the WorkMap.
type WorkerPool struct {
wm WorkMap
interval time.Duration
queue []QueueLimit
client *Client
id string
onAir int32
ctxCancel context.CancelFunc
ctxCancel1 context.CancelCauseFunc
waitStop sync.WaitGroup
logger *zap.Logger
lock sync.RWMutex
queueRestoreAfter time.Duration
queueRestoreInterval time.Duration
graceful bool
gracefulCtx func() context.Context
hooksUnknownJobType []HookFunc
hooksJobDone []HookFunc
panicStackBufSize int
}
func (w *WorkerPool) Client() *Client {
return w.client
}
// NewWorkerPool creates a new WorkerPool with count workers using the Client c.
//
// Each Worker in the pool default to a poll interval of 5 seconds, which can be
// overridden by WithPoolInterval option. The default queue is the
// nameless queue "", which can be overridden by WithPoolQueue option.
func NewWorkerPool(c *Client, options ...WorkerPoolOption) (*WorkerPool, error) {
var w = WorkerPool{
interval: defaultPollInterval,
client: c,
id: RandomStringID(),
wm: WorkMap{},
queueRestoreAfter: time.Minute * 5,
queueRestoreInterval: time.Minute,
logger: zap.NewNop(),
panicStackBufSize: defaultPanicStackBufSize,
}
for _, option := range options {
option(&w)
}
if len(w.wm) == 0 {
return nil, fmt.Errorf("empty worker functions list")
}
return &w, nil
}
// Run runs all the Workers in the WorkerPool in own goroutines.
// Run blocks until all workers exit. Use context cancellation for
// shutdown.
func (w *WorkerPool) Run(parentCtx context.Context) (err error) {
if atomic.LoadInt32(&w.onAir) == 1 {
return nil
}
atomic.AddInt32(&w.onAir, 1)
defer atomic.StoreInt32(&w.onAir, 0)
var (
ctx context.Context
grp *errgroup.Group
)
ctx, w.ctxCancel = context.WithCancel(parentCtx)
grp, ctx = errgroup.WithContext(ctx)
w.waitStop.Add(1)
grp.Go(func() error {
defer w.waitStop.Done()
for {
select {
case <-ctx.Done():
return nil
case <-time.After(w.interval):
if err = w.WorkOne(ctx); err != nil {
w.logger.Error("error run worker", zap.Error(err))
}
}
}
})
w.waitStop.Add(1)
grp.Go(func() error {
defer w.waitStop.Done()
for {
select {
case <-ctx.Done():
return nil
case <-time.After(w.queueRestoreInterval):
}
var childCtx, cancelFunc = context.WithTimeout(ctx, time.Second*30)
if err := w.client.RestoreStuck(childCtx, w.queueRestoreAfter, w.queue...); err != nil {
w.logger.Error("error restore stuck jobs", zap.Error(err))
}
cancelFunc()
}
})
err = grp.Wait()
w.waitStop.Wait()
return err
}
func (w *WorkerPool) Stop() {
w.ctxCancel()
w.waitStop.Wait()
}
func (w *WorkerPool) WorkMap(wm WorkMap) {
w.wm = wm
}
// WorkOne tries to consume single message from the queue.
func (w *WorkerPool) WorkOne(ctx context.Context) (err error) {
var jobs []*Job
w.lock.RLock()
if jobs, err = w.client.LockNextScheduledJob(ctx, w.queue); err != nil {
w.lock.RUnlock()
return fmt.Errorf("error get scheduled job: %w", err)
}
w.lock.RUnlock()
if jobs == nil {
return nil
}
for _, j := range jobs {
w.limitWorker(j.Queue, -1)
w.waitStop.Add(1)
go func(j *Job) {
defer w.waitStop.Done()
defer w.recoverPanic(ctx, j)
defer w.limitWorker(j.Queue, 1)
var (
err error
h WorkFunc
ok bool
l = w.logger.With(zap.String("type", j.JobType), zap.Int64("id", j.ID))
)
if h, ok = w.wm[j.JobType]; !ok {
var errUnknownType = fmt.Errorf("unknown job type: %s", j.JobType)
if err = j.Error(ctx, errUnknownType); err != nil {
l.Error("error set error status for job", zap.Error(err))
return
}
for _, hook := range w.hooksUnknownJobType {
hook(ctx, j, errUnknownType)
}
return
}
if err = h(ctx, j); err != nil {
for _, hook := range w.hooksJobDone {
hook(ctx, j, err)
}
if jErr := j.Error(ctx, err); jErr != nil {
l.Error("error set error status for job", zap.Error(err))
}
return
}
for _, hook := range w.hooksJobDone {
hook(ctx, j, nil)
}
if err = j.Done(ctx); err != nil {
l.Error("error set done status for job", zap.Error(err))
}
return
}(j)
}
return nil
}
func (w *WorkerPool) limitWorker(queue string, counter int32) {
w.lock.Lock()
defer w.lock.Unlock()
for i := range w.queue {
if w.queue[i].Queue != queue {
continue
}
w.queue[i].Limit += counter
break
}
}
// recoverPanic tries to handle panics in job execution.
// A stacktrace is stored into Job last_error.
func (w *WorkerPool) recoverPanic(ctx context.Context, j *Job) {
if r := recover(); r != nil {
// record an error on the job with panic message and stacktrace
var (
stackBuf = make([]byte, w.panicStackBufSize)
n = runtime.Stack(stackBuf, false)
buf = new(bytes.Buffer)
_, printRErr = fmt.Fprintf(buf, "%v\n", r)
_, printStackErr = fmt.Fprintln(buf, string(stackBuf[:n]))
_, printEllipsisErr = fmt.Fprintln(buf, "[...]")
stacktrace = buf.String()
)
if err := multierr.Combine(printRErr, printStackErr, printEllipsisErr); err != nil {
w.logger.Error("could not build panicked job stacktrace", zap.Error(err))
}
if err := j.Error(ctx, errors.New(stacktrace)); err != nil {
w.logger.Error("got an error on setting an error to a panicked job", zap.Error(err))
}
}
}