希望早点加入这个功能啊,定时任务和其他队列系统结合可以衍生出很多用法。
On Monday, December 10, 2012 11:40:26 AM UTC+8, agentzh wrote:
Hello!2012/12/9 kindy:
> 如果添加 Timer 的 lua api,也是不错的。
> 这样就可以在 init_by_lua 或者 其他地方调用了。
>
> add_timer(delay, repeat_count, func)
>
> repeat_count = -1 的话,就一直repeat
> func 可以作为 thread 的入口函数,支持 发起 request
>
嗯,在 init_by_lua 这个上下文中倒是可以支持动态创建 timer 定时任务,比如
init_by_lua '
local function entry ()
-- can use ngx.socket.*, ngx.shared.*, ngx.thread.*, and
etc here...
return true -- continue running at the next timer expiration
end
local ok, err = ngx.set_interval(entry, 1000) -- every 1000ms
(i.e., 1 sec)
if not ok then
ngx.log(ngx.CRIT, "failed to set interval: ", err)
end
';
这里 entry 函数可以返回 false 以终止当前的定时任务。
不过,受多 worker 进程模型的限制,timer 必然是每 worker 的。
具体的实现方法,是为每一个定时任务线程伪造一个 ngx_http_request_t 结构,这样现有的代码几乎可以直接复用 :)
当然,需要注意的另一个细节是 nginx worker 进程退出或者 HUP 信号重新加载配置时的处理。
Best regards,
-agentzh