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 ...
随机推荐
- 2016 年 Java 优秀文章
https://www.ibm.com/developerworks/cn/java/j-2016-java-good-article/index.html
- CentOs下安装gcc/g++/gdb
使用yum安装gcc:yum install gcc即可.使用:which gcc 查看是否安装成功 使用yum安装g++:yum install gcc-c++ 即可.使用:which g++ 查看 ...
- 幸好会java
转做android的可能性又往前增加了一分.
- [dts]TI-am437x dts
imx6 可以参考http://blog.csdn.net/shengzhadon/article/details/49908439 参照文件: Documentation/devicetree/bi ...
- Android——Activity和Intent及Activity的生命周期
实验Activity的生命周期 package com.example.chenshuai.test; import android.app.Activity; import android.os.B ...
- 第二百七十五节,MySQL数据库安装和介绍
MySQL数据库安装 一.概述 1.什么是数据库 ? 答:数据的仓库,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一种 ...
- Collection接口都是通过Iterator()(即迭代器)来对Set和List遍历
以下介绍接口: List接口:(介绍其下的两个实现类:ArrayList和LinkedList) ArrayList和数组非常类似,其底层①也用数组组织数据,ArrayList是动态可变数组. ① 底 ...
- (转)c指针问题
字符串常量问题: http://blog.csdn.net/zhongyili_sohu/article/details/8084188 1. 常量字符串 在代码里直接出现的”abcdef”这种字符串 ...
- POJ 3168 Barn Expansion (几何+排序)
题目链接:id=3168">POJ 3168 Barn Expansion 题意:抽象出来就是给出n个矩形的坐标是(左下角和右上角的坐标,矩形的边都是平行x,y轴),问有几个矩形和其它 ...
- JavaScript入门之函数返回值
函数返回值 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...