-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis-broker.go
98 lines (90 loc) · 2.03 KB
/
redis-broker.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
package tinycelery
import (
"context"
"fmt"
"log"
"reflect"
jsoniter "github.com/json-iterator/go"
"github.com/redis/go-redis/v9"
)
var (
marshal = jsoniter.Marshal
unmarshal = jsoniter.Unmarshal
)
func newDefaultRedisBroker() *redisBroker {
return &redisBroker{
rc: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}),
queue: "tiny-celery-default",
tasks: map[string]Task{},
}
}
type redisBroker struct {
rc *redis.Client
queue string
tasks map[string]Task
}
func (b *redisBroker) send(ctx context.Context, messages ...*Message) error {
if len(messages) == 0 {
return nil
}
pipline := b.rc.Pipeline()
for _, message := range messages {
data, err := marshal(message)
if err != nil {
return err
}
res := pipline.ZAdd(ctx, b.queue, redis.Z{
Score: float64(message.ETA),
Member: data,
})
if err := res.Err(); err != nil {
return err
}
}
_, err := pipline.Exec(ctx)
return err
}
func (b *redisBroker) fetch(ctx context.Context) (*Message, error) {
maxETA := getNow().Unix() + 20
countRes := b.rc.ZCount(ctx, b.queue, "0", fmt.Sprintf("%d", maxETA))
if err := countRes.Err(); err != nil {
return nil, err
}
if countRes.Val() == 0 {
return nil, ErrMessageNotExists
}
res := b.rc.ZPopMin(ctx, b.queue, 1)
if err := res.Err(); err != nil {
return nil, err
}
data := []byte(res.Val()[0].Member.(string))
meta := &Meta{}
if err := unmarshal(data, meta); err != nil {
return nil, err
}
task, ok := b.tasks[meta.Name]
if !ok {
return nil, ErrTaskNotRegistered
}
message := &Message{
Task: reflect.New(reflect.TypeOf(task).Elem()).Interface().(Task),
}
if err := unmarshal(data, message); err != nil {
return nil, err
}
message.setDefault()
if meta.ETA > maxETA {
log.Println("task eta faraway from now, restore")
return nil, b.send(ctx, message)
}
return message, nil
}
func (b *redisBroker) getAccumulate(ctx context.Context) (uint32, error) {
res := b.rc.ZCard(ctx, b.queue)
if err := res.Err(); err != nil {
return 0, err
}
return uint32(res.Val()), nil
}