openresty跑定时任务配置、ngx.timer.every接口使用
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接口使用的更多相关文章
- openresty的ngx.timer.at
openresty的ngx.timer.at真是个强大的方法. 例如某些函数不可以在一些NGINX的执行阶段使用时,可以ngx.timer.at API 创建一个零延迟的timer,在timer中去处 ...
- 在Spring Boot中动态实现定时任务配置
原文路径:https://zhuanlan.zhihu.com/p/79644891 在日常的项目开发中,往往会涉及到一些需要做到定时执行的代码,例如自动将超过24小时的未付款的单改为取消状态,自动将 ...
- spring 定时任务配置
1.(易)如何在spring中配置定时任务? spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 (程序中一般我们都是到过写的,直观些) 1.定义任务 < ...
- spring3.0注解定时任务配置及说明
spring注解方式的定时任务配置: 第一步:spring配置文件 <?xml version="1.0" encoding="UTF-8"?> & ...
- 安装Nginx+Lua+OpenResty开发环境配置全过程实例
安装Nginx+Lua+OpenResty开发环境配置全过程实例 OpenResty由Nginx核心加很多第三方模块组成,默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用. ...
- ngx_lua_API 指令详解(一)ngx.timer.at 指令
语法: ok,err = ngx.timer.at(delay,callback,user_arg1,user_arg2 ...) 上下文: init_worker_by_lua *,set_by_l ...
- Openresty增加waf配置
Openresty增加waf配置 1. Ngx lua waf 说明 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击 防止svn/备份之类文件泄漏 防止Apach ...
- jeecg 定时任务配置用法
方式一: 1.定时任务配置文件 src/main/resources/spring-mvc-timeTask.xml 2.新定义一个定时任务举例 a.配置定时任务,配置文件spring-mvc-tim ...
- 三个标签完成springboot定时任务配置
1. 问题描述 Java项目定时任务是必备模块,月高风黑夜跑个批处理,记录或者统计一些系统信息. 2. 解决方案: 结合springboot,只需三个标签就能完成定时任务配置. 2.1 标签1 用在s ...
随机推荐
- Makefile学习之路5——通过函数增强功能
通过函数能显著增强Makefile的功能.对于simple项目的Makefile,尽管使用了模式规则,但还是有一件比较麻烦的事情,就是要在Makefile中指明每一个项目源文件.下面介绍几个后期会使用 ...
- SQL select查询原理--查询语句执行原则<转>
1.单表查询:根据WHERE条件过滤表中的记录,形成中间表(这个中间表对用户是不可见的):然后根据SELECT的选择列选择相应的列进行返回最终结果. 1)简单的单表查询 SELECT 字段 FROM ...
- 树莓派系统Raspbian安装小结
是有界面的系统. NOOBS, our easy installer for Raspbian 基于debian NOOBS stands for New Out Of Box Software h ...
- 组合数学 - BZOJ 3997 - TJOI2015
TJOI2015 Problem's Link ---------------------------------------------------------------------------- ...
- GIMP 使用
在更改uboot启动logo的时候,需要P图,使用了linux的gimp.本文记录如何更改图片大小以及居中显示. 设置画布大小 在图片中右键 image -> Canvas Size 图片居中 ...
- 如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查
如果可能的话,使用 PC-Lint.LogiScope 等工具进行代码审查. #include <iostream> #include <algorithm> #include ...
- Codeforces Round #265 (Div. 2)
http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...
- Spring Boot注解(annotation)列表
(1)@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,这个配置等同于: @Configuration ,@EnableAutoConfigurati ...
- SQLserver 设置自增为显式插入
默认是状态是set IDENTITY_INSERT T2 off ,就是关闭了自动插入值的功能,为空时就会报错 ,,'') 报错: 消息 544,级别 16,状态 1,第 1 行当 IDENTITY_ ...
- C++之类模板
C++之类模板 代码如下: C++ Code 123456789101112131415161718192021222324252627282930313233343536373839404142 ...