1. 程式人生 > >ngx.timer.at

ngx.timer.at

color timer cal then spa tex 執行 -- span

一、只執行一次

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