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

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

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

具体的nginx.conf配置如下:

worker_processes  ;
error_log logs/error.log;
events {
worker_connections ;
} http {
init_worker_by_lua_block {
local delay = -- 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 == 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 ;
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=, b=}}
)
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=, b=}}},
{"/subduction", {args={a=, b=}}}
})
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=, b=}}}
})
local res2 = ngx.location.capture_multi( {
{"/subduction", {args={a=, b=}}}
})
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 = -- 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 == 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
}

openresty跑定时任务配置、ngx.timer.every接口使用的更多相关文章

  1. openresty的ngx.timer.at

    openresty的ngx.timer.at真是个强大的方法. 例如某些函数不可以在一些NGINX的执行阶段使用时,可以ngx.timer.at API 创建一个零延迟的timer,在timer中去处 ...

  2. 在Spring Boot中动态实现定时任务配置

    原文路径:https://zhuanlan.zhihu.com/p/79644891 在日常的项目开发中,往往会涉及到一些需要做到定时执行的代码,例如自动将超过24小时的未付款的单改为取消状态,自动将 ...

  3. spring 定时任务配置

    1.(易)如何在spring中配置定时任务? spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 (程序中一般我们都是到过写的,直观些) 1.定义任务 < ...

  4. spring3.0注解定时任务配置及说明

    spring注解方式的定时任务配置: 第一步:spring配置文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  5. 安装Nginx+Lua+OpenResty开发环境配置全过程实例

    安装Nginx+Lua+OpenResty开发环境配置全过程实例 OpenResty由Nginx核心加很多第三方模块组成,默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用. ...

  6. ngx_lua_API 指令详解(一)ngx.timer.at 指令

    语法: ok,err = ngx.timer.at(delay,callback,user_arg1,user_arg2 ...) 上下文: init_worker_by_lua *,set_by_l ...

  7. Openresty增加waf配置

    Openresty增加waf配置 1. Ngx lua waf 说明 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击 防止svn/备份之类文件泄漏 防止Apach ...

  8. jeecg 定时任务配置用法

    方式一: 1.定时任务配置文件 src/main/resources/spring-mvc-timeTask.xml 2.新定义一个定时任务举例 a.配置定时任务,配置文件spring-mvc-tim ...

  9. 三个标签完成springboot定时任务配置

    1. 问题描述 Java项目定时任务是必备模块,月高风黑夜跑个批处理,记录或者统计一些系统信息. 2. 解决方案: 结合springboot,只需三个标签就能完成定时任务配置. 2.1 标签1 用在s ...

随机推荐

  1. getAttribute()方法

    http://www.imooc.com/code/1587 getAttribute()方法 通过元素节点的属性名称获取属性的值. 语法: elementNode.getAttribute(name ...

  2. 开启Visual Studio 2013时,出现Microsoft.VisualStudio.Web.PasteJson.JsonPackage无法载入的可能解決方案

    1.先下载:http://www.jb51.net/dll/Microsoft.VisualStudio.Web.PasteJson.dll.html Microsoft.VisualStudio.W ...

  3. Java反射机制的基本概念与使用

    本篇文章分为以下几个部分: 1.认识反射 2.反射的源头(Class类) 3.利用反射操作构造方法 4.利用反射调用类中的方法 5.反射中的invoke方法 6.利用反射调用类中的属性 反射在我们普通 ...

  4. js实现div的置底

    //-------------置底的div---------------------- <div class="mui-content lv-mrcd"  id=" ...

  5. C++ 类的抽象初练

    /* 某商店经销一种货物,货物的购进和卖出以箱为单位,各箱的重量不一样, 因此商店需要目前库存的总重量. 现在用c++模拟商店货物购进和卖出的情况 */ #include<iostream> ...

  6. TrustZone——开源库—Linaro—OP-TEE

    想研究安全系统源代码的有福气了.曾经OVOS的代码缺少TA相关的实现. 这次的版本号,基本框架都有了.先看看架构图吧. 几家大公司做的,可能是ST牵头.页面有ST的LOGO. 代码质量较高. 未来也会 ...

  7. [css]解决iframe在ios设备上无法滚动

    原因: safari的webkit内核特性 解决方案: 在iframe外包裹一层div并另外设置其css属性为如下: -webkit-overflow-scrolling:touch; overflo ...

  8. Oracel 数据库面试题

    1.取出表中第31到40行的记录mysql方案: , oracle方案: select t2.* ) t2 2.truncate和delete有什么区别TRUNCATE TABLE在功能上与不带WHE ...

  9. Loadrunner_http长连接设置

    最近协助同事解决了几个问题,也对loadrunner的一些设置加深了理解,关键是更加知其所以然. ljonathan http://www.51testing.com/html/48/202848-2 ...

  10. boost实用工具:typeof库 BOOST_TYPE BOOST_AUTO

    boost::typeof库中使用宏BOOST_TYPE和BOOST_AUTO来模拟C++11关键字typeof和auto  C++ Code  123456789101112131415161718 ...