ngx.timer.at

Posted Tinywan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器