意图将某个定时器只绑定到一个worker上,于是如下配置
nginx.conf中配置 init_worker_by_lua_file init_worker.lua
在init_worker.lua中:
if ngx.worker.id() ~= 0 then return end
--定义定时器--
local hander
hander = function (premature)
if premature then
log(ngx.ALERT, 'worker exiting. gooebye, timer.')
return
end
ngx.timer.at(5, hander)
log(ngx.ALERT, 'timer is running now at worker : ' .. ngx.worker.pid())
--调用定时器--
ngx.timer.at(0, hander)
结果发现同时运行了两个timer,分别位于第一个worker进程和cache manager进程
2016/01/05 16:07:07 [alert] 16823#0: [lua] init_master.lua:17: log(): timer is running now at worker : 16823, context: ngx.timer
2016/01/05 16:07:07 [alert] 16831#0: [lua] init_master.lua:17: log(): timer is running now at worker : 16831, context: ngx.timer
请教一下,在ngx.worker.id()相同的情况下,如何区分呢?