-
Notifications
You must be signed in to change notification settings - Fork 301
/
scripts.go
78 lines (74 loc) · 2.16 KB
/
scripts.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
// Code generated by generate_scripts.go. DO NOT EDIT.
package redis
import "github.com/go-redis/redis/v8"
var (
popTaskScript = redis.NewScript(`local function format_ready(xs)
local ret = { 'ready', 'id', xs[1][2][1][1] }
for i, v in ipairs(xs[1][2][1][2]) do
ret[i+3] = v
end
return ret
end
local xs = redis.call('xreadgroup', 'group', ARGV[1], ARGV[2], 'count', 1, 'streams', KEYS[1], '>')
if xs then
return format_ready(xs)
end
xs = redis.call('xreadgroup', 'group', ARGV[1], ARGV[2], 'noack', 'streams', KEYS[2], '>')
if xs then
for i, x in ipairs(xs[1][2]) do
local start_at, payload, replace
for j=1,#x[2],2 do
local name = x[2][j]
if name == 'start_at' then start_at = x[2][j+1]
elseif name == 'payload' then payload = x[2][j+1]
elseif name == 'replace' then replace = x[2][j+1]
end
end
if replace then
redis.call('zadd', KEYS[3], start_at, payload)
else
redis.call('zadd', KEYS[3], 'nx', start_at, payload)
end
end
end
local zs = redis.call('zrangebyscore', KEYS[3], '-inf', ARGV[3], 'withscores')
if #zs > 0 then
local members = {}
for i=1,#zs,2 do
local member = zs[i]
members[#members+1] = member
redis.call('xadd', KEYS[1], 'maxlen', '~', ARGV[4],'*', 'payload', member, 'start_at', zs[i+1])
end
redis.call('zrem', KEYS[3], unpack(members))
return format_ready(redis.call('xreadgroup', 'group', ARGV[1], ARGV[2], 'count', 1, 'streams', KEYS[1], '>'))
end
local ret = { 'waiting' }
zs = redis.call('zrangebyscore', KEYS[3], '-inf', '+inf', 'withscores', 'limit', 0, 1)
if #zs > 0 then
ret[#ret+1] = 'next_at'
ret[#ret+1] = zs[2]
end
xs = redis.call('xrevrange', KEYS[2], '+', '-', 'count', 1)
if #xs > 0 then
ret[#ret+1] = 'last_id'
ret[#ret+1] = xs[1][1]
end
return ret`)
xAutoClaimScript = redis.NewScript(`local xps = redis.call('xpending', KEYS[1], ARGV[1], ARGV[4], '+', ARGV[5])
if not xps then
return nil
end
local ids = {}
for _, xp in ipairs(xps) do
if xp[3] >= tonumber(ARGV[3]) then
ids[#ids+1] = xp[1]
end
end
if #ids == 0 then
return nil
end
return {
ids[#ids],
redis.call('xclaim', KEYS[1], ARGV[1], ARGV[2], ARGV[3], unpack(ids)),
}`)
)