Commit 264197f6 authored by 周尚's avatar 周尚

using syslog-ng instead

parent c5379d91
This diff is collapsed.
......@@ -2,14 +2,14 @@
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
worker_connections 10000;
}
# worker_rlimit_nofile 30000;
......@@ -61,42 +61,21 @@ http {
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root /Users/zhoush/Documents/Private/Notes;
# index README.html;
# }
# access_log logs/host.access.log main;
location / {
root /Users/zhoush/Documents/Private/Notes/;
index README.html;
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
}
root /Users/zhoush/Documents/Private/Notes;
index README.html;
}
# location / {
# root /Users/zhoush/Documents/Private/Notes/;
# index README.html;
# lua_code_cache on;
# content_by_lua_file lua/luabiz/log.lua;
# # log_by_lua_file lua/luabiz/log.lua;
# }
location /get_addr {
content_by_lua_block {
local zx_base = loadmod("zx_base")
......@@ -105,6 +84,10 @@ http {
}
}
location /pos {
content_by_lua_file "lua/luabiz/pos.lua";
}
location /settle {
content_by_lua_block {
local settle = loadmod("settle")
......@@ -119,6 +102,9 @@ http {
}
}
location /log {
}
location /echo {
content_by_lua_block {
......
local _M = {}
local logger = require "resty.logger.socket"
function _M.__FILE__ () return string.match(debug.getinfo(2,'S').source, ".+/([^/]*%.%w+)$") end
function _M.__LINE__ () return debug.getinfo(2,'l').currentline end
function _M.log(self, ...)
if not logger.initted() then
local ok, err = logger.init{
host = '0.0.0.0',
port = 1234,
flush_limit = 2048, --日志长度大于flush_limit的时候会将msg信息推送一次
drop_limit = 99999,
}
if not ok then
ngx.log(ngx.ERR, "failed to initialize the logger: ",err)
return
end
end
local str = ""
for i = 1, select("#", ...) do
local tmp = tostring(select(i, ...))
str = string.format("%s %s", str, tmp)
end
local bytes, err = logger.log(str .. "\n")
if err then
ngx.log(ngx.ERR, "failed to log message: ", err)
ngx.log(ngx.NOTICE, str)
return
end
end
function _M.BEGIN(self, modname)
local str = string.format("\n\n\nNOTICE: " .. "-----------------------> <%s> BEGIN\n", string.upper(modname))
logger.log(str)
logger.flush()
end
function _M.END(self, modname)
local str = string.format("NOTICE: " .. "-----------------------> <%s> END\n\n\n", string.upper(modname))
logger.log(str)
logger.flush()
end
return _M
......@@ -5,121 +5,79 @@ local NODE_CARD = {}
local mysql = loadmod("resty.mysql")
local zx_base = loadmod("zx_base")
local log_lua = loadmod("log")
local M = string.format("%-16s", "<NODE_CARD>")
-- local json = loadmod('cjson')
-- local db_conf = "tk7_dbs.json"
local tasks = {}
local db_biz
local function card_sql(db_st, total_time)
ngx.log(ngx.NOTICE, "-----------------------> START")
local function BEGIN(tk_biz)
log_lua:BEGIN("node_card")
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
local err
db_biz, err = mysql:new()
if not db_biz then
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", "failed to instantiate mysql: ", err)
return false
end
db:set_timeout(1000) -- 1 sec
db_biz:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = zx_base:_get_addr(db_st['host']),
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
local ok, err, errcode, sqlstate = db_biz:connect{
host = zx_base:_get_addr(tk_biz['host']),
port = tk_biz['port'],
database = tk_biz['database'],
user = tk_biz['user'],
password = tk_biz['password'],
timeout = tk_biz['timeout'],
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, err, ": ", errcode, " ", sqlstate)
return
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", err, ": ", errcode, " ", sqlstate)
return false
end
return true
end
local function END()
zx_base:close_db(db_biz)
log_lua:END("node_card")
end
local function card_sql()
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_card WHERE (card_status != 0 AND card_status !=1) or now() > card_exp]]
local sql_update = string.format([[UPDATE node_card SET card_status = 8 WHERE %d > card_exp]], os.date("%Y%m%d"))
local res, err, errcode, sqlstate = db:query(sql_update)
ngx.log(ngx.NOTICE, sql_update)
local res, err, errcode, sqlstate = db_biz:query(sql_update)
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "NOTICE: " .. sql_update)
if not res then
ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
-- SET yesterday's balance
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)
ngx.log(ngx.NOTICE, sql_update)
sql_update = [[UPDATE node_card SET card_lbal_amt = card_cbal_amt, card_lbal_count = card_cbal_count]]
local res, err, errcode, sqlstate = db_biz:query(sql_update)
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "NOTICE: " .. sql_update)
if not res then
ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
-- -- INSERT INTO history table
-- local sql_insert = string.format([[INSERT INTO node_card_his SELECT * FROM node_card ]]..
-- -- [[WHERE DATE(`create_time`) < DATE('%s')]],
-- -- [[WHERE EXTRACT(YEAR_MONTH FROM `create_time`) = EXTRACT(YEAR_MONTH FROM DATE_SUB('%s', INTERVAL 3 MONTH))]],
-- os.date("%Y%m%d%H%M%S", total_time))
-- ngx.log(ngx.NOTICE, sql_insert)
-- local res, err, errcode, sqlstate = db:query(sql_insert)
-- if not res then
-- ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
-- return
-- end
-- -- 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", total_time - 24 * 60 * 60 ))
-- ngx.log(ngx.NOTICE, sql_delete)
-- local res, err, errcode, sqlstate = db:query(sql_delete)
-- if not res then
-- ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
-- return
-- end
ngx.log(ngx.NOTICE, "-----------------------> END")
zx_base:close_db(db)
end
local function async(i, total_time)
local co = coroutine.wrap(
function()
card_sql(i, total_time)
end
)
table.insert(tasks, co)
end
local function dispatch()
local i = 1
while true do
if tasks[i] == nil then
if tasks[1] == nil then
break
end
i = 1
end
local res = tasks[i]()
if not res then
table.remove(tasks, i)
else
i = i + 1
end
end
end
function NODE_CARD.run(self, total_time)
-- -- local dbs_st = zx_base:db_read(db_conf)
function NODE_CARD.run(self)
for i = 1, #dbs_st.tk_biz do
-- async(dbs_st.tk_biz[i])
card_sql(dbs_st.tk_biz[i], total_time)
if BEGIN(dbs_st.tk_biz[i]) then
card_sql()
end
END()
end
dispatch()
end
return NODE_CARD
......@@ -2,112 +2,68 @@ local NODE_TICKET = {}
local mysql = loadmod("resty.mysql")
local zx_base = loadmod('zx_base')
local logger = loadmod("log")
local db_conf = "tk7_dbs.json"
local M = string.format("%-16s", "<NODE_TICKET>")
local tasks = {}
local db_biz
local function BEGIN(tk_biz)
logger:BEGIN("node_ticket")
local err
local function ticket_sql(db_st, total_time)
ngx.log(ngx.NOTICE, "-----------------------> START")
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
db_biz, err = mysql:new()
if not db_biz then
logger:log(logger:__FILE__() .. ":" .. logger:__LINE__(), "ERR: ", "failed to instantiate mysql: ", err)
return false
end
db:set_timeout(1000) -- 1 sec
db_biz:set_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = zx_base:_get_addr(db_st['host']),
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
local ok, err, errcode, sqlstate = db_biz:connect{
host = zx_base:_get_addr(tk_biz['host']),
port = tk_biz['port'],
database = tk_biz['database'],
user = tk_biz['user'],
password = tk_biz['password'],
timeout = tk_biz['timeout'],
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, err, ": ", errcode, " ", sqlstate)
return
end
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_ticket WHERE (ticket_status != 0 AND ticket_status !=1) or now() > ticket_exp]]
local sql_update = string.format([[UPDATE node_ticket SET ticket_status = 8 WHERE %d > ticket_exp]], os.date("%Y%m%d"))
ngx.log(ngx.NOTICE, sql_update)
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
logger:log(logger:__FILE__() .. ":" .. logger:__LINE__(), "ERR: ", err, ": ", errcode, " ", sqlstate)
return false
end
-- -- 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", total_time - 24 * 60 * 60))
-- ngx.log(ngx.NOTICE, sql_insert)
-- local res, err, errcode, sqlstate = db:query(sql_insert)
-- if not res then
-- ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
-- return
-- end
-- -- 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", total_time - 24 * 60 * 60))
-- ngx.log(ngx.NOTICE, sql_delete)
-- local res, err, errcode, sqlstate = db:query(sql_delete)
-- if not res then
-- ngx.log(ngx.ERR, "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
-- return
-- end
-- ngx.log(ngx.NOTICE, "-----------------------> END")
zx_base:close_db(db)
return true
end
local function async(i, total_time)
local co = coroutine.wrap(
function()
ticket_sql(i, total_time)
end
)
table.insert(tasks, co)
local function END()
zx_base:close_db(db_biz)
logger:END("node_ticket")
end
local function dispatch()
local i = 1
while true do
if tasks[i] == nil then
if tasks[1] == nil then
break
end
i = 1
end
local function ticket_sql()
logger:BEGIN("node_ticket")
local res = tasks[i]()
if not res then
table.remove(tasks, i)
else
i = i + 1
end
-- UPDATE tickets' status
local sql_update = string.format([[UPDATE node_ticket SET ticket_status = 8 WHERE %d > ticket_exp]], os.date("%Y%m%d"))
logger:log(logger:__FILE__() .. ":" .. logger:__LINE__(), "NOTICE: ", sql_update)
local res, err, errcode, sqlstate = db_biz:query(sql_update)
if not res then
logger:log(logger:__FILE__() .. ":" .. logger:__LINE__(), "ERR: ", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
end
function NODE_TICKET.run(self, total_time)
-- local json = loadmod('cjson')
-- -- local dbs_st = zx_base:db_read(db_conf)
function NODE_TICKET.run(self)
for i = 1, #dbs_st.tk_biz do
-- async(dbs_st.tk_biz[i])
ticket_sql(dbs_st.tk_biz[i], total_time)
if BEGIN(dbs_st.tk_biz[i]) then
ticket_sql()
end
END()
end
dispatch()
end
return NODE_TICKET
......@@ -119,8 +119,8 @@ function SETTLE.run()
-- node_vipacct: run()
tk_sync: run()
node_card: run(t)
node_ticket: run(t)
node_card: run()
node_ticket: run()
tk_sdt: run(t)
end
......
This diff is collapsed.
......@@ -7,33 +7,31 @@ local mysql = loadmod("resty.mysql")
local zx_base = loadmod("zx_base")
local json = loadmod('cjson')
local db_conf = "tk7_dbs.json"
local log_lua = loadmod('log')
-- 协程组
local tasks = {}
local db_biz
local db_control
local function EOF()
end
-- 表信息
-- 初始化数据库连接
-- @param table tk_biz 节点机数据库对象
-- table tk_control 总控机数据库对象
-- table tk_table 数据同步表名
-- @return nil
local function sync_sql(tk_biz, tk_control, tk_table)
ngx.log(ngx.NOTICE, "-----------------------> START")
-- @return true/false
local function BEGIN(tk_biz, tk_control)
log_lua:BEGIN("tk_sync")
local err
-- 新建mysql连接
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
db_biz, err = mysql:new()
if not db_biz then
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: " .. "failed to instantiate mysql: ", err)
return false
end
db:set_timeout(1000) -- 1 sec
-- 连接节点机MySQL
local ok, err, errcode, sqlstate = db:connect{
-- 超时设置
db_biz:set_timeout(1000)
-- 连接tk_biz
local ok, err, errcode, sqlstate = db_biz:connect{
host = zx_base:_get_addr(tk_biz['host']),
port = tk_biz['port'],
database = tk_biz['database'],
......@@ -43,39 +41,22 @@ local function sync_sql(tk_biz, tk_control, tk_table)
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, json.encode(tk_biz), ":", err, errcode, sqlstate)
return
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: " .. json.encode(tk_biz), ":", err, errcode, sqlstate)
return false
end
-- 查询节点机
local sql = string.format([[SELECT * FROM %s where update_time > %s]],
tk_table,
os.date("%Y%m%d000000", os.time()-24*60*60)
)
ngx.log(ngx.NOTICE, sql)
local syncs
syncs, err, errcode, sqlstate = db:query(sql)
if not syncs then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_biz), ":",
err, ":", errcode, ":", sqlstate, ".")
return
end
if #syncs == 0 then
return "00", "SUCCESS"
-- 新建mysql连接
db_control, err = mysql:new()
if not db_control then
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: " .. "failed to instantiate mysql: ", err)
return false
end
-- 超时设置
db_control:set_timeout(1000)
-- 重置MySQL连接
zx_base:close_db(db)
db, err = mysql:new()
if not db then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000)
-- 连接总控机MySQL
ok, err, errcode, sqlstate = db:connect{
-- 连接tk_control
local ok, err, errcode, sqlstate = db_control:connect{
host = zx_base:_get_addr(tk_control['host']),
port = tk_control['port'],
database = tk_control['database'],
......@@ -85,63 +66,58 @@ local function sync_sql(tk_biz, tk_control, tk_table)
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, json.encode(tk_control), ":", err, errcode, sqlstate)
return
end
-- 插入总控机
for i = 1, #syncs do
local sync = syncs[i]
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)
if not ok then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_control), ":",
err, ":", errcode, ":", sqlstate, ".")
end
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: " .. json.encode(tk_control), ":", err, errcode, sqlstate)
return false
end
ngx.log(ngx.NOTICE, "-----------------------> END")
return true;
end
-- 清理连接句柄
-- @param nil
-- @return nil
local function END()
zx_base:close_db(db_biz)
zx_base:close_db(db_control)
log_lua:END("tk_sync")
end
-- 异步运行
-- 表信息
-- @param table tk_biz 节点机数据库对象
-- table tk_control 总控机数据库对象
-- table tk_table 数据同步表名
-- @return nil
local function async(tk_biz, tk_control, tk_table)
local co = coroutine.wrap(
function()
sync_sql(tk_biz, tk_control, tk_table)
end
)
table.insert(tasks, co)
end
local function sync_sql(tk_biz, tk_control, tk_table)
-- 协程调度
-- @param nil
-- @return nil
local function dispatch()
local i = 1
while true do
if tasks[i] == nil then
if tasks[1] == nil then
break
end
i = 1
end
-- 查询节点机
local sql = string.format([[SELECT * FROM %s where update_time > %s]],
tk_table,
os.date("%Y%m%d000000", os.time()-24*60*60)
)
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "NOTICE: ", sql)
local syncs, err, errcode, sqlstate = db_biz:query(sql)
if not syncs then
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", "failed to query:", json.encode(tk_biz), ":",
err, ":", errcode, ":", sqlstate, ".")
return
end
if #syncs == 0 then
return "00", "SUCCESS"
end
local res = tasks[i]()
if not res then
table.remove(tasks, i)
else
i = i + 1
-- 插入总控机
for i = 1, #syncs do
local sync = syncs[i]
local sql_result = zx_base:sql_concate("duplicate", tk_table, sync)
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "NOTICE: ", sql_result)
local ok, err, errcode, sqlstate = db_control:query(sql_result)
if not ok then
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "ERR: ", "failed to query:", json.encode(tk_control), ":",
err, ":", errcode, ":", sqlstate, ".")
end
end
end
......@@ -149,12 +125,15 @@ end
-- @param nil
-- @return nil
function TK_SYNC.run()
-- local dbs_st = zx_base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i], dbs_st.tk_control, "tk_shop")
async(dbs_st.tk_biz[i], dbs_st.tk_control, "tk_casher")
local tk_biz = dbs_st.tk_biz[i]
local tk_control = dbs_st.tk_control
if BEGIN(tk_biz, tk_control) then
sync_sql(tk_biz, tk_control, "tk_shop")
sync_sql(tk_biz, tk_control, "tk_casher")
end
END()
end
dispatch()
end
......@@ -191,7 +170,10 @@ local function tk_sync()
else
-- 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)
if BEGIN(dbs_st.tk_biz[i], dbs_st.tk_control) then
sync_sql(dbs_st.tk_biz[i], dbs_st.tk_control, data.dbTable)
end
END()
code = "00"
mesg = "SUCCESS"
......@@ -212,9 +194,12 @@ end
function TK_SYNC.sync()
local code, mesg = tk_sync()
-- http eof
ngx.say(json.encode({mesgRetCode=code, mesgRetDesc=mesg}))
ngx.eof()
ngx.log(ngx.NOTICE, ngx.var.request_body)
-- log eof
log_lua:log(log_lua:__FILE__() .. ":" .. log_lua:__LINE__(), "NOTICE: ", ngx.var.request_body)
end
......
......@@ -2,24 +2,6 @@ local ZX_BASE = {__index=_G}
local json = loadmod ('cjson')
function ZX_BASE.__FILE__ () return debug.getinfo(2,'S').source end
function ZX_BASE.__LINE__ () return debug.getinfo(2,'l').currentline end
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 f = io.open(fpath, "a+")
local str = ""
for i=1, select("#", ...) do
local tmp= tostring(select(i, ...))
str = string.format("%s %s", str, tmp)
end
f:write(os.date("<%Y-%m-%d %H:%M:%S> "))
f:write(str)
f:write("\n")
f:close()
end
function ZX_BASE.db_read(self, dbpath)
local f = io.open(ngx.var.conf_path.."/"..dbpath, "r")
local tmp = f:read("a*")
......
......@@ -83,7 +83,6 @@ function _G._read_dns_servers_from_resolv_file(self)
end
end
-- _G.fpath = string.format("/var/log/www/S_%s.log", os.date("%Y%m%d"))
-- _G.f = io.open(_G.fpath, "a+")
......@@ -99,19 +98,25 @@ _G.dbs_st = {
},
tk_control = {
host = "rm-bp1u9rp1p216nb15f.mysql.rds.aliyuncs.com",
port = 3306,
database = "tk_control",
user = "tk_control_user",
password = "KJjsdn%zx3@jr#y57dhh20drYT!z",
-- host = "192.168.254.10",
-- user = "root",
-- password = "control_pwd",
port = 3306,
database = "tk_control",
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",
-- host = "192.168.254.20",
-- user = "root",
-- password = "node_pwd",
port = 3306,
database = "tk_biz",
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