Commit 8fe82754 authored by 周尚's avatar 周尚

批处理业务 - 营销工具类 [完成]

parent 89aa8f62
......@@ -10,4 +10,29 @@
** TODO 待开发/完善
1. [ ] 异步推送
2. [ ] 业务批处理
2. [-] 业务批处理
1. [ ] 库存信息类
1. [ ] 门店库存
2. [ ] 会员管理类
1. [ ] 会员账户
3. [X] 营销工具
1. [X] 卡账户
2. [X] 券账户
3. [X] 充值优惠
4. [X] 促销计划
4. [ ] 订单流水
1. [ ] 支付流水
2. [ ] 会员充值订单
3. [ ] 门店零售订单
4. [ ] 门店零售明细
5. [ ] 门店进货订单
6. [ ] 门店进货明细
7. [ ] 门店调拨记录
8. [ ] 门店调拨明细
9. [ ] 门店盘库记录
10. [ ] 门店盘库明细
11. [ ] 短信流水
12. [ ] 交班记录
5. [ ] 统计汇总
1. [ ] 门店日统计
2. [ ] 会员日统计
......@@ -19,7 +19,7 @@ http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
#log_format main "'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
......@@ -31,6 +31,8 @@ http {
#keepalive_timeout 0;
keepalive_timeout 65;
init_worker_by_lua_file "lua/settle/settle.lua";
#gzip on;
server {
listen 80 ;
......@@ -61,6 +63,14 @@ http {
index index.html index.htm;
}
location /echo {
content_by_lua_block {
ngx.log(ngx.ERR, ngx.var.request_body)
ngx.say(ngx.var.request_body)
}
}
location /re {
content_by_lua_block {
local regex = [[\d{14}]]
......
{
"tk_sku": {
"host": "192.168.254.100",
"port": 3306,
"database": "tk_sku",
"user": "root",
"password": "",
"timeout": 30
},
"tk_control": {
"host": "192.168.254.10",
"port": 3306,
"database": "tk_control",
"user": "root",
"password": "",
"timeout": 30
},
"tk_biz": [
{
"host": "192.168.254.20",
"port": 3306,
"database": "tk_biz",
"user": "root",
"password": "",
"timeout": 30
},
{
"host": "192.168.254.21",
"port": 3306,
"database": "tk_biz",
"user": "root",
"password": "",
"timeout": 30
}
]
}
<2018-06-23 20:28:58> {
<2018-06-23 20:30:18> {
"host" : "127.0.0.1",
"port" : 3306,
"database" : "zzd_tk",
"user" : "root",
"password" : "",
"timeout" : 30
}
<2018-06-23 20:31:01> {
"host" : "127.0.0.1",
"port" : 3306,
"database" : "zzd_tk",
"user" : "root",
"password" : "",
"timeout" : 30
}
<2018-06-23 19:58:46> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 11, trans_amt = 11.22, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 19:59:11> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 11.22, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 19:59:40> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:11:36> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:12:21> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:12:44> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:12:46> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:13:23> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:19:15> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:21:25> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:22:15> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:22:24> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:23:38> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:23:39> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:23:51> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:24:09> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:24:32> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:24:59> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:25:50> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:26:02> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:26:30> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:27:01> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:28:58> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:30:18> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:31:01> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 20:35:28> INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
<2018-06-23 21:05:57> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 21:05:57> ASYNC_DB {
"host" : "127.0.0.1",
"port" : 3306,
"database" : "zzd_tk",
"user" : "root",
"password" : "",
"timeout" : 30
}
<2018-06-23 21:05:57> ASYNC_DB bad result: No database selected : 1046 : 3D000 .
<2018-06-23 21:06:07> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 21:06:07> ASYNC_DB {
"host" : "127.0.0.1",
"port" : 3306,
"database" : "zzd_tk",
"user" : "root",
"password" : "",
"timeout" : 30
}
<2018-06-23 21:07:07> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 21:08:02> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 21:08:16> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-23 21:08:35> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333, pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
<2018-06-23 22:55:13> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333,
pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
<2018-06-25 11:40:44> ASYNC_SQL INSERT INTO pay_noty_trace SET host_map_id = 3, tkpay_order_no = 11111, channel_order_no = 22222222, host_order_no = 333333333,
pay_type = 12, trans_amt = 1122, trans_time = 20180622130112, trans_status = 0, currency = 'CNY', rate = 305
* 模块说明
| 文件名 | 说明 |
|-------------+----------------------------|
|--------------+----------------------------|
| ~base.lua~ | 基础支持 |
|-------------+----------------------------|
|--------------+----------------------------|
| ~async.lua~ | 唐库6.0 异步通知功能模块 |
|-------------+----------------------------|
|--------------+----------------------------|
| ~sync.lua~ | 唐库7.0 总控-节点 同步模块 |
| ~check.lua~ | 唐库7.0 通用字段校验 |
| | |
| ~settle.lua~ | 批处理业务 |
......@@ -2,20 +2,20 @@ local _BASE = {}
local json = require ('cjson')
function _BASE.print_log(fname, ...)
local f = io.open(string.format("/Users/zhoush/openresty/nginx/logs/%s_%s.log", fname, os.date("%Y%m%d")), "a+")
function _BASE.print_log(self, fname, ...)
local fpath = string.format("/Users/zhoush/openresty/nginx/logs/%s_%s.log", 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,...))
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(os.date("<%Y-%m-%d %H:%M:%S> ")..str)
f:write("\n")
f:close()
end
function _BASE.db_read(dbpath)
function _BASE.db_read(self, dbpath)
local f = io.open("/Users/zhoush/openresty/nginx/conf/"..dbpath, "r")
local tmp = f:read("a*")
local db_st = json.decode(tmp)
......@@ -23,14 +23,14 @@ function _BASE.db_read(dbpath)
return db_st
end
function _BASE.close_db(db)
function _BASE.close_db(self, db)
if not db then
return
end
local ok, err = db:close()
if not ok then
print_log("failed to close:", err)
_BASE.print_log("failed to close:", err)
end
end
......
local _NC = {}
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/?.lua;;"
local mysql = require("resty.mysql")
local base = require("base")
local tasks = {}
local db_conf = "tk7_dbs.json"
local function card_sql(db_st)
local log = ngx.log
local ERR = ngx.ERR
local db, err = mysql:new()
if not db then
base:print_log("S", "[CARD]:", "failed to instantiate mysql: ", err)
return false
end
db:SET_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'],
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
}
if not ok then
base:print_log("S", "[NODE_CARD]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_card WHERE (card_status != 0 AND card_status !=1) or current_time() > card_exp]]
local sql_update = [[UPDATE node_card SET card_status = 8 WHERE current_time() > card_exp]]
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
base:print_log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_CARD", "<SQL>", sql_update)
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)
if not res then
base:print_log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_CARD", "<SQL>", sql_update)
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')]], os.DATE("%Y%m%d%H%M%S"))
local res, err, errcode, sqlstate = db:query(sql_insert)
if not res then
base:print_log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_CARD", "<SQL>", sql_insert)
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"))
local res, err, errcode, sqlstate = db:query(sql_delete)
if not res then
base:print_log("S", "[NODE_CARD]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_CARD", "<SQL>", sql_delete)
return
end
end
local function async(i)
local co = coroutine.wrap(
function()
card_sql(i)
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 _NC.run()
local base = require('base')
local json = require('cjson')
local dbs_st = base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i])
end
dispatch()
end
return _NC
local _CR = {}
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/?.lua;;"
local mysql = require("resty.mysql")
local base = require("base")
local tasks = {}
local db_conf = "tk7_dbs.json"
local function chgrule_sql(db_st)
local log = ngx.log
local ERR = ngx.ERR
local db, err = mysql:new()
if not db then
base:print_log("S", "[CHGRULE]:", "failed to instantiate mysql: ", err)
return false
end
db:SET_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'],
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
}
if not ok then
base:print_log("S", "[NODE_CHGRULE]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_CHGRULE WHERE (card_status != 0 AND card_status !=1) or current_time() > card_exp]]
local sql_update = [[UPDATE node_chgrule SET rule_status = 1 WHERE current_time() > `DATE_end`]]
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
base:print_log("S", "[NODE_CHGRULE]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_CHGRULE", "<SQL>", sql_update)
return
end
end
local function async(i)
local co = coroutine.wrap(
function()
chgrule_sql(i)
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 _CR.run()
local base = require('base')
local json = require('cjson')
local dbs_st = base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i])
end
dispatch()
end
return _CR
local _MS = {}
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/?.lua;;"
local mysql = require("resty.mysql")
local base = require("base")
local tasks = {}
local db_conf = "tk7_dbs.json"
local function ms_sql(db_st)
local log = ngx.log
local ERR = ngx.ERR
local db, err = mysql:new()
if not db then
base:print_log("S", "[MS]:", "failed to instantiate mysql: ", err)
return false
end
db:SET_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'],
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
}
if not ok then
base:print_log("S", "[NODE_MS]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_MS WHERE (card_status != 0 AND card_status !=1) or current_time() > card_exp]]
local sql_update = [[UPDATE node_ms SET ms_status = 1 WHERE current_time() > `ms_date_end`]]
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
base:print_log("S", "[NODE_MS]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_MS", "<SQL>", sql_update)
return
end
end
local function async(i)
local co = coroutine.wrap(
function()
ms_sql(i)
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 _MS.run()
local base = require('base')
local json = require('cjson')
local dbs_st = base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i])
end
dispatch()
end
return _MS
local _NT = {}
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/?.lua;;"
local mysql = require("resty.mysql")
local base = require("base")
local tasks = {}
local db_conf = "tk7_dbs.json"
local function ticket_sql(db_st)
local log = ngx.log
local ERR = ngx.ERR
local db, err = mysql:new()
if not db then
base:print_log("S", "[TICKET]:", "failed to instantiate mysql: ", err)
return false
end
db:SET_timeout(1000) -- 1 sec
local ok, err, errcode, sqlstate = db:connect{
host = db_st['host'],
port = db_st['port'],
database = db_st['database'],
user = db_st['user'],
password = db_st['password'],
timeout = db_st['timeout'],
}
if not ok then
base:print_log("S", "[NODE_TICKET]:", "failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_ticket WHERE (ticket_status != 0 AND ticket_status !=1) or current_time() > ticket_exp]]
local sql_update = [[UPDATE node_ticket SET ticket_status = 8 WHERE current_time() > ticket_exp]]
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
base:print_log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_TICKET", "<SQL>", sql_update)
return
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"))
local res, err, errcode, sqlstate = db:query(sql_insert)
if not res then
base:print_log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_TICKET", "<SQL>", sql_insert)
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"))
local res, err, errcode, sqlstate = db:query(sql_delete)
if not res then
base:print_log("S", "[NODE_TICKET]:", "bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
base:print_log("NODE_TICKET", "<SQL>", sql_delete)
return
end
end
local function async(i)
local co = coroutine.wrap(
function()
ticket_sql(i)
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 _NT.run()
local base = require('base')
local json = require('cjson')
local dbs_st = base:db_read(db_conf)
for i = 1, #dbs_st.tk_biz do
async(dbs_st.tk_biz[i])
end
dispatch()
end
return _NT
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/?.lua;;"
package.path = package.path..";/Users/zhoush/openresty/nginx/lua/settle/?.lua;;"
local base = require('base')
-- Settle modules
local NC = require ("node_card")
local NT = require ('node_ticket')
local delay = 5 -- delaytime
local interval_time = '1200' -- time to run, here is 12:00 for each day
local handler
handler = function(premature, interval_time)
if not premature then
local nowtime = os.date("%H%M")
if nowtime == interval_time then
-- settle features start
NC:run() -- node_card
NC:run() -- node_ticket
-- reset interval_time
interval_time = tostring(tonumber(interval_time) - 1)
end
local ok, err = ngx.timer.at(delay, handler, interval_time)
if not ok then
ngx.log(ngx.ERR, "failed to create the timer:", err)
return
end
end
end
if ngx.worker.id() == 1 then
local ok, err = ngx.timer.at(delay, handler, interval_time)
if not ok then
ngx.log(ngx.ERR, "failed to create the timer:", err)
return
end
end
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