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

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

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

具体的nginx.conf配置如下:

  1. worker_processes ;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections ;
  5. }
  6.  
  7. http {
  8. init_worker_by_lua_block {
  9. local delay = -- in seconds
  10. local new_timer = ngx.timer.at
  11. local log = ngx.log
  12. local ERR = ngx.ERR
  13. local check
  14.  
  15. check = function(premature)
  16. if not premature then
  17. -- do the health check or other routine work
  18. log(ERR, "mm test mm test")
  19. local ok, err = new_timer(delay, check)
  20. if not ok then
  21. log(ERR, "failed to create timer: ", err)
  22. return
  23. end
  24. end
  25. end
  26.  
  27. if == ngx.worker.id() then
  28. local ok, err = new_timer(delay, check)
  29. if not ok then
  30. log(ERR, "failed to create timer: ", err)
  31. return
  32. end
  33. end
  34. }
  35.  
  36. server {
  37. listen ;
  38. location / {
  39. default_type text/html;
  40. content_by_lua '
  41. ngx.say("<p>hello, world</p>")
  42. ';
  43. }
  44.  
  45. location = /app/test {
  46. content_by_lua_block {
  47. local res = ngx.location.capture(
  48. "/sum", {args={a=, b=}}
  49. )
  50. ngx.say("status:", res.status, " response:", res.body)
  51. }
  52. }
  53. location = /sum {
  54. internal;
  55. content_by_lua_block {
  56. ngx.sleep(0.1)
  57. local args = ngx.req.get_uri_args()
  58. ngx.print(tonumber(args.a) + tonumber(args.b))
  59. }
  60. }
  61.  
  62. location = /subduction {
  63. internal;
  64. content_by_lua_block {
  65. ngx.sleep(0.1)
  66. local args = ngx.req.get_uri_args()
  67. ngx.print(tonumber(args.a) - tonumber(args.b))
  68. }
  69. }
  70.  
  71. location = /app/test_parallels {
  72. content_by_lua_block {
  73. local start_time = ngx.now()
  74. local res1, res2 = ngx.location.capture_multi( {
  75. {"/sum", {args={a=, b=}}},
  76. {"/subduction", {args={a=, b=}}}
  77. })
  78. ngx.say("status:", res1.status, " response:", res1.body)
  79. ngx.say("status:", res2.status, " response:", res2.body)
  80. ngx.say("time used:", ngx.now() - start_time)
  81. }
  82. }
  83.  
  84. location = /app/test_queue {
  85. content_by_lua_block {
  86. local start_time = ngx.now()
  87. local res1 = ngx.location.capture_multi( {
  88. {"/sum", {args={a=, b=}}}
  89. })
  90. local res2 = ngx.location.capture_multi( {
  91. {"/subduction", {args={a=, b=}}}
  92. })
  93. ngx.say("status:", res1.status, " response:", res1.body)
  94. ngx.say("status:", res2.status, " response:", res2.body)
  95. ngx.say("time used:", ngx.now() - start_time)
  96. }
  97. }
  98. }
  99. }

注意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接口,再来试一下:

  1. init_worker_by_lua_block {
  2. local delay = -- in seconds
  3. -- local new_timer = ngx.timer.at
  4. local log = ngx.log
  5. local ERR = ngx.ERR
  6. local check
  7.  
  8. check = function(premature)
  9. if not premature then
  10. -- do the health check or other routine work
  11. log(ERR, "mm test mm test")
  12. -- local ok, err = new_timer(delay, check)
  13. -- if not ok then
  14. -- log(ERR, "failed to create timer: ", err)
  15. -- return
  16. -- end
  17. end
  18. end
  19.  
  20. if == ngx.worker.id() then
  21. local ok, err = ngx.timer.every(delay, check)
  22. if not ok then
  23. log(ERR, "failed to create timer: ", err)
  24. return
  25. end
  26. end
  27. }

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. Makefile学习之路5——通过函数增强功能

    通过函数能显著增强Makefile的功能.对于simple项目的Makefile,尽管使用了模式规则,但还是有一件比较麻烦的事情,就是要在Makefile中指明每一个项目源文件.下面介绍几个后期会使用 ...

  2. SQL select查询原理--查询语句执行原则<转>

    1.单表查询:根据WHERE条件过滤表中的记录,形成中间表(这个中间表对用户是不可见的):然后根据SELECT的选择列选择相应的列进行返回最终结果. 1)简单的单表查询 SELECT 字段 FROM ...

  3. 树莓派系统Raspbian安装小结

    是有界面的系统. NOOBS, our easy installer for Raspbian  基于debian NOOBS stands for New Out Of Box Software h ...

  4. 组合数学 - BZOJ 3997 - TJOI2015

    TJOI2015 Problem's Link ---------------------------------------------------------------------------- ...

  5. GIMP 使用

    在更改uboot启动logo的时候,需要P图,使用了linux的gimp.本文记录如何更改图片大小以及居中显示. 设置画布大小 在图片中右键 image -> Canvas Size 图片居中 ...

  6. 如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查

    如果可能的话,使用 PC-Lint.LogiScope 等工具进行代码审查. #include <iostream> #include <algorithm> #include ...

  7. Codeforces Round #265 (Div. 2)

    http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...

  8. Spring Boot注解(annotation)列表

    (1)@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,这个配置等同于: @Configuration ,@EnableAutoConfigurati ...

  9. SQLserver 设置自增为显式插入

    默认是状态是set IDENTITY_INSERT T2 off ,就是关闭了自动插入值的功能,为空时就会报错 ,,'') 报错: 消息 544,级别 16,状态 1,第 1 行当 IDENTITY_ ...

  10. C++之类模板

    C++之类模板 代码如下:  C++ Code  123456789101112131415161718192021222324252627282930313233343536373839404142 ...