Search code examples
dictionarynginxluacronredis

How to create asynchronous "cron like" scheduler inside nginx


I need to create an asynchronous scheduler inside nginx server to update a variable. Let me give you an example what I mean by this and why I need it.

Imagine config file that looks something like this:

http {
    lua_shared_dict foo 5m;
    server {
        location /set {
            content_by_lua '
                local foo = ngx.shared.foo
                ngx.say(foo:get("12345"))
            ';
        }
    }
}  

I specified variable foo that resides in shared memory and all worker processes have access to it. What I want to do is to set those values from lua script that will be called every minite. Just for reference it will be going to the Redis and then retrieve necessary data, and update this variable. I know I can do this in content_by_lua in every call, but it's highly inefficient for a huge volume of traffic.

I would like a separate process that would be triggered every minute or so to just go and one task. Is there anything like this in nginx or are there any modules that could help me with that?


Solution

  • You can use the new ngx.timer API provided by ngx_lua. See the documentation for details:

    https://www.nginx.com/resources/wiki/modules/lua/#ngx-timer-at

    You can create a new timer in your timer handler to make the timer keeps triggering like a cronjob ;)

    BTW, the timer is per-worker process, you can use the lua-resty-lock library in your timer handler to ensure that only one timer is active at a time among all the nginx workers: https://github.com/agentzh/lua-resty-lock