Skip to content

Commit 965c631

Browse files
committed
Initial stab at loading scripts
0 parents  commit 965c631

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "qless-core"]
2+
path = qless-core
3+
url = https://github.com/seomoz/qless-core.git

lib/resty/qless.lua

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
local ffi = require "ffi"
2+
local redis_mod = require "resty.redis"
3+
4+
local qless_luascript = require "resty.qless.luascript"
5+
local qless_queues = require "resty.qless.queues"
6+
7+
local ngx_now = ngx.now
8+
local ngx_log = ngx.log
9+
local ngx_DEBUG = ngx.DEBUG
10+
local ngx_ERR = ngx.ERR
11+
local math_floor = math.floor
12+
local ffi_cdef = ffi.cdef
13+
local ffi_new = ffi.new
14+
local ffi_string = ffi.string
15+
local C = ffi.C
16+
17+
18+
ffi_cdef[[
19+
typedef unsigned char u_char;
20+
u_char * ngx_hex_dump(u_char *dst, const u_char *src, size_t len);
21+
int RAND_pseudo_bytes(u_char *buf, int num);
22+
]]
23+
24+
25+
local function random_hex(len)
26+
local len = math_floor(len / 2)
27+
28+
local bytes = ffi_new("uint8_t[?]", len)
29+
C.RAND_pseudo_bytes(bytes, len)
30+
if not bytes then
31+
ngx_log(ngx_ERR, "error getting random bytes via FFI")
32+
return nil
33+
end
34+
35+
local hex = ffi_new("uint8_t[?]", len * 2)
36+
C.ngx_hex_dump(hex, bytes, len)
37+
return ffi_string(hex, len * 2)
38+
end
39+
40+
41+
local _M = {
42+
_VERSION = '0.01',
43+
}
44+
45+
local mt = { __index = _M }
46+
47+
local OPTION_DEFAULTS = {
48+
redis = {
49+
host = "127.0.0.1",
50+
port = 6379,
51+
connect_timeout = 100,
52+
read_timeout = 5000,
53+
}
54+
}
55+
56+
57+
function _M.new(options)
58+
if not options then options = {} end
59+
setmetatable(options, { __index = OPTION_DEFAULTS })
60+
setmetatable(options.redis, { __index = OPTION_DEFAULTS.redis })
61+
62+
local redis = redis_mod:new()
63+
redis:set_timeout(options.redis.connect_timeout)
64+
local ok, err = redis:connect(options.redis.host, options.redis.port)
65+
if not ok then
66+
ngx_log(ngx_ERR, err)
67+
else
68+
redis:set_timeout(options.redis.read_timeout)
69+
end
70+
71+
return setmetatable({
72+
redis = redis,
73+
worker_name = random_hex(8),
74+
luascript = qless_luascript.new("qless", redis),
75+
}, mt)
76+
end
77+
78+
79+
function _M.generate_jid(self)
80+
return random_hex(32)
81+
end
82+
83+
84+
function _M.call(self, command, ...)
85+
self.luascript:call(command, ngx_now(), select(1, ...))
86+
end
87+
88+
89+
return _M

lib/resty/qless/luascript.lua

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
local ngx_log = ngx.log
2+
local ngx_DEBUG = ngx.DEBUG
3+
local ngx_ERR = ngx.ERR
4+
local ngx_sha1_bin = ngx.sha1_bin
5+
local str_gsub = string.gsub
6+
local str_format = string.format
7+
local str_byte = string.byte
8+
9+
10+
local _M = {
11+
_VERSION = '0.01',
12+
}
13+
14+
local mt = { __index = _M }
15+
16+
17+
-- Load the qless scripts and generate the sha1 digest.
18+
local f = assert(io.open("/home/jhurst/prj/lua-resty-qless/qless-core/qless.lua", "r"))
19+
local qless_script = f:read("*all")
20+
local qless_script_sha1 = ngx_sha1_bin(qless_script)
21+
local qless_script_sha1_sum = str_gsub(qless_script_sha1, "(.)",
22+
function (c)
23+
return str_format("%02x%s", str_byte(c), "")
24+
end)
25+
26+
27+
function _M.new(name, redis)
28+
return setmetatable({
29+
name = name,
30+
redis = redis,
31+
sha = qless_script_sha1_sum,
32+
}, mt)
33+
end
34+
35+
36+
function _M.reload(self)
37+
self.sha = self.redis:script("load", qless_script)
38+
end
39+
40+
41+
function _M.call(self, ...)
42+
local res, err = self.redis:evalsha(self.sha, select(1, ...))
43+
if not res and err == "NOSCRIPT No matching script. Please use EVAL." then
44+
self:reload()
45+
res, err = self.redis:evalsha(self.sha, select(1, ...))
46+
end
47+
return res, err
48+
end
49+
50+
51+
return _M

lib/resty/qless/queues.lua

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local ngx_log = ngx.log
2+
local ngx_DEBUG = ngx.DEBUG
3+
local ngx_ERR = ngx.ERR
4+
5+
6+
local _M = {
7+
_VERSION = '0.01',
8+
}
9+
10+
local mt = { __index = _M }
11+
12+
13+
function _M.new(qless)
14+
return setmetatable({ qless = qless }, mt)
15+
end
16+
17+
return _M

qless-core

Submodule qless-core added at f7ef735

0 commit comments

Comments
 (0)