From 6340720a0d6ce9a5fb9d63b8896c78df02f5670e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 17 Oct 2014 03:15:09 +0200 Subject: sfive nginx-lua plugin works now --- src/daq/nginx-lua/s5-nginx-fetch.lua | 48 +++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'src/daq/nginx-lua/s5-nginx-fetch.lua') diff --git a/src/daq/nginx-lua/s5-nginx-fetch.lua b/src/daq/nginx-lua/s5-nginx-fetch.lua index 90503de..8bfcf07 100644 --- a/src/daq/nginx-lua/s5-nginx-fetch.lua +++ b/src/daq/nginx-lua/s5-nginx-fetch.lua @@ -33,10 +33,52 @@ -- Install this by adding the following to your nginx.conf -- -- location /sfive { +-- allow localhost; +-- deny all; +-- +-- lua_check_client_abort on; -- content_by_lua_file '/path/to/s5-nginx-fetch.lua'; -- } -- -ngx.say('Content-type: text/plain; charset=UTF-8') -ngx.say('') -ngx.say('sfive says hello world!') +local sfive = ngx.shared.sfive + +local function fetch_cleanup() + ngx.log(ngx.INFO, "SFive(fetch): cleanup fetch") + sfive:delete("fetch:connected") +end + +local ok, err, force = sfive:add("fetch:connected", 1) +-- TODO: race condition: when client disconnects here we won't free up the singleton... +if not ok then + if err == "exists" then + ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE) + else + ngx.log(ngx.ERR, "SFive(fetch): failed to set connected flag: " .. err) + ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) + end +else + local ok, err = ngx.on_abort(fetch_cleanup) + if not ok then + ngx.log(ngx.ERR, "SFive(fetch): failed to register the on_abort callback: ", err) + ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) + end + + ngx.log(ngx.INFO, "SFive(fetch): client connected") + + local sfive_log = ngx.shared.sfive_log + while true do + logs = sfive_log:get_keys() + for i, k in ipairs(logs) do + local ok, err = ngx.say(sfive_log:get(k)) + if not ok then + ngx.log(ngx.ERR, "SFive(fetch): failed to register the on_abort callback: ", err) + break + end + sfive_log:delete(k) + end + ngx.flush() + ngx.sleep(0.1) + end + fetch_cleenup() +end -- cgit v1.2.3