mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-18 23:45:52 +08:00
Update webstats_common.lua
This commit is contained in:
@@ -377,6 +377,7 @@ end
|
||||
function _M.cronPre(self)
|
||||
self:lock_working('cron_init_stat')
|
||||
|
||||
local ok, err = pcall(function()
|
||||
-- 获取当前小时和下一小时的存储键
|
||||
local time_key = self:get_store_key()
|
||||
local time_key_next = self:get_store_key_with_time(ngx.time() + 3600)
|
||||
@@ -389,10 +390,9 @@ function _M.cronPre(self)
|
||||
local input_sn = site_v["name"]
|
||||
|
||||
-- 安全地初始化数据库连接
|
||||
local ok, db = pcall(function() return self:initDB(input_sn) end)
|
||||
if not ok or not db then
|
||||
local db_ok, db = pcall(function() return self:initDB(input_sn) end)
|
||||
if not db_ok or not db then
|
||||
self:D("cronPre initDB failed for " .. tostring(input_sn) .. ": " .. tostring(db))
|
||||
self:unlock_working('cron_init_stat')
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -423,13 +423,21 @@ function _M.cronPre(self)
|
||||
pcall(function() db:close() end)
|
||||
|
||||
if not success then
|
||||
self:unlock_working('cron_init_stat')
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
self:unlock_working('cron_init_stat')
|
||||
return true
|
||||
end)
|
||||
|
||||
self:unlock_working('cron_init_stat')
|
||||
|
||||
if not ok then
|
||||
self:D("cronPre error: " .. tostring(err))
|
||||
return false
|
||||
end
|
||||
|
||||
return err
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -484,23 +492,36 @@ function _M.cron(self)
|
||||
|
||||
local time_key = self:get_store_key()
|
||||
|
||||
local function cleanup_and_unlock()
|
||||
for _, site_v in ipairs(sites) do
|
||||
local input_sn = site_v["name"]
|
||||
local stmt = stmts[input_sn]
|
||||
if stmt then
|
||||
pcall(function() stmt:finalize() end)
|
||||
end
|
||||
local db = dbs[input_sn]
|
||||
if db and db:isopen() then
|
||||
pcall(function() db:close() end)
|
||||
end
|
||||
end
|
||||
self:unlock_working(cron_key)
|
||||
end
|
||||
|
||||
local ok, err = pcall(function()
|
||||
for _, site_v in ipairs(sites) do
|
||||
local input_sn = site_v["name"]
|
||||
if self:is_migrating(input_sn) then
|
||||
self:unlock_working(cron_key)
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
if self:is_working('cron_init_stat') then
|
||||
self:unlock_working(cron_key)
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
local ok, db = pcall(function() return self:initDB(input_sn) end)
|
||||
if not ok or not db then
|
||||
local db_ok, db = pcall(function() return self:initDB(input_sn) end)
|
||||
if not db_ok or not db then
|
||||
self:D("initDB failed for " .. input_sn .. ": " .. tostring(db))
|
||||
self:unlock_working(cron_key)
|
||||
return true
|
||||
return
|
||||
end
|
||||
|
||||
stat_fields[input_sn] = {}
|
||||
@@ -508,7 +529,7 @@ function _M.cron(self)
|
||||
dbs[input_sn] = db
|
||||
self:clean_stats(db, input_sn)
|
||||
|
||||
local ok, stmt = pcall(function()
|
||||
local stmt_ok, stmt = pcall(function()
|
||||
return db:prepare[[INSERT INTO web_logs(
|
||||
time, ip, domain, server_name, method, status_code, uri, body_length,
|
||||
referer, user_agent, protocol, request_time, is_spider, request_headers, ip_list, client_port)
|
||||
@@ -517,57 +538,49 @@ function _M.cron(self)
|
||||
:request_headers, :ip_list, :client_port)]]
|
||||
end)
|
||||
|
||||
if ok and stmt then
|
||||
if stmt_ok and stmt then
|
||||
stmts[input_sn] = stmt
|
||||
db:exec([[BEGIN TRANSACTION]])
|
||||
else
|
||||
-- self:D("prepare statement failed for " .. input_sn .. ": " .. tostring(stmt))
|
||||
if db and db:isopen() then
|
||||
db:close()
|
||||
end
|
||||
dbs[input_sn] = nil
|
||||
self:unlock_working(cron_key)
|
||||
return true
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
local has_error = false
|
||||
|
||||
for i = 1, llen do
|
||||
local data = ngx.shared.mw_total:lpop(total_key)
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
|
||||
local ok, info = pcall(function() return json.decode(data) end)
|
||||
if not ok then
|
||||
local decode_ok, info = pcall(function() return json.decode(data) end)
|
||||
if not decode_ok then
|
||||
self:D("json decode failed: " .. tostring(info))
|
||||
ngx.shared.mw_total:rpush(total_key, data)
|
||||
has_error = true
|
||||
break
|
||||
return
|
||||
end
|
||||
|
||||
local input_sn = info['server_name']
|
||||
local db = dbs[input_sn]
|
||||
if not db then
|
||||
ngx.shared.mw_total:rpush(total_key, data)
|
||||
has_error = true
|
||||
break
|
||||
return
|
||||
end
|
||||
|
||||
local stmt = stmts[input_sn]
|
||||
if not stmt then
|
||||
ngx.shared.mw_total:rpush(total_key, data)
|
||||
has_error = true
|
||||
break
|
||||
return
|
||||
end
|
||||
|
||||
local insert_ok = self:store_logs_line(db, stmt, input_sn, info)
|
||||
if not insert_ok then
|
||||
ngx.shared.mw_total:rpush(total_key, data)
|
||||
rollback_sites[input_sn] = true
|
||||
has_error = true
|
||||
break
|
||||
return
|
||||
end
|
||||
|
||||
local log_kv = info["log_kv"]
|
||||
@@ -691,6 +704,13 @@ function _M.cron(self)
|
||||
pcall(function() db:close() end)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
if not ok then
|
||||
self:D("cron process error: " .. tostring(err))
|
||||
cleanup_and_unlock()
|
||||
return true
|
||||
end
|
||||
|
||||
self:unlock_working(cron_key)
|
||||
end
|
||||
@@ -943,7 +963,7 @@ end
|
||||
|
||||
function _M.lock_working(self, sign)
|
||||
local working_key = sign.."_working"
|
||||
cache:set(working_key, true, 60)
|
||||
cache:set(working_key, true, 10)
|
||||
end
|
||||
|
||||
function _M.unlock_working(self, sign)
|
||||
|
||||
Reference in New Issue
Block a user