openresty跑定时任务配置、ngx.timer.every接口使用

openresty的定时任务是要跟worker绑定的。如果不绑定特定的worker,那么所有启动的woker都会去执行定时任务。

一般情况下默认绑定worker_id=0的,这样在nginx整个进程里面,就只执行一个timer。

在conf中具体的位置可以写自己的任务逻辑。

具体的nginx.conf配置如下:

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级别的日志,方便观察。

接下来启动ngin:sudo nginx -p `pwd`/ -c conf/nginx.conf

然后tailf logs/error.log:

追日志会发现,每隔2s就会打印一条日志。

 二、使用ngx.timer.every接口

ngx提供了最新的ngx.timer.every接口,再来试一下:

    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
    }

 

转载于:https://www.cnblogs.com/sonofelice/p/8259712.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值