Hi guys,
I also need timer in my nginx lua code. I need to execute some lua code on every 2 seconds. Is there any workaround for this. I came up with following nginx config, but it has 2 massive drawbacks:
I can't reload nginx config clean
The timer is not shared between children.
user nginx nginx;
worker_processes 1;
worker_rlimit_nofile 9000;
error_log /var/log/nginx/error.log info;
events {
worker_connections 8192;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type text/html;
lua_shared_dict log_dict 5M;
proxy_http_version 1.1;
recursive_error_pages on;
server {
listen 172.16.10.10:80;
set $site_name example;
set $site_id example;
server_name example.ecl-cdn.com example.ecl-cdn.net ;
location = /start-timer {
default_type text/html;
access_by_lua '
local log_dict = ngx.shared.log_dict
log_dict:set("timer_counter", 1)
ngx.thread.spawn(function () while 1 < 2 do log_dict:incr("timer_counter", 1) ngx.sleep(1) end end)
ngx.say("Started new timer")
';
}
location = /time_spent {
content_by_lua '
local log_dict = ngx.shared.log_dict
local value = log_dict:get("timer_counter") or 0
ngx.say("Timer value is:", value)
';
}
}
}
Is there any other solution? Of course I can create an external timer that makes specific request nginx that executes the code but I don't want to create external daemon for just one http request on every 2 seconds.
Regards,
Kiril
On Wednesday, October 31, 2012 1:52:06 PM UTC+2, Brian Akins wrote:
I couldn't come up with a good subject line...
Increasingly, nginx is just becoming a platform for us to run Lua.
More and more our configs are 90%+ Lua. There are lots of reasons for
this - we have a lot of "lightweight apps" in Lua, Lua is easier than
nginx config for some things, etc.
However, we do come upon situations where core nginx gets in the way.
An example is that you can't use cosockets in filters or loggers. I
can fake it by throwing everything into content_by_lua, but then I
have to write my handlers a certain way. We also would like to have
background tasks and timers (I think the perl module does this) that
can also use cosockets.
I've been playing with luvit ( http://luvit.io/ ) and generally like
the idea, but it has some rough edges. If luvit had cosockets and was
generally more Lua-like (rather than a node.js clone) it would be
almost perfect. If cosockets were ported over, then it seems most of
the resty modules would work with very little effort. FWIW, working
with the underlying libuv and sockets is a lot easier than working
with nginx core.
Just thinking out loud, so to speak. But at some point when all you
are doing is using nginx to run Lua and running into
complications/pitfalls, you start to look around.
--Brian