首页 > 代码库 > ngx.timer.at
ngx.timer.at
一、只执行一次
nginx.conf
location /ngx_timer_at { default_type ‘text/html‘; lua_code_cache off; content_by_lua_file /home/tinywan/Openresty_Protect/First_Protect/lua/get_timer_at.lua; }
get_timer_at.lua 文件
local delay = 5 local handler handler = function (premature,param) -- do some routine job in Lua just like a cron job if premature then return end ngx.log(ngx.ERR, "param is : ", param) end local ok, err = ngx.timer.at(delay, handler,"Hello Tinywan")
执行请求后:curl http://127.0.0.1/ngx_timer_at 执行 5 s 后打印以下内容在日志文件中
tail -f error.log
2017/05/04 23:24:38 [error] 95933#0: *433016 [lua] get_timer_at.lua:9: param is : Hello Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80
二、不停的循环,和while 一样的结果
local delay = 5 local handler handler = function (premature,param) -- do some routine job in Lua just like a cron job if premature then return end ngx.log(ngx.ERR, "param is : ", param) ngx.timer.at(delay, handler,"again run... Tinywan") end local ok, err = ngx.timer.at(delay, handler,"Hello Tinywan")
执行请求后:curl http://127.0.0.1/ngx_timer_at 执行 5 s 后打印以下内容在日志文件中
2017/05/04 23:34:53 [error] 96020#0: *437080 [lua] get_timer_at.lua:9: param is : Hello Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:34:58 [error] 96020#0: *437123 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:03 [error] 96020#0: *437152 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:08 [error] 96020#0: *437195 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80 2017/05/04 23:35:13 [error] 96020#0: *437224 [lua] get_timer_at.lua:9: param is : again run... Tinywan, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:80
ngx.timer.at
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。