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
}

最新文章

  1. [PDO绑定参数]使用PHP的PDO扩展进行批量更新操作
  2. markdown简要说明显示样式
  3. 64位 SQL Server2008链接访问Oracle 过程汇总解决方法记录
  4. struct内存对齐1:gcc与VC的差别
  5. hdu 1213 How Many Tables 解题报告
  6. Hibernate 代码生成器
  7. mysql 安全
  8. asp显示多条记录的代码
  9. vs开发工具之--自动生成注释
  10. Windows Live Writer的Markdown插件MarkdownInLiveWriter支持语法高亮了
  11. 微信OPENID授权方法
  12. iOS自动自动隐藏软键盘
  13. HDU4815/计数DP
  14. python 中如何导入一个自己创建的模块
  15. RabbitMQ在Windows环境下的安装与使用
  16. [C++ Primer Plus] 第10章、对象和类(二)课后习题
  17. 【转】vmware 安装 osx 无法登录 appstore 的解决办法 (伪造smbios设备信息)
  18. DTO转DOMAIN动态转换类。
  19. SQL中的between and
  20. centos服务器删除/usr目录怎么办

热门文章

  1. windows下wim配置成IDE
  2. [uboot]uboot如何引导系统
  3. UCOS2系统内核讲述(五)_初始化TCB详情
  4. MapReduce总体架构分析
  5. selenium使用中遇到的问题
  6. TensorFlow基础笔记(3) cifar10 分类学习
  7. qtcreator 中文乱码
  8. Yii2中Html的使用
  9. Ubuntu下Eclipse无法添加Tomcat7解决方法
  10. ps -ef|grep htpd|wd -l