-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.lua
More file actions
66 lines (58 loc) · 1.51 KB
/
events.lua
File metadata and controls
66 lines (58 loc) · 1.51 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
local cjson = require "cjson"
local _M = {}
-- luacheck: globals ngx
_M.post = function(red, stream, data)
local kvs = { stream, "*" }
for k, v in pairs(data) do
table.insert(kvs, k)
table.insert(kvs, v)
end
red:xadd(unpack(kvs))
end
local function parse_xread_response (rr)
local evs = {}
-- only 1 stream
local ourstream = rr[1]
-- 1st element is stream name, 2nd is data
local ourevents = ourstream[2]
for i = 1, #ourevents do
local r_ev = ourevents[i][2]
local ev = {}
for n = 1, #r_ev, 2 do
ev[r_ev[n]] = r_ev[n+1]
end
table.insert(evs, ev)
end
-- last item has last id
local lastevent = ourevents[#ourevents]
-- 1st element is id, 2nd is data
local last_id = lastevent[1]
return evs, last_id
end
_M.str = function(evs, last_id)
if #evs == 0 then
return '{"data":[],"last_id":"' .. (last_id or "0-0") .. '"}'
end
return cjson.encode({data = evs, last_id = last_id})
end
_M.get = function(red, stream, opts)
opts = opts or {}
assert(type(stream) == "string", "stream name must be string")
local rargs = {}
if opts.wait then
table.insert(rargs, "BLOCK")
table.insert(rargs, "30000")
end
table.insert(rargs, "COUNT")
table.insert(rargs, opts.count or 100)
table.insert(rargs, "STREAMS")
table.insert(rargs, stream)
table.insert(rargs, opts.after or "0-0")
local rr = red:xread(unpack(rargs))
if rr == ngx.null then
return {}, opts.after
end
local evs, last_id = parse_xread_response(rr)
return evs, last_id
end
return _M