Commit f58ebb7a authored by 周尚's avatar 周尚

pay_vipcard_amt 错误修复

日志记录转由ngx.log
数据库参数写入init.lua
parent b0640587
...@@ -35,11 +35,11 @@ http { ...@@ -35,11 +35,11 @@ http {
lua_package_path "/Users/zhoush/openresty/nginx/lua/luabiz/?.lua;;"; lua_package_path "/Users/zhoush/openresty/nginx/lua/luabiz/?.lua;;";
init_by_lua_file "lua/luainit/init.lua"; init_by_lua_file "lua/luainit/init.lua";
# init_worker_by_lua_file "luabiz/settle/settle.lua"; init_worker_by_lua_file "lua/luabiz/settle.lua";
#gzip on; #gzip on;
server { server {
listen 7777 ; listen 8080;
server_name localhost; server_name localhost;
...@@ -70,12 +70,48 @@ http { ...@@ -70,12 +70,48 @@ http {
root html; root html;
index index.html index.htm; index index.html index.htm;
lua_code_cache on; lua_code_cache on;
log_by_lua_block {
local logger = require("socket")
if not logger.initted() then
local ok, err = logger.init {
host = '127.0.0.1',
port = 1234,
flush_limit = 4096
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
return
end
end
-- construct the custom access log message in
-- the Lua variable "msg"
local bytes, err = logger.log(msg)
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
return
end
}
}
location /regex {
content_by_lua_block {
local str = '2018-08-15'
local regex = [[(\d+)-(\d+)-(\d+)]]
local r = ngx.re.match(str, regex, 'jo')
if r then
ngx.say(r[1], r[2], r[3])
else
ngx.say('no matched')
end
}
} }
location /test { location /settle {
content_by_lua_block { content_by_lua_block {
local tk_sdt = loadmod("tk_sdt") local settle = loadmod("settle")
tk_sdt:run() settle:run()
} }
} }
......
...@@ -52,7 +52,7 @@ local function async_db(ins_sql) ...@@ -52,7 +52,7 @@ local function async_db(ins_sql)
} }
if not ok then if not ok then
zx_base:log("ASYNC_DB", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("ASYNC_DB", err, ": ", errcode, " ", sqlstate)
return return
end end
......
...@@ -18,7 +18,7 @@ local function asb_sql(db_st, interval) ...@@ -18,7 +18,7 @@ local function asb_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -31,7 +31,7 @@ local function asb_sql(db_st, interval) ...@@ -31,7 +31,7 @@ local function asb_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_ASB]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_ASB]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -93,7 +93,7 @@ local function dispatch() ...@@ -93,7 +93,7 @@ local function dispatch()
end end
function _ASB.run() function _ASB.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -19,7 +19,7 @@ local function asbdetail_sql(db_st, interval) ...@@ -19,7 +19,7 @@ local function asbdetail_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -32,7 +32,7 @@ local function asbdetail_sql(db_st, interval) ...@@ -32,7 +32,7 @@ local function asbdetail_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_ASBDETAIL]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_ASBDETAIL]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -94,7 +94,7 @@ local function dispatch() ...@@ -94,7 +94,7 @@ local function dispatch()
end end
function _ASBDETAIL.run() function _ASBDETAIL.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
local _NC = {} local NODE_CARD = {}
local mysql = loadmod("resty.mysql") local mysql = loadmod("resty.mysql")
local zx_base = loadmod("zx_base") local zx_base = loadmod("zx_base")
local json = loadmod('cjson') local M = string.format("%-16s", "<NODE_CARD>")
local db_conf = "tk7_dbs.json" -- local json = loadmod('cjson')
-- local db_conf = "tk7_dbs.json"
local tasks = {} local tasks = {}
local function card_sql(db_st, total_time)
local function card_sql(db_st) ngx.log(ngx.NOTICE, "-----------------------> START")
local log = ngx.log
local ERR = ngx.ERR
local db, err = mysql:new() local db, err = mysql:new()
if not db then if not db then
zx_base:log("S", "[CARD]:", "failed to instantiate mysql: ", err) ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +33,7 @@ local function card_sql(db_st) ...@@ -34,7 +33,7 @@ local function card_sql(db_st)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_CARD]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) ngx.log(ngx.ERR, err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -42,47 +41,52 @@ local function card_sql(db_st) ...@@ -42,47 +41,52 @@ local function card_sql(db_st)
-- local sql_delete = [[DELETE FROM node_card WHERE (card_status != 0 AND card_status !=1) or now() > card_exp]] -- local sql_delete = [[DELETE FROM node_card WHERE (card_status != 0 AND card_status !=1) or now() > card_exp]]
local sql_update = [[UPDATE node_card SET card_status = 8 WHERE now() > card_exp]] local sql_update = [[UPDATE node_card SET card_status = 8 WHERE now() > card_exp]]
local res, err, errcode, sqlstate = db:query(sql_update) local res, err, errcode, sqlstate = db:query(sql_update)
ngx.log(ngx.NOTICE, sql_update)
if not res then if not res then
zx_base:log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CARD", sql_update)
return return
end end
-- SET yesterday's balance -- SET yesterday's balance
local sql_update = [[UPDATE node_card SET card_lbal_amt = card_cbal_amt, card_lbal_count = card_cbal_count]] local sql_update = [[UPDATE node_card SET card_lbal_amt = card_cbal_amt, card_lbal_count = card_cbal_count]]
local res, err, errcode, sqlstate = db:query(sql_update) local res, err, errcode, sqlstate = db:query(sql_update)
ngx.log(ngx.NOTICE, sql_update)
if not res then if not res then
zx_base:log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CARD", sql_update)
return return
end end
-- INSERT INTO history table -- INSERT INTO history table
local sql_insert = string.format([[INSERT INTO node_card_his SELECT * FROM node_card WHERE DATE(`create_time`) < DATE('%s')]], os.date("%Y%m%d%H%M%S")) local sql_insert = string.format([[INSERT INTO node_card_his SELECT * FROM node_card ]]..
[[WHERE DATE(`create_time`) < DATE('%s')]],
os.date("%Y%m%d%H%M%S", total_time - 24 * 60 * 60))
ngx.log(ngx.NOTICE, sql_insert)
local res, err, errcode, sqlstate = db:query(sql_insert) local res, err, errcode, sqlstate = db:query(sql_insert)
if not res then if not res then
zx_base:log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CARD", sql_insert)
return return
end end
-- DELETE current tables record -- DELETE current tables record
local sql_delete = string.format([[DELETE FROM node_card WHERE DATE(`create_time`) < DATE('%s')]], os.date("%Y%m%d%H%M%S")) local sql_delete = string.format([[DELETE FROM node_card WHERE DATE(`create_time`) < DATE('%s')]],
os.date("%Y%m%d%H%M%S", total_time - 24 * 60 * 60 ))
ngx.log(ngx.NOTICE, sql_delete)
local res, err, errcode, sqlstate = db:query(sql_delete) local res, err, errcode, sqlstate = db:query(sql_delete)
if not res then if not res then
zx_base:log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CARD", sql_delete)
return return
end end
ngx.log(ngx.NOTICE, "-----------------------> END")
zx_base:close_db(db)
end end
local function async(i) local function async(i, total_time)
local co = coroutine.wrap( local co = coroutine.wrap(
function() function()
card_sql(i) card_sql(i, total_time)
end end
) )
table.insert(tasks, co) table.insert(tasks, co)
...@@ -108,12 +112,13 @@ local function dispatch() ...@@ -108,12 +112,13 @@ local function dispatch()
end end
end end
function _NC.run() function NODE_CARD.run(self, total_time)
local dbs_st = zx_base:db_read(db_conf) -- -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) -- async(dbs_st.tk_biz[i])
card_sql(dbs_st.tk_biz[i], total_time)
end end
dispatch() dispatch()
end end
return _NC return NODE_CARD
...@@ -21,7 +21,7 @@ local function chgrule_sql(db_st) ...@@ -21,7 +21,7 @@ local function chgrule_sql(db_st)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function chgrule_sql(db_st) ...@@ -34,7 +34,7 @@ local function chgrule_sql(db_st)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_CHGRULE]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_CHGRULE]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -81,7 +81,7 @@ local function dispatch() ...@@ -81,7 +81,7 @@ local function dispatch()
end end
function _CR.run() function _CR.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) async(dbs_st.tk_biz[i])
end end
......
...@@ -6,6 +6,7 @@ local NODE_CS = {} ...@@ -6,6 +6,7 @@ local NODE_CS = {}
local mysql = loadmod("resty.mysql") local mysql = loadmod("resty.mysql")
local zx_base = loadmod('zx_base') local zx_base = loadmod('zx_base')
local db_conf = "tk7_dbs.json" local db_conf = "tk7_dbs.json"
local MODULE = "S"
local tasks = {} local tasks = {}
...@@ -15,11 +16,11 @@ local function cs_sql(db_st, interval) ...@@ -15,11 +16,11 @@ local function cs_sql(db_st, interval)
local db, err = mysql:new() local db, err = mysql:new()
if not db then if not db then
zx_base:log("S", "[CS]:", "failed to instantiate mysql: ", err) zx_base:log(MODULE, "[CS]:", "failed to instantiate mysql: ", err)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -32,7 +33,7 @@ local function cs_sql(db_st, interval) ...@@ -32,7 +33,7 @@ local function cs_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_CS]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log(MODULE, "[NODE_CS]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -43,8 +44,8 @@ local function cs_sql(db_st, interval) ...@@ -43,8 +44,8 @@ local function cs_sql(db_st, interval)
local res, err, errcode, sqlstate = db:query(sql_update) local res, err, errcode, sqlstate = db:query(sql_update)
if not res then if not res then
zx_base:log("S", "[NODE_CS]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") zx_base:log(MODULE, "[NODE_CS]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CS", sql_update) zx_base:log(MODULE, "[NODE_CS]", sql_update)
return return
end end
...@@ -57,8 +58,8 @@ local function cs_sql(db_st, interval) ...@@ -57,8 +58,8 @@ local function cs_sql(db_st, interval)
local res, err, errcode, sqlstate = db:query(sql_delete) local res, err, errcode, sqlstate = db:query(sql_delete)
if not res then if not res then
zx_base:log("S", "[NODE_CS]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") zx_base:log(MODULE, "[NODE_CS]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_CS", sql_delete) zx_base:log(MODULE, "[NODE_CS]", sql_delete)
return return
end end
...@@ -99,10 +100,11 @@ local function dispatch() ...@@ -99,10 +100,11 @@ local function dispatch()
end end
function NODE_CS.run() function NODE_CS.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) cs_sql(dbs_st.tk_biz[i], interval)
-- async(dbs_st.tk_biz[i], interval)
end end
dispatch() dispatch()
end end
......
...@@ -21,7 +21,7 @@ local function csdetail_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function csdetail_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function csdetail_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function csdetail_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_CSDETAIL]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_CSDETAIL]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _CSDETAIL.run() function _CSDETAIL.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -19,7 +19,7 @@ local function ms_sql(db_st) ...@@ -19,7 +19,7 @@ local function ms_sql(db_st)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -32,7 +32,7 @@ local function ms_sql(db_st) ...@@ -32,7 +32,7 @@ local function ms_sql(db_st)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_MS]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_MS]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -79,7 +79,7 @@ local function dispatch() ...@@ -79,7 +79,7 @@ local function dispatch()
end end
function _MS.run() function _MS.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) async(dbs_st.tk_biz[i])
end end
......
...@@ -21,7 +21,7 @@ local function payment_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function payment_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function payment_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function payment_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_PAYMENT]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_PAYMENT]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _PAYMENT.run() function _PAYMENT.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -20,7 +20,7 @@ local function psb_sql(db_st, interval) ...@@ -20,7 +20,7 @@ local function psb_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -33,7 +33,7 @@ local function psb_sql(db_st, interval) ...@@ -33,7 +33,7 @@ local function psb_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_PSB]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_PSB]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -97,7 +97,7 @@ end ...@@ -97,7 +97,7 @@ end
function _PSB.run() function _PSB.run()
local zx_base = loadmod('zx_base') local zx_base = loadmod('zx_base')
local json = loadmod('cjson') local json = loadmod('cjson')
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -20,7 +20,7 @@ local function psbdetail_sql(db_st, interval) ...@@ -20,7 +20,7 @@ local function psbdetail_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -33,7 +33,7 @@ local function psbdetail_sql(db_st, interval) ...@@ -33,7 +33,7 @@ local function psbdetail_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_PSBDETAIL]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_PSBDETAIL]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -97,7 +97,7 @@ end ...@@ -97,7 +97,7 @@ end
function _PSBDETAIL.run() function _PSBDETAIL.run()
local zx_base = loadmod('zx_base') local zx_base = loadmod('zx_base')
local json = loadmod('cjson') local json = loadmod('cjson')
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -20,7 +20,7 @@ local function rchg_sql(db_st, interval) ...@@ -20,7 +20,7 @@ local function rchg_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -33,7 +33,7 @@ local function rchg_sql(db_st, interval) ...@@ -33,7 +33,7 @@ local function rchg_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_RCHG]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_RCHG]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -92,7 +92,7 @@ local function dispatch() ...@@ -92,7 +92,7 @@ local function dispatch()
end end
function _RCHG.run() function _RCHG.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 local interval = 3
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
......
...@@ -20,7 +20,7 @@ local function rchgdetail_sql(db_st, interval) ...@@ -20,7 +20,7 @@ local function rchgdetail_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -33,7 +33,7 @@ local function rchgdetail_sql(db_st, interval) ...@@ -33,7 +33,7 @@ local function rchgdetail_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_RCHGDETAIL]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_RCHGDETAIL]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -95,7 +95,7 @@ local function dispatch() ...@@ -95,7 +95,7 @@ local function dispatch()
end end
function _RCHGDETAIL.run() function _RCHGDETAIL.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -21,7 +21,7 @@ local function saledetail_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function saledetail_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function saledetail_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function saledetail_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_SALEDETAIL]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_SALEDETAIL]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _SALEDETAIL.run() function _SALEDETAIL.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -19,7 +19,7 @@ local function saleorder_sql(db_st, interval) ...@@ -19,7 +19,7 @@ local function saleorder_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -32,7 +32,7 @@ local function saleorder_sql(db_st, interval) ...@@ -32,7 +32,7 @@ local function saleorder_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log(MODULE, "[NODE_SALEORDER]", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log(MODULE, "[NODE_SALEORDER]", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _SALEORDER.run() function _SALEORDER.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -21,7 +21,7 @@ local function shift_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function shift_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function shift_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function shift_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_SHIFT]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_SHIFT]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _SHIFT.run() function _SHIFT.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -21,7 +21,7 @@ local function sku_sql(db_st) ...@@ -21,7 +21,7 @@ local function sku_sql(db_st)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function sku_sql(db_st) ...@@ -34,7 +34,7 @@ local function sku_sql(db_st)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_SKU]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_SKU]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -82,7 +82,7 @@ local function dispatch() ...@@ -82,7 +82,7 @@ local function dispatch()
end end
function _SKU.run() function _SKU.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) async(dbs_st.tk_biz[i])
end end
......
...@@ -21,7 +21,7 @@ local function sms_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function sms_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function sms_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function sms_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_SMS]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_SMS]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _SMS.run() function _SMS.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
...@@ -21,7 +21,7 @@ local function smsvip_sql(db_st, interval) ...@@ -21,7 +21,7 @@ local function smsvip_sql(db_st, interval)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -34,7 +34,7 @@ local function smsvip_sql(db_st, interval) ...@@ -34,7 +34,7 @@ local function smsvip_sql(db_st, interval)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_SMSVIP]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_SMSVIP]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -96,7 +96,7 @@ local function dispatch() ...@@ -96,7 +96,7 @@ local function dispatch()
end end
function _SMSVIP.run() function _SMSVIP.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local interval = 3 -- the month interval local interval = 3 -- the month interval
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], interval) async(dbs_st.tk_biz[i], interval)
......
local _NT = {} local NODE_TICKET = {}
local mysql = loadmod("resty.mysql") local mysql = loadmod("resty.mysql")
local zx_base = loadmod('zx_base') local zx_base = loadmod('zx_base')
local db_conf = "tk7_dbs.json"
local M = string.format("%-16s", "<NODE_TICKET>")
local tasks = {} local tasks = {}
local db_conf = "tk7_dbs.json"
local function ticket_sql(db_st) local function ticket_sql(db_st, total_time)
local log = ngx.log ngx.log(ngx.NOTICE, "-----------------------> START")
local ERR = ngx.ERR
local db, err = mysql:new() local db, err = mysql:new()
if not db then if not db then
zx_base:log("S", "[TICKET]:", "failed to instantiate mysql: ", err) ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return false return false
end end
db:SET_timeout(1000) -- 1 sec db:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'], host = db_st['host'],
...@@ -33,46 +30,52 @@ local function ticket_sql(db_st) ...@@ -33,46 +30,52 @@ local function ticket_sql(db_st)
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_TICKET]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) ngx.log(ngx.ERR, err, ": ", errcode, " ", sqlstate)
return return
end end
-- DELETE invalid rows -- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_ticket WHERE (ticket_status != 0 AND ticket_status !=1) or now() > ticket_exp]] -- local sql_delete = [[DELETE FROM node_ticket WHERE (ticket_status != 0 AND ticket_status !=1) or now() > ticket_exp]]
local sql_update = [[UPDATE node_ticket SET ticket_status = 8 WHERE now() > ticket_exp]] local sql_update = [[UPDATE node_ticket SET ticket_status = 8 WHERE now() > ticket_exp]]
ngx.log(ngx.NOTICE, sql_update)
local res, err, errcode, sqlstate = db:query(sql_update) local res, err, errcode, sqlstate = db:query(sql_update)
if not res then if not res then
zx_base:log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_TICKET", sql_update)
return return
end end
-- INSERT INTO history table -- INSERT INTO history table
local sql_insert = string.format([[INSERT INTO node_ticket_his SELECT * FROM node_ticket WHERE DATE(`create_time`) < DATE('%s')]], os.date("%Y%m%d%H%M%S")) local sql_insert = string.format([[INSERT INTO node_ticket_his SELECT * FROM node_ticket WHERE ]]..
[[DATE(`create_time`) < DATE('%s')]],
os.date("%Y%m%d%H%M%S", total_time - 24 * 60 * 60))
ngx.log(ngx.NOTICE, sql_insert)
local res, err, errcode, sqlstate = db:query(sql_insert) local res, err, errcode, sqlstate = db:query(sql_insert)
if not res then if not res then
zx_base:log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_TICKET", sql_insert)
return return
end end
-- DELETE current tables record -- DELETE current tables record
local sql_delete = string.format([[DELETE FROM node_ticket WHERE DATE(`create_time`) < DATE('%s')]], os.date("%Y%m%d%H%M%S")) local sql_delete = string.format([[DELETE FROM node_ticket WHERE DATE(`create_time`) < DATE('%s')]],
os.date("%Y%m%d%H%M%S", total_time - 24 * 60 * 60))
ngx.log(ngx.NOTICE, sql_delete)
local res, err, errcode, sqlstate = db:query(sql_delete) local res, err, errcode, sqlstate = db:query(sql_delete)
if not res then if not res then
zx_base:log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".") ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("NODE_TICKET", sql_delete)
return return
end end
ngx.log(ngx.NOTICE, "-----------------------> END")
zx_base:close_db(db)
end end
local function async(i) local function async(i, total_time)
local co = coroutine.wrap( local co = coroutine.wrap(
function() function()
ticket_sql(i) ticket_sql(i, total_time)
end end
) )
table.insert(tasks, co) table.insert(tasks, co)
...@@ -98,14 +101,14 @@ local function dispatch() ...@@ -98,14 +101,14 @@ local function dispatch()
end end
end end
function _NT.run() function NODE_TICKET.run(self, total_time)
local zx_base = loadmod('zx_base') -- local json = loadmod('cjson')
local json = loadmod('cjson') -- -- local dbs_st = zx_base:db_read(db_conf)
local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) -- async(dbs_st.tk_biz[i])
ticket_sql(dbs_st.tk_biz[i], total_time)
end end
dispatch() dispatch()
end end
return _NT return NODE_TICKET
...@@ -46,7 +46,7 @@ local function vipacct_sql(db_st) ...@@ -46,7 +46,7 @@ local function vipacct_sql(db_st)
charset = 'utf8' charset = 'utf8'
} }
if not ok then if not ok then
zx_base:log("S", "[NODE_VIPACCT]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate) zx_base:log("S", "[NODE_VIPACCT]:", err, ": ", errcode, " ", sqlstate)
return return
end end
...@@ -136,7 +136,7 @@ end ...@@ -136,7 +136,7 @@ end
function _VIPACCT.run() function _VIPACCT.run()
local zx_base = loadmod('zx_base') local zx_base = loadmod('zx_base')
local json = loadmod('cjson') local json = loadmod('cjson')
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i]) async(dbs_st.tk_biz[i])
end end
......
-- Settle modules -- Settle modules
local zx_base = loadmod('zx_base')
-- local node_asb = loadmod('node_asb') -- local node_asb = loadmod('node_asb')
-- local node_asbdetail = loadmod('node_asbdetail') -- local node_asbdetail = loadmod('node_asbdetail')
-- local node_card = loadmod('node_card') local node_card = loadmod('node_card')
-- local node_chgrule = loadmod('node_chgrule') -- local node_chgrule = loadmod('node_chgrule')
-- local node_cs = loadmod('node_cs') -- local node_cs = loadmod('node_cs')
-- local node_csdetail = loadmod('node_csdetail') -- local node_csdetail = loadmod('node_csdetail')
...@@ -17,24 +18,29 @@ ...@@ -17,24 +18,29 @@
-- local node_sku = loadmod('node_sku') -- local node_sku = loadmod('node_sku')
-- local node_sms = loadmod('node_sms') -- local node_sms = loadmod('node_sms')
-- local node_smsvip = loadmod('node_smsvip') -- local node_smsvip = loadmod('node_smsvip')
-- local node_ticket = loadmod('node_ticket') local node_ticket = loadmod('node_ticket')
-- local node_vipacct = loadmod('node_vipacct') -- local node_vipacct = loadmod('node_vipacct')
local tk_sdt = loadmod('tk_sdt') local tk_sdt = loadmod('tk_sdt')
local tk_sync = loadmod('tk_sync')
local delay = 5 -- delaytime local delay = 5 -- delaytime
local interval_time = '1500' -- time to run, here is 12:00 for each day local interval_time = '0100' -- time to run, here is 12:00 for each day
-- local settledate = "2018-08-16"
local settledate = os.date('%Y-%m-%d', os.time())
local settletime = zx_base:date2time(settledate)
local handler local handler
handler = function(premature, interval_time)
handler = function(premature, interval_time, settletime)
if not premature then if not premature then
local nowtime = os.date("%H%M") local nowtime = os.date("%H%M")
if nowtime == interval_time then if nowtime == interval_time then
-- settle features start -- settle features start
-- node_asb: run() -- node_asb: run()
-- node_asbdetail: run() -- node_asbdetail: run()
-- node_card: run()
-- node_chgrule: run() -- node_chgrule: run()
-- node_cs: run() -- node_cs: run()
-- node_csdetail: run() -- node_csdetail: run()
...@@ -50,14 +56,20 @@ handler = function(premature, interval_time) ...@@ -50,14 +56,20 @@ handler = function(premature, interval_time)
-- node_sku: run() -- node_sku: run()
-- node_sms: run() -- node_sms: run()
-- node_smsvip: run() -- node_smsvip: run()
-- node_ticket: run()
-- node_vipacct: run() -- node_vipacct: run()
tk_sdt: run()
tk_sync: run()
node_card: run(settletime)
node_ticket: run(settletime)
tk_sdt: run(settletime)
-- reset interval_time -- reset interval_time
interval_time = tostring(tonumber(interval_time) - 1) interval_time = tostring(tonumber(interval_time) - 1)
-- reset settle time
settledate = os.date('%Y-%m-%d', os.time())
settletime = zx_base:date2time(settledate)
end end
local ok, err = ngx.timer.at(delay, handler, interval_time) local ok, err = ngx.timer.at(delay, handler, interval_time, settletime)
if not ok then if not ok then
ngx.log(ngx.ERR, "failed to create the timer:", err) ngx.log(ngx.ERR, "failed to create the timer:", err)
return return
...@@ -66,9 +78,45 @@ handler = function(premature, interval_time) ...@@ -66,9 +78,45 @@ handler = function(premature, interval_time)
end end
if ngx.worker.id() == 1 then if ngx.worker.id() == 1 then
local ok, err = ngx.timer.at(delay, handler, interval_time) local ok, err = ngx.timer.at(delay, handler, interval_time, settletime)
if not ok then if not ok then
ngx.log(ngx.ERR, "failed to create the timer:", err) ngx.log(ngx.ERR, "failed to create the timer:", err)
return return
end end
end end
local SETTLE = {}
function SETTLE.run()
local args = ngx.req.get_uri_args()
for k, v in pairs(args) do
if k == "total_date" then
local t = zx_base:date2time(v)
-- settle features start
-- node_asb: run()
-- node_asbdetail: run()
-- node_chgrule: run()
-- node_cs: run()
-- node_csdetail: run()
-- node_ms: run()
-- node_payment: run()
-- node_psb: run()
-- node_psbdetail: run()
-- node_rchg: run()
-- node_rchgdetail: run()
-- node_saledetail: run()
-- node_saleorder: run()
-- node_shift: run()
-- node_sku: run()
-- node_sms: run()
-- node_smsvip: run()
-- node_vipacct: run()
tk_sync: run()
node_card: run(t)
node_ticket: run(t)
tk_sdt: run(t)
end
end
end
return SETTLE
This diff is collapsed.
...@@ -14,73 +14,66 @@ local tasks = {} ...@@ -14,73 +14,66 @@ local tasks = {}
local function EOF() local function EOF()
zx_base:log(MODULE, "-----------------------> END[TK_SYNC]")
ngx.eof()
end end
-- 表信息 -- 表信息
-- @param table tk_biz 节点机数据库对象 -- @param table tk_biz 节点机数据库对象
-- table tk_control 总控机数据库对象 -- table tk_control 总控机数据库对象
-- table tk_database 数据同步数据库名
-- table tk_table 数据同步表名 -- table tk_table 数据同步表名
-- @return nil -- @return nil
local function sync_sql(tk_biz, tk_control, tk_database, tk_table) local function sync_sql(tk_biz, tk_control, tk_table)
zx_base:log(MODULE, "-----------------------> START[TK_SYNC]") ngx.log(ngx.NOTICE, "-----------------------> START")
-- 新建mysql连接 -- 新建mysql连接
local db, err = mysql:new() local db, err = mysql:new()
if not db then if not db then
zx_base:log(MODULE, "[TK_SYNC]", "failed to instantiate mysql: ", err) ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return false return false
end end
db:set_timeout(1000) -- 1 sec
-- 连接节点机MySQL -- 连接节点机MySQL
local ok, err, errcode, sqlstate = db:connect{ local ok, err, errcode, sqlstate = db:connect{
host = tk_biz['host'], host = tk_biz['host'],
port = tk_biz['port'], port = tk_biz['port'],
database = tk_database, database = tk_biz['database'],
user = tk_biz['user'], user = tk_biz['user'],
password = tk_biz['password'], password = tk_biz['password'],
timeout = tk_biz['timeout'], timeout = tk_biz['timeout'],
charset = 'utf8' charset = 'utf8'
} }
if not ok then if not ok then
zx_base:log(MODULE, "[TK_SYNC]", "failed to connect:", json.encode(tk_biz), ":", ngx.log(ngx.ERR, json.encode(tk_biz), ":", err, errcode, sqlstate)
err, ":", errcode, sqlstate)
return return
end end
-- 查询节点机 -- 查询节点机
local sql = string.format([[SELECT * FROM %s where update_time > %s]], local sql = string.format([[SELECT * FROM %s where update_time > %s]],
-- os.date("%Y%m%d000000", os.time()-24*60*60)
tk_table, tk_table,
"20180716000000" os.date("%Y%m%d000000", os.time()-24*60*60)
) )
ngx.log(ngx.NOTICE, sql)
local syncs local syncs
syncs, err, errcode, sqlstate = db:query(sql) syncs, err, errcode, sqlstate = db:query(sql)
if not syncs then if not syncs then
zx_base:log(MODULE, "[TK_SYNC]", sql) ngx.log(ngx.ERR, "failed to query:", json.encode(tk_biz), ":",
zx_base:log(MODULE, "[TK_SYNC]", "failed to query:", json.encode(tk_biz), ":",
err, ":", errcode, ":", sqlstate, ".") err, ":", errcode, ":", sqlstate, ".")
return return
end end
if #syncs == 0 then if #syncs == 0 then
ngx.say("no syncs found") return "00", "SUCCESS"
ngx.eof()
return
end end
-- 重置MySQL连接 -- 重置MySQL连接
zx_base:close_db(db) zx_base:close_db(db)
db, err = mysql:new() db, err = mysql:new()
if not db then if not db then
zx_base:log(MODULE, "[TK_SYNC]", "failed to instantiate mysql: ", err) ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return false return
end end
db:set_timeout(1000)
-- 连接总控机MySQL -- 连接总控机MySQL
ok, err, errcode, sqlstate = db:connect{ ok, err, errcode, sqlstate = db:connect{
host = tk_control['host'], host = tk_control['host'],
...@@ -92,7 +85,7 @@ local function sync_sql(tk_biz, tk_control, tk_database, tk_table) ...@@ -92,7 +85,7 @@ local function sync_sql(tk_biz, tk_control, tk_database, tk_table)
charset = 'utf8' charset = 'utf8'
} }
if not ok then if not ok then
zx_base:log(MODULE, "[TK_SYNC]", "failed to connect:", json.encode(tk_biz), ":", errcode, sqlstate) ngx.log(ngx.ERR, json.encode(tk_control), ":", err, errcode, sqlstate)
return return
end end
...@@ -101,17 +94,15 @@ local function sync_sql(tk_biz, tk_control, tk_database, tk_table) ...@@ -101,17 +94,15 @@ local function sync_sql(tk_biz, tk_control, tk_database, tk_table)
for i = 1, #syncs do for i = 1, #syncs do
local sync = syncs[i] local sync = syncs[i]
local sql_result = zx_base:sql_concate("duplicate", tk_table, sync) local sql_result = zx_base:sql_concate("duplicate", tk_table, sync)
ngx.log(ngx.NOTICE, sql_result)
ok, err, errcode, sqlstate = db:query(sql_result) ok, err, errcode, sqlstate = db:query(sql_result)
if not ok then if not ok then
zx_base:log(MODULE, "[TK_SYNC]", sql_result) ngx.log(ngx.ERR, "failed to query:", json.encode(tk_control), ":",
zx_base:log(MODULE, "[TK_SYNC]", "failed to query:", json.encode(tk_control), ":",
err, ":", errcode, ":", sqlstate, ".") err, ":", errcode, ":", sqlstate, ".")
end end
end end
ngx.log(ngx.NOTICE, "-----------------------> END")
ngx.say("SUCCESS")
EOF()
end end
...@@ -119,13 +110,12 @@ end ...@@ -119,13 +110,12 @@ end
-- 异步运行 -- 异步运行
-- @param table tk_biz 节点机数据库对象 -- @param table tk_biz 节点机数据库对象
-- table tk_control 总控机数据库对象 -- table tk_control 总控机数据库对象
-- table tk_database 数据同步数据库名
-- table tk_table 数据同步表名 -- table tk_table 数据同步表名
-- @return nil -- @return nil
local function async(tk_biz, tk_control, tk_database, tk_table) local function async(tk_biz, tk_control, tk_table)
local co = coroutine.wrap( local co = coroutine.wrap(
function() function()
sync_sql(tk_biz, tk_control, tk_database, tk_table) sync_sql(tk_biz, tk_control, tk_table)
end end
) )
table.insert(tasks, co) table.insert(tasks, co)
...@@ -159,10 +149,10 @@ end ...@@ -159,10 +149,10 @@ end
-- @param nil -- @param nil
-- @return nil -- @return nil
function TK_SYNC.run() function TK_SYNC.run()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], dbs_st.tk_control, dbs_st.tk_biz[i].database, "tk_shop") async(dbs_st.tk_biz[i], dbs_st.tk_control, "tk_shop")
async(dbs_st.tk_biz[i], dbs_st.tk_control, dbs_st.tk_biz[i].database, "tk_casher") async(dbs_st.tk_biz[i], dbs_st.tk_control, "tk_casher")
end end
dispatch() dispatch()
end end
...@@ -172,7 +162,7 @@ end ...@@ -172,7 +162,7 @@ end
-- @param nil -- @param nil
-- @return ngx.say -- @return ngx.say
local function tk_sync() local function tk_sync()
local dbs_st = zx_base:db_read(db_conf) -- local dbs_st = zx_base:db_read(db_conf)
local code local code
local mesg local mesg
...@@ -200,7 +190,8 @@ local function tk_sync() ...@@ -200,7 +190,8 @@ local function tk_sync()
return code, mesg return code, mesg
else else
async(dbs_st.tk_biz[i], dbs_st.tk_control, dbs_st.tk_biz[i].database, data.dbTable) -- async(dbs_st.tk_biz[i], dbs_st.tk_control, data.dbTable)
sync_sql(dbs_st.tk_biz[i], dbs_st.tk_control, data.dbTable)
code = "00" code = "00"
mesg = "SUCCESS" mesg = "SUCCESS"
...@@ -223,7 +214,9 @@ function TK_SYNC.sync() ...@@ -223,7 +214,9 @@ function TK_SYNC.sync()
ngx.say(json.encode({mesgRetCode=code, mesgRetDesc=mesg})) ngx.say(json.encode({mesgRetCode=code, mesgRetDesc=mesg}))
ngx.eof() ngx.eof()
ngx.log(ngx.ERR, ngx.var.request_body)
end end
return TK_SYNC return TK_SYNC
local _BASE = {} local ZX_BASE = {__index=_G}
local json = loadmod ('cjson') local json = loadmod ('cjson')
function _BASE.__FILE__ () return debug.getinfo(2,'S').source end function ZX_BASE.__FILE__ () return debug.getinfo(2,'S').source end
function _BASE.__LINE__ () return debug.getinfo(2,'l').currentline end function ZX_BASE.__LINE__ () return debug.getinfo(2,'l').currentline end
function _BASE.log(self, fname, ...) function ZX_BASE.log(self, fname, ...)
local fpath = string.format("%s/%s_%s.log", ngx.var.logs_path, tostring(fname), os.date("%Y%m%d")) local fpath = string.format("%s/%s_%s.log", ngx.var.logs_path, tostring(fname), os.date("%Y%m%d"))
local f = io.open(fpath, "a+") local f = io.open(fpath, "a+")
local str = "" local str = ""
...@@ -15,13 +15,12 @@ function _BASE.log(self, fname, ...) ...@@ -15,13 +15,12 @@ function _BASE.log(self, fname, ...)
str = string.format("%s %s", str, tmp) str = string.format("%s %s", str, tmp)
end end
f:write(os.date("<%Y-%m-%d %H:%M:%S> ")) f:write(os.date("<%Y-%m-%d %H:%M:%S> "))
-- f:write(...)
f:write(str) f:write(str)
f:write("\n") f:write("\n")
f:close() f:close()
end end
function _BASE.db_read(self, dbpath) function ZX_BASE.db_read(self, dbpath)
local f = io.open(ngx.var.conf_path.."/"..dbpath, "r") local f = io.open(ngx.var.conf_path.."/"..dbpath, "r")
local tmp = f:read("a*") local tmp = f:read("a*")
local db_st = json.decode(tmp) local db_st = json.decode(tmp)
...@@ -29,19 +28,18 @@ function _BASE.db_read(self, dbpath) ...@@ -29,19 +28,18 @@ function _BASE.db_read(self, dbpath)
return db_st return db_st
end end
function _BASE.close_db(self, db) function ZX_BASE.close_db(self, db)
if not db then if not db then
return return
end end
local ok, err = db:close() local ok, err = db:close()
if not ok then if not ok then
_BASE.log("failed to close:", err) ZX_BASE.log("failed to close:", err)
end end
return return
end end
function ZX_BASE.sql_concate(self, operate, tb_name, tb_data)
function _BASE.sql_concate(self, operate, tb_name, tb_data)
local sub = "" local sub = ""
for k, v in pairs(tb_data) do for k, v in pairs(tb_data) do
if v ~= nil and v ~= "" then if v ~= nil and v ~= "" then
...@@ -67,7 +65,7 @@ function _BASE.sql_concate(self, operate, tb_name, tb_data) ...@@ -67,7 +65,7 @@ function _BASE.sql_concate(self, operate, tb_name, tb_data)
end end
function _BASE.tonumber(self, str) function ZX_BASE.tonumber(self, str)
if str == nil or str == "" then if str == nil or str == "" then
return 0 return 0
else else
...@@ -75,4 +73,12 @@ function _BASE.tonumber(self, str) ...@@ -75,4 +73,12 @@ function _BASE.tonumber(self, str)
end end
end end
return _BASE
function ZX_BASE.date2time(self, settletime)
local regex = [[(%d+)-(%d+)-(%d+)]]
local y, m, d = settletime:match(regex)
local time = os.time({year = y, month = m, day = d})
return time
end
return ZX_BASE
...@@ -44,3 +44,36 @@ function _G.loadmod(namespace) ...@@ -44,3 +44,36 @@ function _G.loadmod(namespace)
-- 模块加载失败 -- 模块加载失败
error(module, 2) error(module, 2)
end end
-- _G.fpath = string.format("/var/log/www/S_%s.log", os.date("%Y%m%d"))
-- _G.f = io.open(_G.fpath, "a+")
-- 唐库 7 数据库文件
_G.dbs_st = {
tk_sku = {
host = "192.168.254.100",
port = 3306,
database = "tk_sku",
user = "root",
password = "",
timeout = 30
},
tk_control = {
host = "rm-bp1u9rp1p216nb15f.mysql.rds.aliyuncs.com",
port = 3306,
database = "tk_control",
user = "tk_control_user",
password = "KJjsdn%zx3@jr#y57dhh20drYT!z",
timeout = 30
},
tk_biz = {
{
host = "rm-bp1rob0a7ry7zmc0n.mysql.rds.aliyuncs.com",
port = 3306,
database = "tk_biz",
user = "tk_node_user",
password = "KJjsnc%gs5@jr#y789sajkshdrYT!z",
timeout = 30
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment