hello
目前有这样一个需求:
在init_worker阶段有定时器timer定期去获取一个名为ups的upstream块的内容(借助https://github.com/openresty/lua-upstream-nginx-module), 其内容打算放在一个全局变量中,因为之后的模块中要用到此,每个请求来的时候需要这个内容。
但用了全局变量后,发现这个变量没有被定期更新,只执行了一次。(不知道本来就是这样还是我写的有问题)。
后来用了模块级别的变量,发现这个变量被定时更新了。 但如何让外部模块也能获取其更新的内容??
(ps:不用dict可以做到么?)
--timer.lua
local _M ={
}
local ups
_M.ups=ups
_M.timer=function()
.....
ups=update()
end
return _M
---app.lua
local _M={
}
local timer=require("timer)
_M.run=function()
--here use ups
timer.ups ----- 在这里为空。。大概原因是这个时候的ups本来也就为空吧。。。
end
init_worker_by_lua_block {
local timer=requre("timer")
timer.timer()
}
content_by_lua_file {
local app=require("app")
app.run()
}