openresty跑定時任務配置、ngx.timer.every接口使用

openresty的定時任務是要跟worker綁定的。若是不綁定特定的worker,那麼全部啓動的woker都會去執行定時任務。html

通常狀況下默認綁定worker_id=0的,這樣在nginx整個進程裏面,就只執行一個timer。nginx

在conf中具體的位置能夠寫本身的任務邏輯。app

具體的nginx.conf配置以下:lua

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}


http {
    init_worker_by_lua_block { local delay = 2  -- in seconds local new_timer = ngx.timer.at local log = ngx.log local ERR = ngx.ERR local check check = function(premature) if not premature then -- do the health check or other routine work log(ERR, "mm test mm test") local ok, err = new_timer(delay, check) if not ok then log(ERR, "failed to create timer: ", err) return end end end if 0 == ngx.worker.id() then local ok, err = new_timer(delay, check) if not ok then log(ERR, "failed to create timer: ", err) return end end }

    server {
        listen 8081;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }

        location = /app/test {
            content_by_lua_block {
                local res = ngx.location.capture(
                                "/sum", {args={a=3, b=8}}
                                )
                ngx.say("status:", res.status, " response:", res.body)
            }
        }
        location = /sum {
            internal;
            content_by_lua_block {
                ngx.sleep(0.1)
                local args = ngx.req.get_uri_args()
                ngx.print(tonumber(args.a) + tonumber(args.b))
            }
        }

        location = /subduction {
            internal;
            content_by_lua_block {
                ngx.sleep(0.1)
                local args = ngx.req.get_uri_args()
                ngx.print(tonumber(args.a) - tonumber(args.b))
            }
        }

        location = /app/test_parallels {
            content_by_lua_block {
                local start_time = ngx.now()
                local res1, res2 = ngx.location.capture_multi( {
                                {"/sum", {args={a=3, b=8}}},
                                {"/subduction", {args={a=3, b=8}}}
                            })
                ngx.say("status:", res1.status, " response:", res1.body)
                ngx.say("status:", res2.status, " response:", res2.body)
                ngx.say("time used:", ngx.now() - start_time)
            }
        }

        location = /app/test_queue {
            content_by_lua_block {
                local start_time = ngx.now()
                local res1 = ngx.location.capture_multi( {
                                {"/sum", {args={a=3, b=8}}}
                            })
                local res2 = ngx.location.capture_multi( {
                                {"/subduction", {args={a=3, b=8}}}
                            })
                ngx.say("status:", res1.status, " response:", res1.body)
                ngx.say("status:", res2.status, " response:", res2.body)
                ngx.say("time used:", ngx.now() - start_time)
            }
        }
}
}

 

注意init_worker_by_lua_block是放在http裏面的。由於此處只配置了error.log,所以是打印的err級別的日誌,方便觀察。spa

接下來啓動ngin:sudo nginx -p `pwd`/ -c conf/nginx.confrest

而後tailf logs/error.log:日誌

追日誌會發現,每隔2s就會打印一條日誌。code

 2、使用ngx.timer.every接口server

ngx提供了最新的ngx.timer.every接口,再來試一下:htm

    init_worker_by_lua_block {
         local delay = 2  -- in seconds
         -- local new_timer = ngx.timer.at
         local log = ngx.log
         local ERR = ngx.ERR
         local check

         check = function(premature)
             if not premature then
                 -- do the health check or other routine work
                 log(ERR, "mm test mm test")
                 -- local ok, err = new_timer(delay, check)
                 -- if not ok then
                 --     log(ERR, "failed to create timer: ", err)
                 --     return
                 --  end
             end
         end

         if 0 == ngx.worker.id() then
             local ok, err = ngx.timer.every(delay, check)
             if not ok then
                 log(ERR, "failed to create timer: ", err)
                 return
             end
         end
    }
相關文章
相關標籤/搜索