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 +++++++++++++++++++++++++++++++++--- src/daq/nginx-lua/s5-nginx-init.lua | 8 +++--- src/daq/nginx-lua/s5-nginx-log.lua | 18 +++++++------- 3 files changed, 58 insertions(+), 16 deletions(-) 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 diff --git a/src/daq/nginx-lua/s5-nginx-init.lua b/src/daq/nginx-lua/s5-nginx-init.lua index d5bc6a7..9d73603 100644 --- a/src/daq/nginx-lua/s5-nginx-init.lua +++ b/src/daq/nginx-lua/s5-nginx-init.lua @@ -40,16 +40,16 @@ -- ngx.log(ngx.DEBUG, "SFive: loaded") -local sfive = ngx.shared.sfive; +local sfive = ngx.shared.sfive sfive:flush_all() sfive:flush_expired() -local sfive_log = ngx.shared.sfive_log; +local sfive_log = ngx.shared.sfive_log sfive_log:flush_all() sfive_log:flush_expired() local ok, err, force = sfive:set("log:idx", 0) if not ok then - ngx.log(ngx.ERR, "SFive: creating log index counter failed: " .. err) + ngx.log(ngx.ERR, "SFive(init): creating log index counter failed: " .. err) else - ngx.log(ngx.INFO, "SFive: initialized successfully!") + ngx.log(ngx.INFO, "SFive(init): initialized successfully!") end diff --git a/src/daq/nginx-lua/s5-nginx-log.lua b/src/daq/nginx-lua/s5-nginx-log.lua index 862abee..c862343 100644 --- a/src/daq/nginx-lua/s5-nginx-log.lua +++ b/src/daq/nginx-lua/s5-nginx-log.lua @@ -41,14 +41,14 @@ local cleanup_delay = 1200 local log_exptime = 600 local log_cleanup = function(premature) - local sfive = ngx.shared.sfive; + local sfive = ngx.shared.sfive sfive:flush_expired() if premature then return end local ok, err = ngx.timer.at(cleanup_delay, log_cleanup) if not ok then - ngx.log(ngx.ERR, "SFive: failed to reenqueue log_cleanup: ", err) + ngx.log(ngx.ERR, "SFive(log): failed to reenqueue log_cleanup: ", err) sfive:delete("log:cleanup_running") return end @@ -60,11 +60,11 @@ local start_cleanup_thread = function(sfive) if err == "exists" then return end - ngx.log(ngx.ERR, "SFive: failed to set cleanup_running flag: " .. err) + ngx.log(ngx.ERR, "SFive(log): failed to set cleanup_running flag: " .. err) end ok, err = ngx.timer.at(cleanup_delay, log_cleanup) if not ok then - ngx.log(ngx.ERR, "SFive: failed to enqueue log_cleanup: ", err) + ngx.log(ngx.ERR, "SFive(log): failed to enqueue log_cleanup: ", err) sfive:delete("log:cleanup_running") return end @@ -73,10 +73,10 @@ end local status = ngx.var.status if status == '200' or status == '206' then - local sfive = ngx.shared.sfive; + local sfive = ngx.shared.sfive local idx, err = sfive:incr("log:idx", 1) if not idx then - ngx.log(ngx.ERR, "SFive: incrementing log index failed: " .. err) + ngx.log(ngx.ERR, "SFive(log): incrementing log index failed: " .. err) else local json = '{' json = json .. '"time": "' .. string.gsub(ngx.utctime(), " ", "T", 1) .. 'Z",' @@ -88,12 +88,12 @@ if status == '200' or status == '206' then json = json .. '"bytes_sent": ' .. ngx.var.bytes_sent json = json .. '}' - local sfive_log = ngx.shared.sfive_log; + local sfive_log = ngx.shared.sfive_log local ok, err, force = sfive_log:add(idx, json, log_exptime) if not ok then - ngx.log(ngx.ERR, "SFive: adding log line (".. idx .. ") to log store failed: " .. err) + ngx.log(ngx.ERR, "SFive(log): adding log line (".. idx .. ") to log store failed: " .. err) elseif force then - ngx.log(ngx.WARN, "SFive: adding log line has overwritten other log lines - consider increasing the log store!") + ngx.log(ngx.WARN, "SFive(log): adding log line has overwritten other log lines - consider increasing the log store!") end start_cleanup_thread(sfive) end -- cgit v1.2.3