Skip to content

Commit d0e0409

Browse files
authored
fix
1 parent 6bbbaac commit d0e0409

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

backend/redis/queue.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ func newTaskQueue[T any](rdb redis.UniversalClient, tasktype string) (*taskQueue
5050
"completeCmd": completeCmd.Load(context.Background(), rdb),
5151
}
5252

53-
// Create the consumer group
54-
_, err := createGroupCmd.Run(context.Background(), rdb, []string{tq.streamKey, tq.groupName}).Result()
55-
if err != nil {
56-
return nil, fmt.Errorf("creating task queue: %w", err)
57-
}
58-
5953
for name, cmd := range cmds {
6054
// fmt.Println(name, cmd.Val())
6155

@@ -64,6 +58,12 @@ func newTaskQueue[T any](rdb redis.UniversalClient, tasktype string) (*taskQueue
6458
}
6559
}
6660

61+
// Create the consumer group
62+
_, err := createGroupCmd.Run(context.Background(), rdb, []string{tq.streamKey, tq.groupName}).Result()
63+
if err != nil {
64+
return nil, fmt.Errorf("creating task queue: %w", err)
65+
}
66+
6767
return tq, nil
6868
}
6969

@@ -89,22 +89,33 @@ var enqueueCmd = redis.NewScript(
8989
`)
9090

9191
var createGroupCmd = redis.NewScript(`
92-
local streamKey = KEYS[1]
93-
local groupName = KEYS[2]
94-
local status, groups = pcall(redis.call, 'XINFO', 'GROUPS', streamKey)
95-
96-
if status then
97-
for i = 1, #groups do
98-
if groups[i].name == groupName then
99-
return false
100-
end
101-
end
102-
end
103-
104-
redis.call('XGROUP', 'CREATE', streamKey, groupName, '0', 'MKSTREAM')
105-
return true
92+
local streamKey = KEYS[1]
93+
local groupName = KEYS[2]
94+
local exists = false
95+
local res = redis.pcall('XINFO', 'GROUPS', streamKey)
96+
97+
if res and type(res) == 'table' then
98+
for _, groupInfo in ipairs(res) do
99+
if type(groupInfo) == 'table' then
100+
for i = 1, #groupInfo, 2 do
101+
if groupInfo[i] == 'name' and groupInfo[i+1] == groupName then
102+
exists = true
103+
break
104+
end
105+
end
106+
end
107+
if exists then break end
108+
end
109+
end
110+
111+
if not exists then
112+
redis.pcall('XGROUP', 'CREATE', streamKey, groupName, '$', 'MKSTREAM')
113+
end
114+
115+
return true
106116
`)
107117

118+
108119
func (q *taskQueue[T]) Enqueue(ctx context.Context, p redis.Pipeliner, id string, data *T) error {
109120
ds, err := json.Marshal(data)
110121
if err != nil {

0 commit comments

Comments
 (0)