第一个参数 delay 以秒为单位指定计时器的延迟时间,支持分秒设置,比如 0.001 在这里表示 1 毫秒延迟。delay 同样可以设置为 0 ,此时如果当前句柄正被唤醒则计时器将立即获得执行。(in which case the timer will immediately expire when the current handler yields execution.//TODO yields)
location/ {...log_by_lua_block {localfunctionpush_data(premature,uri,args,status) -- push the data uri, args, and status to the remote -- via ngx.socket.tcp or ngx.socket.udp -- (one may want to buffer the data in Lua a bit to -- save I/O operations)endlocalok, err=ngx.timer.at(0, push_data,ngx.var.uri, ngx.var.args, ngx.header.status)ifnotokthenngx.log(ngx.ERR, "failed to create timer: ", err)returnend }}
local delay = 5
local handler
handler = function (premature)
-- do some routine job in Lua just like a cron job
if premature then
return
end
local ok, err = ngx.timer.at(delay, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create the timer: ", err)
return
end
end
local ok, err = ngx.timer.at(delay, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create the timer: ", err)
return
end