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 ...
随机推荐
- FreeRtos——空闲任务与空闲任务钩子函数
以下基础知识转载自正点原子PDF资料. 前面例子 中创建的任务大部份时间都处于阻塞态.这种状态下所有的任务都不可运行,所以也不能被调度器选中.但处理器总是需要代码来执行——所以至少要有一个任务处于运行 ...
- python操作word(改课文格式)【最终版】
python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...
- 纯CSS炫酷3D旋转立方体进度条特效
在网站制作中,提高用户体验度是一项非常重要的任务.一个创意设计不但能吸引用户的眼球,还能大大的提高用户的体验.在这篇文章中,我们将大胆的将前面所学的3D立方体和进度条结合起来,制作一款纯CSS3的3D ...
- 利用|,&,^,~,<<,>>>写出高效艺术的代码
简单介绍: 大家在阅读源代码的时候常常会看到一些比方以下这样特别难理解的代码. cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEve ...
- mysql中创建用户和赋权限
mysql命令行用的不多,大部分使用工具类替代,所以这里记录下命令行模式下创建用户和赋予权限的命令,不用每次麻烦百度. 1. 创建oozie用户,%符号表示仅限于远程登录 create user 'o ...
- phpstorm 中文版 支持BUG调试 IDE
下载地址:http://dx2.7down.net/soft/P/phpstorm8_cn.zip
- 微服务vs传统开发
使用微服务有一段时间了,这种开发模式和传统的开发模式对比,有很大的不同. 分工不同,以前我们可能是一个一个模块,现在可能是一人一个系统. 架构不同,服务的拆分是一个技术含量很高的问题,拆分是否合理对以 ...
- Spring Cloud体系介绍
上图只是Spring Cloud体系的一部分,Spring Cloud共集成了19个子项目,里面都包含一个或者多个第三方的组件或者框架! Spring Cloud 工具框架 1.Spring Clou ...
- C# 中的treeview绑定数据库(递归算法)
近日面试的给我两道题目,一道是IQ测试,第二个就是题目所言 总共两个表 department(id int not null primary key,parentid int,name char(50 ...
- 注解-->Spring配置
有必要对JDK 5.0新增的注解(Annotation)技术进行简单的学习,因为Spring 支持@AspectJ,而@AspectJ本身就是基于JDK 5.0的注解技术.所以学习JDK 5.0的注解 ...