-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_worker.go
146 lines (116 loc) · 3.52 KB
/
redis_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
package goscheds
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/bsm/redislock"
"github.com/cenkalti/backoff/v4"
"github.com/go-redis/redis/v9"
log "github.com/sirupsen/logrus"
)
type RedisWorker struct {
namespace string
client *redis.Client
jobHandler map[string]HandlerFunc
locker *redislock.Client
*sync.Mutex
}
var _ WorkerService = (*RedisWorker)(nil)
func NewRedisWorker(namespace string, client *redis.Client) *RedisWorker {
redisLocker := redislock.New(client)
return &RedisWorker{
namespace: namespace,
locker: redisLocker,
client: client,
jobHandler: make(map[string]HandlerFunc),
Mutex: &sync.Mutex{},
}
}
func (r *RedisWorker) RegisterHandler(name string, fn HandlerFunc) {
r.Lock()
defer r.Unlock()
r.jobHandler[name] = fn
}
func (r *RedisWorker) ProcessJob(ctx context.Context) {
for k, _ := range r.jobHandler {
go r.processJobForQueue(ctx, k)
}
}
const onProgressSuffixKey = "on_progress"
func (r *RedisWorker) processJobForQueue(ctx context.Context, queueName string) {
lockKey := fmt.Sprintf("%s:LOCK:%s", r.namespace, queueName)
for {
locker, err := r.locker.Obtain(ctx, lockKey, time.Second, nil)
if err != nil {
log.Error(err)
time.Sleep(100 * time.Millisecond)
continue
}
queueKey := fmt.Sprintf("%s:%s", r.namespace, queueName)
res, err := r.client.BLPop(ctx, time.Second, queueKey).Result()
if err != nil && err != redis.Nil {
log.Error(err)
locker.Release(ctx)
continue
}
if len(res) < 2 {
locker.Release(ctx)
continue
}
var job *Job
_ = json.Unmarshal([]byte(res[1]), &job)
log.Debugf("processing %s args %s", queueName, job.Args)
if err := r.addItemToOnProgressQueue(ctx, job); err != nil {
log.Error(err)
locker.Release(ctx)
continue
}
locker.Release(ctx)
fn := r.jobHandler[queueName]
err = fn(job)
if err != nil {
err = r.requeueFailedJob(ctx, job)
if err != nil {
log.Error(err)
locker.Release(ctx)
continue
}
} else {
err = r.deleteItemFromOnProgressQueue(ctx, job)
if err != nil {
log.Error(err)
}
locker.Release(ctx)
}
}
}
func (r *RedisWorker) addItemToOnProgressQueue(ctx context.Context, job *Job) error {
log.Debugf("addItemToOnProgressQueue %s args %s", job.JobName, job.Args)
key := fmt.Sprintf("%s:%s:%s", r.namespace, job.JobName, onProgressSuffixKey)
// ignore the error, bcs this must be a valid struct
val, _ := job.marshal()
return backoff.Retry(r.client.RPush(ctx, key, val).Err, backoff.NewConstantBackOff(time.Millisecond))
}
func (r *RedisWorker) requeueFailedJob(ctx context.Context, job *Job) error {
log.Debugf("requeue failed job %s args %s", job.JobName, job.Args)
// ignore the error, bcs this must be a valid struct
val, _ := job.marshal()
key := fmt.Sprintf("%s:%s", r.namespace, job.JobName)
onProgressKey := fmt.Sprintf("%s:%s:%s", r.namespace, job.JobName, onProgressSuffixKey)
ops := func() error {
_, err := r.client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.RPush(ctx, key, val)
pipe.LPop(ctx, onProgressKey)
return nil
})
return err
}
return backoff.Retry(ops, backoff.NewConstantBackOff(time.Millisecond))
}
func (r *RedisWorker) deleteItemFromOnProgressQueue(ctx context.Context, job *Job) error {
log.Debugf("deleteItemFromOnProgressQueue %s args %s", job.JobName, job.Args)
onProgressKey := fmt.Sprintf("%s:%s:%s", r.namespace, job.JobName, onProgressSuffixKey)
return backoff.Retry(r.client.LPop(ctx, onProgressKey).Err, backoff.NewConstantBackOff(time.Millisecond))
}