-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathworker.go
More file actions
1451 lines (1294 loc) · 41 KB
/
Copy pathworker.go
File metadata and controls
1451 lines (1294 loc) · 41 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gobullmq
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/vmihailenco/msgpack/v5"
"go.codycody31.dev/gobullmq/internal/fifoqueue"
eventemitter "go.codycody31.dev/gobullmq/internal/eventEmitter"
"go.codycody31.dev/gobullmq/internal/lua"
"go.codycody31.dev/gobullmq/internal/utils"
backoffutil "go.codycody31.dev/gobullmq/internal/utils/backoff"
)
// ProcessFunc is the callback invoked for each job.
// D is the type of the job's data payload, R is the result type.
type ProcessFunc[D any, R any] func(ctx context.Context, job *Job[D]) (R, error)
// Worker processes jobs from a queue.
// D is the type of job data, R is the type of job results.
type Worker[D any, R any] struct {
name string
token uuid.UUID
ee *eventemitter.EventEmitter
running atomic.Bool
closing atomic.Bool
forceClosing atomic.Bool
paused atomic.Bool
redisClient redis.Cmdable
// runCtx is the run-lifetime context created by Run and cancelled by
// Close/Shutdown. Storing a context in a struct is normally discouraged,
// but a worker is a long-lived process whose background goroutines,
// timers, and Redis calls all share one lifetime, the documented
// exception to that rule.
runCtx context.Context
cancel context.CancelFunc
prefix string
keyPrefix string
mutex sync.Mutex
wg sync.WaitGroup
opts WorkerOptions
processFn ProcessFunc[D, R]
extendLocksTimer *time.Timer
stalledCheckTimer *time.Timer
jobsInProgress *jobsInProgress
blockUntil atomic.Int64
// limitUntil holds an absolute epoch-ms deadline for the rate-limit
// window (0 when unlimited).
limitUntil atomic.Int64
drained atomic.Bool
// blockingFetch serializes the blocking wait-list read so at most one
// BLMOVE blocks at a time (upstream's this.waiting guard).
blockingFetch sync.Mutex
// waitCancel aborts an in-flight blocking read (upstream force-disconnects
// the blocking connection on pause/close).
waitCancelMu sync.Mutex
waitCancel context.CancelFunc
scripts *scripts
asyncFifoQueue *fifoqueue.FifoQueue[rawJob]
pauseCh chan struct{}
}
// WorkerOptions configures a Worker.
type WorkerOptions struct {
Autorun bool
Concurrency int
Limiter *RateLimiterOptions
Metrics *MetricsOptions
Prefix string
MaxStalledCount int
StalledInterval time.Duration
RemoveOnComplete *KeepJobs
RemoveOnFail *KeepJobs
SkipStalledCheck bool
SkipLockRenewal bool
DrainDelay time.Duration
LockDuration time.Duration
LockRenewTime time.Duration
RunRetryDelay time.Duration
Backoff *BackoffOptions
BackoffStrategy BackoffStrategyFunc
}
// BackoffStrategyFunc is a custom function to calculate backoff delay in milliseconds.
// Return 0 for immediate retry, or a positive value for the delay.
// Return -1 to signal "do not retry" (move to failed).
type BackoffStrategyFunc func(attemptsMade int, opts *BackoffOptions, err error, jobID string) int
// RateLimiterOptions caps the worker at Max jobs per Duration milliseconds.
type RateLimiterOptions struct {
Max int `msgpack:"max"`
Duration int `msgpack:"duration"`
}
// MetricsOptions enables collection of completed/failed job counts, keeping
// up to MaxDataPoints one-minute data points.
type MetricsOptions struct {
MaxDataPoints int
}
type nextJobOptions struct {
Block bool
}
type jobsInProgress struct {
sync.Mutex
jobs map[string]jobInProgress
}
type jobInProgress struct {
job rawJob
ts time.Time
}
// Name returns the queue name of the worker.
func (w *Worker[D, R]) Name() string {
return w.name
}
// OnCompleted registers a typed callback for when a job completes and returns a ListenerID.
func (w *Worker[D, R]) OnCompleted(fn func(job *Job[D], result R)) ListenerID {
return w.ee.On("completed", func(args ...any) {
if len(args) >= 2 {
if raw, ok := args[0].(rawJob); ok {
typed, _ := wrapRawJob[D](&raw)
if typed != nil {
if r, ok := args[1].(R); ok {
fn(typed, r)
}
}
}
}
})
}
// OnFailed registers a typed callback for when a job fails and returns a ListenerID.
func (w *Worker[D, R]) OnFailed(fn func(job *Job[D], err error)) ListenerID {
return w.ee.On("failed", func(args ...any) {
if len(args) >= 2 {
if raw, ok := args[0].(rawJob); ok {
typed, _ := wrapRawJob[D](&raw)
if typed != nil {
if e, ok := args[1].(error); ok {
fn(typed, e)
}
}
}
}
})
}
// OnActive registers a typed callback for when a job becomes active and returns a ListenerID.
func (w *Worker[D, R]) OnActive(fn func(job *Job[D])) ListenerID {
return w.ee.On("active", func(args ...any) {
if len(args) >= 1 {
if raw, ok := args[0].(rawJob); ok {
typed, _ := wrapRawJob[D](&raw)
if typed != nil {
fn(typed)
}
}
}
})
}
// OnStalled registers a typed callback for when a job is detected as stalled and returns a ListenerID.
func (w *Worker[D, R]) OnStalled(fn func(jobID string)) ListenerID {
return w.ee.On("stalled", func(args ...any) {
if len(args) >= 1 {
if id, ok := args[0].(string); ok {
fn(id)
}
}
})
}
// OnDrained registers a typed callback for when the queue is drained and returns a ListenerID.
func (w *Worker[D, R]) OnDrained(fn func()) ListenerID {
return w.ee.On("drained", func(...any) {
fn()
})
}
// OnError registers a typed callback for worker errors and returns a ListenerID.
func (w *Worker[D, R]) OnError(fn func(err error)) ListenerID {
return w.ee.On("error", func(args ...any) {
if len(args) >= 1 {
switch e := args[0].(type) {
case error:
fn(e)
case string:
fn(errors.New(e))
default:
fn(fmt.Errorf("%v", e))
}
}
})
}
// OnRetriesExhausted registers a typed callback for when all retry attempts are exhausted and returns a ListenerID.
func (w *Worker[D, R]) OnRetriesExhausted(fn func(job *Job[D], err error)) ListenerID {
return w.ee.On("retries-exhausted", func(args ...any) {
if len(args) >= 2 {
if raw, ok := args[0].(rawJob); ok {
typed, _ := wrapRawJob[D](&raw)
if typed != nil {
if e, ok := args[1].(error); ok {
fn(typed, e)
}
}
}
}
})
}
// nextJobData represents the structured data returned by raw2NextJobData.
type nextJobData struct {
JobData map[string]any
ID string
LimitUntil int64
DelayUntil int64
}
// Default timing configuration
const (
defaultLockDuration = 30 * time.Second
defaultLockRenewTime = 15 * time.Second
defaultStalledInterval = 30 * time.Second
defaultRunRetryDelay = 15 * time.Second // upstream runRetryDelay: 15000
defaultDrainDelay = 5 * time.Second // upstream drainDelay: 5
minQueueCleanupTimeout = time.Second
)
// NewWorker creates a new Worker instance.
// opts may be nil, in which case sensible defaults are used.
func NewWorker[D any, R any](name string, client redis.Cmdable, processor ProcessFunc[D, R], opts *WorkerOptions) (*Worker[D, R], error) {
if name == "" {
return nil, fmt.Errorf("worker name must be provided")
}
if client == nil {
return nil, fmt.Errorf("redis client must not be nil")
}
if opts == nil {
opts = &WorkerOptions{}
}
if opts.LockDuration <= 0 {
opts.LockDuration = defaultLockDuration
}
if opts.LockRenewTime <= 0 || opts.LockRenewTime >= opts.LockDuration {
opts.LockRenewTime = opts.LockDuration / 2
}
if opts.StalledInterval <= 0 {
opts.StalledInterval = defaultStalledInterval
}
if opts.MaxStalledCount == 0 {
opts.MaxStalledCount = 1
} else if opts.MaxStalledCount < 0 {
opts.MaxStalledCount = 0
}
if opts.Concurrency <= 0 {
opts.Concurrency = 1
}
if opts.RunRetryDelay <= 0 {
opts.RunRetryDelay = defaultRunRetryDelay
}
if opts.DrainDelay <= 0 {
opts.DrainDelay = defaultDrainDelay
}
w := &Worker[D, R]{
name: name,
token: uuid.New(),
ee: eventemitter.NewEventEmitter(),
redisClient: client,
opts: *opts,
processFn: processor,
jobsInProgress: &jobsInProgress{
jobs: make(map[string]jobInProgress),
},
pauseCh: make(chan struct{}, 1),
}
if opts.Prefix == "" {
w.keyPrefix = "bull"
} else {
w.keyPrefix = strings.Trim(opts.Prefix, ":")
if w.keyPrefix == "" {
return nil, fmt.Errorf("prefix cannot be empty or just colons")
}
}
w.prefix = w.keyPrefix
w.keyPrefix = w.keyPrefix + ":" + name + ":"
w.scripts = newScripts(w.redisClient, w.keyPrefix)
w.scripts.limiter = opts.Limiter
w.scripts.removeOnComplete = opts.RemoveOnComplete
w.scripts.removeOnFail = opts.RemoveOnFail
return w, nil
}
// emit emits the event with the given name and arguments.
func (w *Worker[D, R]) emit(event string, args ...any) {
w.ee.Emit(event, args...)
}
// Off removes a specific listener by its ListenerID.
func (w *Worker[D, R]) Off(event string, id ListenerID) {
w.ee.RemoveListener(event, id)
}
// On listens for the event and returns a ListenerID that can be used with Off.
func (w *Worker[D, R]) On(event string, listener func(...any)) ListenerID {
return w.ee.On(event, listener)
}
// Once listens for the event only once and returns a ListenerID.
func (w *Worker[D, R]) Once(event string, listener func(...any)) ListenerID {
return w.ee.Once(event, listener)
}
// createJob constructs a rawJob from the map data retrieved from Redis.
func (w *Worker[D, R]) createJob(jobData map[string]any, jobId string) (rawJob, error) {
job, err := jobFromJson(jobData)
if err != nil {
return rawJob{}, fmt.Errorf("failed to deserialize job %s: %w", jobId, err)
}
job.id = jobId
job.setJobContext(w.redisClient, w.keyPrefix)
return job, nil
}
// Run starts the worker with the given context.
func (w *Worker[D, R]) Run(ctx context.Context) error {
w.mutex.Lock()
defer w.mutex.Unlock()
if w.running.Load() {
return ErrWorkerRunning
}
if w.closing.Load() {
return ErrWorkerClosing
}
w.running.Store(true)
ctx, cancel := context.WithCancel(ctx)
w.runCtx = ctx
w.cancel = cancel
clientName := fmt.Sprintf("%s:%s", w.prefix, base64.StdEncoding.EncodeToString([]byte(w.name)))
switch c := w.redisClient.(type) {
case *redis.Client:
_ = c.Do(ctx, "CLIENT", "SETNAME", clientName).Err()
case *redis.ClusterClient:
_ = c.ForEachShard(ctx, func(ctx context.Context, shardClient *redis.Client) error {
return shardClient.Do(ctx, "CLIENT", "SETNAME", clientName).Err()
})
}
go w.startStalledCheckTimer()
go w.startLockExtender()
w.asyncFifoQueue = fifoqueue.NewFifoQueue[rawJob](w.opts.Concurrency, false)
tokenPostfix := 0
addFetchTask := func() {
// Fetch tasks ARE re-added while paused: they park in getNextJob's
// pause-wait loop and Resume wakes them. Refusing to re-add here
// would leave a resumed worker with zero fetchers (livelock).
if w.closing.Load() {
return
}
tokenPostfix++
token := fmt.Sprintf("%s:%d", w.token, tokenPostfix)
if err := w.asyncFifoQueue.Add(func() (rawJob, error) {
j, err := w.retryIfFailed(func() (*rawJob, error) {
nextJob, err := w.getNextJob(w.runCtx, token, nextJobOptions{Block: true})
if err != nil {
return nil, err
}
if nextJob == nil {
return nil, nil
}
nextJob.token = token
return nextJob, nil
}, w.opts.RunRetryDelay)
if err != nil {
w.emit("error", fmt.Errorf("error fetching job: %v", err))
return rawJob{}, err
}
if j.id == "" {
return rawJob{}, nil
}
return j, nil
}); err != nil {
w.emit("error", fmt.Errorf("error adding fetch task to queue: %v", err))
}
}
w.wg.Add(1)
go func() {
defer w.wg.Done()
for i := 0; i < w.opts.Concurrency; i++ {
addFetchTask()
}
for {
if w.closing.Load() && w.asyncFifoQueue.NumTotal() == 0 {
w.emit("closing")
return
}
select {
case <-w.runCtx.Done():
w.emit("closing")
return
default:
}
jobResult, taskErr := w.asyncFifoQueue.Fetch(w.runCtx)
if taskErr != nil {
if errors.Is(taskErr, context.Canceled) || errors.Is(taskErr, fifoqueue.ErrQueueClosed) {
w.emit("info", fmt.Sprintf("Worker stopping due to context cancellation or queue closure: %v", taskErr))
return
}
w.emit("error", fmt.Errorf("error fetching task result from queue: %v", taskErr))
addFetchTask()
continue
}
if jobResult != nil && jobResult.id != "" && jobResult.id != "0" {
fetchedJob := *jobResult
token := fetchedJob.token
w.trackJob(fetchedJob)
if err := w.asyncFifoQueue.Add(func() (rawJob, error) {
// Chain jobs handed back by moveToFinished's fetch-next
// under the same token, like upstream's process loop;
// otherwise the prefetched job is stranded in active.
job := fetchedJob
for job.id != "" && job.id != "0" {
next, _ := w.processJob(
job,
token,
func() bool {
// The running process task counts in NumTotal
// (upstream: numTotal() <= concurrency).
return w.asyncFifoQueue.NumTotal() <= w.opts.Concurrency
},
)
job = next
}
return rawJob{}, nil
}); err != nil {
w.untrackJob(fetchedJob.id)
w.emit("error", fmt.Errorf("error adding process task for job %s: %v", fetchedJob.id, err))
addFetchTask()
}
} else {
addFetchTask()
}
}
}()
return nil
}
// getNextJob gets the next job.
func (w *Worker[D, R]) getNextJob(ctx context.Context, token string, opts nextJobOptions) (*rawJob, error) {
if w.paused.Load() {
if opts.Block {
for w.paused.Load() && !w.closing.Load() {
select {
case <-w.pauseCh:
case <-time.After(100 * time.Millisecond):
case <-ctx.Done():
return nil, nil
}
}
} else {
return nil, nil
}
}
if w.closing.Load() {
return nil, nil
}
if limitUntil := w.limitUntil.Load(); limitUntil > time.Now().UnixMilli() {
if err := w.delay(ctx, limitUntil); err != nil {
return nil, fmt.Errorf("failed to delay: %w", err)
}
}
if w.drained.Load() && opts.Block {
// One blocking read at a time (upstream this.waiting).
w.blockingFetch.Lock()
var jobID string
// Re-check under the guard: Pause/Close may have landed while this
// task waited for the previous blocking read to finish; starting a
// fresh BLMOVE then would keep pulling jobs during a pause or a
// Shutdown drain window.
if !w.paused.Load() && !w.closing.Load() {
var err error
jobID, err = w.waitForJob(ctx)
if err != nil {
w.blockingFetch.Unlock()
if !w.paused.Load() && !w.closing.Load() {
return nil, fmt.Errorf("failed to wait for job: %w", err)
}
return nil, nil
}
}
w.blockingFetch.Unlock()
if jobID == "" && (w.paused.Load() || w.closing.Load()) {
// Nothing was moved and we are pausing/closing: do not promote
// or lock new work. (A non-empty jobID was already moved by the
// BLMOVE server-side; locking it lets stalled recovery handle
// the boundary, same as upstream's disconnect race.)
return nil, nil
}
// A block timeout still proceeds to moveToActive: its Lua promotes
// due delayed jobs, the only mechanism by which an idle worker
// picks up delayed work.
return w.moveToActive(ctx, token, jobID)
}
return w.moveToActive(ctx, token, "")
}
// moveToActive moves the job to the active list.
func (w *Worker[D, R]) moveToActive(ctx context.Context, token string, jobId string) (*rawJob, error) {
if jobId != "" && len(jobId) > 2 && jobId[0:2] == "0:" {
blockUntil, err := strconv.Atoi(jobId[2:])
if err != nil {
return nil, fmt.Errorf("failed to parse blockUntil: %w", err)
}
w.blockUntil.Store(int64(blockUntil))
}
keys := []string{
w.keyPrefix + "wait",
w.keyPrefix + "active",
w.keyPrefix + "prioritized",
w.keyPrefix + "events",
w.keyPrefix + "stalled",
w.keyPrefix + "limiter",
w.keyPrefix + "delayed",
w.keyPrefix + "paused",
w.keyPrefix + "meta",
w.keyPrefix + "pc",
}
opts := map[string]any{
"token": token,
"lockDuration": int(w.opts.LockDuration / time.Millisecond),
}
if w.opts.Limiter != nil {
opts["limiter"] = map[string]any{
"max": w.opts.Limiter.Max,
"duration": w.opts.Limiter.Duration,
}
}
msgPackedOpts, err := msgpack.Marshal(opts)
if err != nil {
return nil, fmt.Errorf("failed to marshal opts: %w", err)
}
rawResult, err := lua.MoveToActive(ctx, w.redisClient, keys, w.keyPrefix, time.Now().UnixMilli(), jobId, string(msgPackedOpts))
if err != nil {
return nil, err
}
rawResultSlice, ok := rawResult.([]any)
if !ok {
return nil, fmt.Errorf("unexpected type for rawResult: %T", rawResult)
}
result := raw2NextJobData(rawResultSlice)
if result == nil {
w.emit("error", fmt.Errorf("moveToActive received invalid data from Lua for token %s, jobID %s", token, jobId))
return nil, nil
}
if result.ID == "0" || result.ID == "" {
return nil, nil
}
return w.nextJobFromJobData(ctx, result.JobData, result.ID, result.LimitUntil, result.DelayUntil, token)
}
// raw2NextJobData processes raw data and returns a typed nextJobData structure.
func raw2NextJobData(raw []any) *nextJobData {
if len(raw) < 4 {
return nil
}
limitVal, limitOk := raw[2].(int64)
delayVal, delayOk := raw[3].(int64)
if !limitOk || !delayOk {
return nil
}
result := &nextJobData{
ID: fmt.Sprintf("%v", raw[1]),
LimitUntil: clampMin(limitVal, 0),
DelayUntil: clampMin(delayVal, 0),
}
if raw[0] != nil {
jobMap := utils.Array2obj(raw[0])
result.JobData = jobMap
}
return result
}
// waitForJob waits for a job ID from the wait list using the
// BRPOPLPUSH-equivalent orientation (pop the oldest job from the right of
// wait, push to the left of active). A block timeout returns "" without
// error; Pause/Close abort the wait.
func (w *Worker[D, R]) waitForJob(ctx context.Context) (string, error) {
blockTimeout := w.opts.DrainDelay
if blockUntil := w.blockUntil.Load(); blockUntil > 0 {
blockTimeout = time.Until(time.UnixMilli(blockUntil))
}
// go-redis only supports whole-second BLMOVE timeouts (and logs when
// truncating), so clamp to [1s, 10s].
if blockTimeout < time.Second {
blockTimeout = time.Second
}
if blockTimeout > 10*time.Second {
blockTimeout = 10 * time.Second
}
waitCtx, cancel := context.WithCancel(ctx)
defer cancel()
w.waitCancelMu.Lock()
w.waitCancel = cancel
w.waitCancelMu.Unlock()
defer func() {
w.waitCancelMu.Lock()
w.waitCancel = nil
w.waitCancelMu.Unlock()
}()
result, err := w.redisClient.BLMove(waitCtx, w.keyPrefix+"wait", w.keyPrefix+"active", "RIGHT", "LEFT", blockTimeout).Result()
if err != nil {
// Block timeout (redis.Nil) and an aborted wait are not errors.
if errors.Is(err, redis.Nil) || errors.Is(err, context.Canceled) {
return "", nil
}
return "", err
}
return result, nil
}
// abortWait cancels an in-flight blocking read, if any.
func (w *Worker[D, R]) abortWait() {
w.waitCancelMu.Lock()
if w.waitCancel != nil {
w.waitCancel()
}
w.waitCancelMu.Unlock()
}
// sleepContext sleeps for the specified duration or until context is cancelled.
func sleepContext(ctx context.Context, d time.Duration) error {
if d <= 0 {
return nil
}
timer := time.NewTimer(d)
defer timer.Stop()
select {
case <-timer.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// delay delays the execution for the specified time, respecting context cancellation.
func (w *Worker[D, R]) delay(ctx context.Context, until int64) error {
now := time.Now().UnixMilli()
if until > now {
return sleepContext(ctx, time.Duration(until-now)*time.Millisecond)
}
return nil
}
// nextJobFromJobData processes the next job data and returns a rawJob.
func (w *Worker[D, R]) nextJobFromJobData(
ctx context.Context,
jobData map[string]any,
jobID string,
limitUntil int64,
delayUntil int64,
token string,
) (*rawJob, error) {
if jobData == nil {
if !w.drained.Load() {
w.emit("drained")
w.drained.Store(true)
w.blockUntil.Store(0)
}
}
// The Lua returns limitUntil as a relative PTTL; store an absolute
// deadline so delay() sleeps for the actual window.
if limitUntil > 0 {
w.limitUntil.Store(time.Now().UnixMilli() + limitUntil)
} else {
w.limitUntil.Store(0)
}
if delayUntil > 0 {
w.blockUntil.Store(clampMin(delayUntil, 0))
}
if jobData == nil {
return nil, nil
}
w.drained.Store(false)
job, err := w.createJob(jobData, jobID)
if err != nil {
return nil, err
}
job.token = token
if job.opts.Repeat != nil {
// Schedule the next iteration at fetch time, before processing, so a
// failing iteration does not kill the chain. The existence check
// (skipCheckExists=false) lets RemoveRepeatable stop the chain.
// A scheduling failure aborts the fetch like upstream (the job stays
// in active for stalled recovery) instead of silently ending the chain.
jsonData, _ := job.data.(string)
if _, err := addNextRepeatableJobInternal(ctx, w.redisClient, w.keyPrefix, job.name, jsonData, job.opts, false); err != nil {
return nil, fmt.Errorf("failed to schedule next repeatable iteration for job %s: %w", job.id, err)
}
}
return &job, nil
}
// processJob processes the job.
func (w *Worker[D, R]) processJob(job rawJob, token string, fetchNextCallback func() bool) (rawJob, error) {
if w.forceClosing.Load() {
w.untrackJob(job.id)
return rawJob{}, nil
}
w.trackJob(job)
defer w.untrackJob(job.id)
w.emit("active", job, "waiting")
var result R
var err error
processFnCtx, processFnCtxCancel := context.WithCancel(w.runCtx)
defer processFnCtxCancel()
func() {
defer func() {
if r := recover(); r != nil {
w.emit("error", fmt.Errorf("panic recovered for job %s with token %s: %v", job.id, token, r))
err = fmt.Errorf("panic: %v", r)
}
}()
typedJob, wrapErr := wrapRawJob[D](&job)
if wrapErr != nil {
err = fmt.Errorf("failed to deserialize job data: %w", wrapErr)
return
}
result, err = w.processFn(processFnCtx, typedJob)
}()
if err != nil {
return w.handleJobError(job, token, err)
}
return w.handleJobSuccess(job, token, result, fetchNextCallback)
}
func (w *Worker[D, R]) trackJob(job rawJob) {
if job.id == "" || job.id == "0" {
return
}
w.jobsInProgress.Lock()
if _, exists := w.jobsInProgress.jobs[job.id]; !exists {
w.jobsInProgress.jobs[job.id] = jobInProgress{job: job, ts: time.Now()}
}
w.jobsInProgress.Unlock()
}
func (w *Worker[D, R]) untrackJob(jobID string) {
if jobID == "" || jobID == "0" {
return
}
w.jobsInProgress.Lock()
delete(w.jobsInProgress.jobs, jobID)
w.jobsInProgress.Unlock()
}
// handleJobError handles the error path of processJob. The fail path never
// prefetches (upstream job.moveToFailed defaults fetchNext to false).
func (w *Worker[D, R]) handleJobError(job rawJob, token string, err error) (rawJob, error) {
if errors.Is(err, ErrRateLimit) {
if w.scripts != nil {
pttl, mErr := w.scripts.moveJobFromActiveToWait(w.runCtx, job.id, token)
if mErr != nil {
w.emit("error", fmt.Errorf("moveJobFromActiveToWait failed for %s: %v", job.id, mErr))
} else {
w.limitUntil.Store(time.Now().Add(time.Duration(pttl) * time.Millisecond).UnixMilli())
}
}
return rawJob{}, err
}
if errors.Is(err, ErrDelayed) || errors.Is(err, ErrWaitingChildren) {
return rawJob{}, err
}
// failedReason and the stacktrace are persisted atomically with the state
// move below, like upstream's single multi in job.moveToFailed.
stacktrace, stErr := job.prepareStacktrace(err)
if stErr != nil {
w.emit("error", fmt.Errorf("failed to marshal stacktrace for job %s: %v", job.id, stErr))
return rawJob{}, err
}
// An automatic retry is performed unless attempts are exhausted, the job
// was discarded, or the error is marked unrecoverable (upstream job.ts:622).
if job.attemptsMade < job.opts.Attempts && !job.discarded && !errors.Is(err, ErrUnrecoverable) {
// The job's own backoff wins; WorkerOptions.Backoff is the default for
// jobs without one (like queue defaultJobOptions.backoff upstream).
backoff := job.opts.Backoff
if backoff == nil {
backoff = w.opts.Backoff
}
delayMs := 0
if backoff != nil {
if w.opts.BackoffStrategy != nil && !backoffutil.IsBuiltin(backoff.Type) {
// Custom strategies handle non-builtin types, like upstream
// settings.backoffStrategy in lookupStrategy.
delayMs = w.opts.BackoffStrategy(job.attemptsMade, backoff, err, job.id)
} else {
var backoffErr error
delayMs, backoffErr = backoffutil.Calculate(backoffutil.Options{Type: backoff.Type, Delay: backoff.Delay}, job.attemptsMade)
if backoffErr != nil {
// Upstream throws before any Redis write: the job stays in
// active and the stalled checker recovers it.
w.emit("error", fmt.Errorf("backoff calculation failed for job %s: %v", job.id, backoffErr))
return rawJob{}, err
}
}
}
if delayMs >= 0 {
var opErr error
jobKey := w.keyPrefix + job.id
if delayMs > 0 {
keys, args := w.scripts.moveToDelayedArgs(job.id, time.Now().UnixMilli()+int64(delayMs), token)
result, derr := runFailureMulti(w.runCtx, w.redisClient, jobKey, stacktrace, job.failedReason, lua.MoveToDelayedScript, keys, args)
opErr = derr
if opErr == nil {
if code, ok := result.(int64); ok && code < 0 {
opErr = finishedErrors(code, job.id, "moveToDelayed")
}
}
} else {
keys, args := w.scripts.retryJobArgs(job.id, job.opts.Lifo, token)
result, rerr := runFailureMulti(w.runCtx, w.redisClient, jobKey, stacktrace, job.failedReason, lua.RetryJobScript, keys, args)
opErr = rerr
if opErr == nil {
if code, ok := result.(int64); ok && code < 0 {
opErr = finishedErrors(code, job.id, "retryJob")
}
}
}
if opErr != nil {
// Like upstream handleFailed's catch: emit only; a worker will
// (or already has) recover the job through the stalled path.
w.emit("error", fmt.Errorf("failed to schedule retry for job %s: %v", job.id, opErr))
return rawJob{}, err
}
w.emit("failed", job, err, "active")
return rawJob{}, err
}
// delayMs < 0: the strategy vetoed the retry; fall through to failed.
}
if job.opts.Attempts > 0 && job.attemptsMade >= job.opts.Attempts {
w.emit("retries-exhausted", job, err)
}
lockDurationMs := int(w.opts.LockDuration / time.Millisecond)
maxMetricsSize := ""
if w.opts.Metrics != nil && w.opts.Metrics.MaxDataPoints > 0 {
maxMetricsSize = strconv.Itoa(w.opts.Metrics.MaxDataPoints)
}
if moveErr := jobMoveToFailed(w.runCtx, w.scripts, &job, stacktrace, token, false, lockDurationMs, maxMetricsSize); moveErr != nil {
w.emit("error", fmt.Errorf("error explicitly moving job %s to failed: %v", job.id, moveErr))
}
w.emit("failed", job, err, "active")
return rawJob{}, err
}
// handleJobSuccess handles the success path of processJob.
func (w *Worker[D, R]) handleJobSuccess(job rawJob, token string, result R, fetchNextCallback func() bool) (rawJob, error) {
lockDurationMs := int(w.opts.LockDuration / time.Millisecond)
maxMetricsSize := ""
if w.opts.Metrics != nil && w.opts.Metrics.MaxDataPoints > 0 {
maxMetricsSize = strconv.Itoa(w.opts.Metrics.MaxDataPoints)
}
job.returnValue = result
stringifiedReturnValue, marshalErr := json.Marshal(result)
if marshalErr != nil {
w.emit("error", fmt.Errorf("error marshaling result for job %s: %v", job.id, marshalErr))
return rawJob{}, marshalErr
}
getNext := fetchNextCallback() && !w.closing.Load() && !w.paused.Load()
keys, args, scriptErr := w.scripts.moveToFinishedArgs(&job, string(stringifiedReturnValue), "returnvalue", job.opts.RemoveOnComplete, "completed", token, time.Now(), getNext, lockDurationMs, maxMetricsSize)
if scriptErr != nil {
w.emit("error", fmt.Errorf("error moving job to completed: %v", scriptErr))
return rawJob{}, scriptErr
}
job.finishedOn = time.Now()
rawLuaResult, luaErr := lua.MoveToFinished(w.runCtx, w.redisClient, keys, args...)
if luaErr != nil {
w.emit("error", fmt.Errorf("error moving job to completed: %v", luaErr))
return rawJob{}, luaErr
}
var completed []any
switch v := rawLuaResult.(type) {
case int64:
completed = []any{v}
case []any:
completed = v
default:
return rawJob{}, fmt.Errorf("unexpected type for rawResult: %T", rawLuaResult)
}
if code, ok := completed[0].(int64); ok && code < 0 {
return rawJob{}, finishedErrors(code, job.id, "moveToFinished")
}
w.emit("completed", job, result, "active")
nextData := raw2NextJobData(completed)
if nextData != nil {
j, nextErr := w.nextJobFromJobData(w.runCtx, nextData.JobData, nextData.ID, nextData.LimitUntil, nextData.DelayUntil, token)
if nextErr != nil {
w.emit("error", fmt.Errorf("error getting next job: %v", nextErr))
return rawJob{}, nextErr
}
if j != nil {
return *j, nil
}
}
return rawJob{}, nil
}
// NextJob manually fetches the next waiting job and moves it to active,
// for workers used without Run() (upstream's manual processing pattern).
// token identifies the lock owner and must be passed to the job's
// MoveToCompleted/MoveToFailed calls (the returned job carries it).
// Returns nil when no job is available.
func (w *Worker[D, R]) NextJob(ctx context.Context, token string) (*Job[D], error) {
raw, err := w.getNextJob(ctx, token, nextJobOptions{Block: false})
if err != nil || raw == nil {
return nil, err