-- 添加触发器
addTrigger(roleId, triggerId, interval)
-- 设置一个interval过期的key,并设置一个interval+10的参数过期key
local globals = ngx.shared.GLOBALS
local redis = require "resty.redis"
local redisInf = loadMod('redisInf')
local lockKey = 'trigger.lock'
local lockInterval = 90
function accept()
if globals:safe_add(lockKey, true, lockInterval) then
ngx.thread.spawn(function()
receive()
end)
util:debug('aaaaaaa', 'accept')
else
util:debug('aaaaaaa', 'replace')
globals:replace(lockKey, true, lockInterval)
end
end
function receive()
local client = redis:new()
client:set_timeout(3000)
client:connect(redisInf.HOST, redisInf.PORT)
local result = client:psubscribe('__key*__:*')
util:debug('xxxxxxxxx', result)
if not result then
return
end
client:set_timeout(1080000)
while true do
local res, err = client:read_reply()
if not res and err ~= 'timeout' then
util.file:append('/tmp/redis.log', 'err:' .. tostring(err))
break
end
if res then -- 返回数据样本 ["pmessage","__key*__:*","__keyevent@7__:expired","roleId.1848.tollgateId.410406"]
local matchs = ngx.re.match(res[3], '^__keyevent@([0-9]+?)__:expired$')
if matchs ~= ngx.null and matchs ~= nil then
local dbIndex = matchs[1]
matchs = ngx.re.match(res[4], '^roleId.([0-9]+?).triggerId.([0-9]+)$')
if matchs ~= ngx.null and matchs ~= nil then
--- 这里重新调用loadMod的原因是在while true 里 懒加载(热更新 shared + version file)
local util = loadMod('shark.util')
util.file:append('/tmp/redis.log', 'roleId: ' .. matchs[1] .. ', triggerId: ' .. matchs[2] .. '\n')
--- 调用不同的 trigger 触发器,如果是ngx.timer.at的话就不能用懒加载模式了
--- 只能用子请求,ngx.location.capture 来处理了
end
end
end
end
client:close()
globals:delete(lockKey)
end