openresty 定時器

【1】nginx定時器應用nginx

(1)文件目錄結構ui

(2)nginx.conf配置lua

lua_package_path  "/usr/local/lib/ubcsrvd/lualib/?.lua;;"; init_worker_by_lua_file work/bill_timer.lua;

(3)配置定時器時間spa

公共配置文件config.luacode

-- 公共配置文件

local _M = {} -- 吃飯定時器(3m)
_M.eat_timer_interval = 180
    
-- 其餘 -- TO DO ....

return _M

(4)定時器應用代碼blog

-- 定時器應用示例

    local cfg = require("config") local new_timer = ngx.timer.at local function timer_eat(premature) ngx.log(ngx.ERR, "premature : " .. ((premature and {"true"} or {"false"})[1])) if not premature then ngx.log(ngx.ERR, "timer to do eat .....") local rtn, err = new_timer(cfg.eat_timer_interval, timer_eat) if not rtn then ngx.log(ngx.ERR, "failed to create timer to eat : ", err) else ngx.log(ngx.ERR, "success to create timer to eat interval(s) : " .. cfg.eat_timer_interval) end
        end
    end
 
    if 0 == ngx.worker.id() then
        local rtn, err = new_timer(cfg.eat_timer_interval, timer_eat) if not rtn then ngx.log(ngx.ERR, "failed to create timer to eat : ", err) else ngx.log(ngx.ERR, "success to create timer to eat interval(s) : " .. cfg.eat_timer_interval) end
    end

(5)0 == ngx.worker.id()接口

注意:本地nginx啓動後,工做進程配置的是3個。進程

ngx.worker.id() 爲0 表示第一個工做進程。即這個定時器由進程1管理執行。rem

若想啓動多個定時器,且分別由不一樣的進程管理執行,能夠使用ngx.worker.id()爲1或2等等其餘進程。it

固然,具體使用那個進程(必須知足:ngx.worker.id() < worker_processes)須要根據nginx.conf配置文件中worker_processes字段具體配置的工做進程個數。

【2】參數premature應用

(1)場景1:啓動nginx

(2)場景2:關閉nginx

(3)場景3:重啓nginx

(4)場景4:加載nginx配置 reload

(5)其餘場景

【3】總結

nginx定時器應用示例如上。

執行定時器任務接口,增長premature參數,結合具體場景分析過程。

 

Good Good Study, Day Day Up.

順序 選擇 循環 總結

相關文章
相關標籤/搜索