From 560d482f319d5c9f78925d594b28181a6d6b5df4 Mon Sep 17 00:00:00 2001 From: midoks Date: Sat, 11 Feb 2023 23:25:29 +0800 Subject: [PATCH] up --- plugins/op_waf/waf/lua/init_worker.lua | 18 +++++++++--------- plugins/webstats/index.py | 17 +++++++++++++++++ plugins/webstats/lua/webstats_common.lua | 6 +----- plugins/webstats/lua/webstats_worker.lua | 20 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 plugins/webstats/lua/webstats_worker.lua diff --git a/plugins/op_waf/waf/lua/init_worker.lua b/plugins/op_waf/waf/lua/init_worker.lua index 335fd1709..a4897b7b8 100644 --- a/plugins/op_waf/waf/lua/init_worker.lua +++ b/plugins/op_waf/waf/lua/init_worker.lua @@ -12,28 +12,28 @@ end local json = require "cjson" -local __C = require "waf_common" -local C = __C:getInstance() +local __WAF_C = require "waf_common" +local WAF_C = __WAF_C:getInstance() local waf_config = require "waf_config" local waf_site_config = require "waf_site" -C:setConfData(waf_config, waf_site_config) -C:setDebug(true) +WAF_C:setConfData(waf_config, waf_site_config) +WAF_C:setDebug(true) -- C:D("init worker"..tostring(ngx.worker.id())) local function waf_timer_stats_total_log(premature) - C:timer_stats_total() + WAF_C:timer_stats_total() end local waf_clean_expire_data = function(premature) - C:clean_log() + WAF_C:clean_log() end ngx.shared.waf_limit:set("cpu_usage", 0, 10) function waf_timer_every_get_cpu(premature) - local cpu_percent = C:read_file_body(waf_root.."/cpu.info") - -- C:D("cpu_usage:"..tostring(cpu_percent )) + local cpu_percent = WAF_C:read_file_body(waf_root.."/cpu.info") + -- WAF_C:D("cpu_usage:"..tostring(cpu_percent )) if cpu_percent then ngx.shared.waf_limit:set("cpu_usage", tonumber(cpu_percent), 10) else @@ -48,5 +48,5 @@ if ngx.worker.id() == 0 then ngx.timer.every(3, waf_timer_stats_total_log) ngx.timer.every(10, waf_clean_expire_data) - C:cron() + WAF_C:cron() end \ No newline at end of file diff --git a/plugins/webstats/index.py b/plugins/webstats/index.py index 985348351..7cfd5a3e3 100755 --- a/plugins/webstats/index.py +++ b/plugins/webstats/index.py @@ -254,11 +254,22 @@ def initDreplace(): file_list = [ 'webstats_common.lua', 'webstats_log.lua', + 'webstats_worker.lua', ] for fl in file_list: loadLuaFile(fl) + root_worker_dir = mw.getServerDir() + '/web_conf/nginx/lua/init_worker_by_lua_file' + webstats_worker_src_file = lua_dir = getServerDir() + "/lua/webstats_worker.lua" + webstats_worker_dst_file = root_worker_dir + '/webstats_worker.lua' + + content = mw.readFile(webstats_worker_src_file) + content = content.replace('{$SERVER_APP}', getServerDir()) + content = content.replace('{$ROOT_PATH}', mw.getServerDir()) + mw.writeFile(webstats_worker_dst_file, content) + mw.opLuaInitWorkerFile() + loadConfigFile() loadLuaSiteFile() loadDebugLogFile() @@ -290,6 +301,12 @@ def stop(): if os.path.exists(path): os.remove(path) + root_worker_dir = mw.getServerDir() + '/web_conf/nginx/lua/init_worker_by_lua_file' + webstats_worker_dst_file = root_worker_dir + '/webstats_worker.lua' + if os.path.exists(webstats_worker_dst_file): + os.remove(webstats_worker_dst_file) + mw.opLuaInitWorkerFile() + import tool_task tool_task.removeBgTask() diff --git a/plugins/webstats/lua/webstats_common.lua b/plugins/webstats/lua/webstats_common.lua index beb50ec45..f86700296 100644 --- a/plugins/webstats/lua/webstats_common.lua +++ b/plugins/webstats/lua/webstats_common.lua @@ -46,10 +46,6 @@ end function _M.getInstance(self) if rawget(self, "instance") == nil then rawset(self, "instance", self.new()) - - if 0 == ngx.worker.id() then - self:cron() - end end assert(self.instance ~= nil) return self.instance @@ -276,7 +272,7 @@ function _M.cron(self) local url_stats = {} local time_key = self:get_store_key() - + for site_k, site_v in ipairs(sites) do local input_sn = site_v["name"] -- self:D("input_sn:"..input_sn) diff --git a/plugins/webstats/lua/webstats_worker.lua b/plugins/webstats/lua/webstats_worker.lua new file mode 100644 index 000000000..08ff6f9bf --- /dev/null +++ b/plugins/webstats/lua/webstats_worker.lua @@ -0,0 +1,20 @@ +local cpath = "{$SERVER_APP}/lua/" +if not package.cpath:find(cpath) then + package.cpath = cpath .. "?.so;" .. package.cpath +end +if not package.path:find(cpath) then + package.path = cpath .. "?.lua;" .. package.path +end + +local __WS_C = require "webstats_common" +local WS_C = __WS_C:getInstance() + +WS_C:cronPre() +local webstats_cron_pre = function(premature) + WS_C:cronPre() +end + +if ngx.worker.id() == 0 then + ngx.timer.every(60, webstats_cron_pre) + WS_C:cron() +end \ No newline at end of file