Skip to content

Instantly share code, notes, and snippets.

@hamishforbes
Created May 1, 2014 18:19
Show Gist options
  • Select an option

  • Save hamishforbes/34a4b85a4a29ad46b9fc to your computer and use it in GitHub Desktop.

Select an option

Save hamishforbes/34a4b85a4a29ad46b9fc to your computer and use it in GitHub Desktop.
Single background thread
error_log logs/error.log debug;
events {
worker_connections 1024;
}
worker_processes 4;
http {
default_type application/octet-stream;
lua_shared_dict test_dict 1m;
init_by_lua '
function my_bg_func(premature)
local dict = ngx.shared["test_dict"]
local flagged_pid = dict:get("flag")
local pid = ngx.worker.pid()
if flagged_pid ~= pid then
return false
else
ngx.log(ngx.DEBUG, "background thread running in ", pid)
ngx.timer.at(1, my_bg_func)
end
end
';
init_worker_by_lua '
local dict = ngx.shared["test_dict"]
local ok, err = ngx.timer.at(1, my_bg_func)
if ok then
dict:set("flag", ngx.worker.pid())
else
ngx.log(ngx.ERR, "Failed to start background thread: "..err)
end
';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment