-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_work.lua
More file actions
132 lines (108 loc) · 2.95 KB
/
init_work.lua
File metadata and controls
132 lines (108 loc) · 2.95 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
local _M
local delay = 5
local handler
local log = ngx.log
local ERR = ngx.ERR
local redis = require("resty.redis")
local red
local confstr
local var = require("workvar")
local function errlog(...)
log(ERR, "InitWork:", ...)
end
-- 从redis中读取配置
local function getConfigByRedis()
red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6391)
if not ok then
errlog("redis connect faild:" .. err)
return
end
local _conf, err = red:get("ngx.conf")
if err then
errlog("redis faild to get conf" .. err)
return
end
if not _conf then
errlog("redis get conf is nil")
return
end
confstr = _conf
local ok, err = red:set_keepalive(10000, 100)
if not ok then
errlog("redis set keepalive " .. err)
end
end
-- 从文本文件中读取配置
local function getConfigByFile()
end
-- 往共享内存中塞数据
local function set2Cache(key, value, exptime)
if not exptime then
exptime = 0
end
local cache_ngx = ngx.shared.globle_cache
local succ, err, forcible = cache_ngx:set(key, value, exptime)
-- errlog("---" .. err)
return succ
end
-- 解析配置
local function parseConf()
if not confstr then
errlog("ngx conf is nil")
return
end
local cjson = require("cjson")
local confobj = cjson.decode(confstr)
-- 检测配置是否为空或长度为0
if not confobj and #confobj == 0 then
errlog("ngx conf is invalid:" .. confstr)
return
end
local _newcnf = {}
for i = 1, #confobj do
local _cnf = confobj[i]
if not _cnf.expr or _cnf.expr == "" then
_cnf.expr = "="
end
if not _newcnf[_cnf.servername] then
_newcnf[_cnf.servername] = {}
end
if not _newcnf[_cnf.servername][_cnf.expr] then
_newcnf[_cnf.servername][_cnf.expr] = {}
end
-- table.insert(_newcnf[_cnf.servername][_cnf.expr], _cnf.url)
_newcnf[_cnf.servername][_cnf.expr][_cnf.url] = true
_newcnf[_cnf.servername][_cnf.url] = _cnf
end
var.setGlobleCnf(_newcnf)
-- for sname, vobj in pairs(_newcnf) do
-- -- k 是expr或url,v是数组或者_cnf
-- for k, v in pairs(vobj) do
-- -- errlog(sname .. k .. cjson.encode(v))
-- set2Cache(sname .. k, v)
-- end
-- end
end
handler = function(premature)
if not premature then
ngx.log(ngx.ERR, os.date("%Y-%m-%d %H:%M:%S", ngx.time()).." create timer success!")
getConfigByRedis()
parseConf()
local ok,err = ngx.timer.at(delay, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create timer:", err)
end
end
end
function init_work()
local ok,err = ngx.timer.at(delay, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create timer:", err)
end
end
_M = {
init_work = init_work,
}
return _M