112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
const utils = require("./utils");
|
|
const db = require("./db");
|
|
const sync = require("./sync");
|
|
const metamgr = require("./metamgr");
|
|
const log = require("./log");
|
|
const C = require("./C");
|
|
|
|
class OrderMgr {
|
|
|
|
constructor() {
|
|
this.pendingOrderHash = {};
|
|
this.historyOrderCond = new sync.Cond();
|
|
this.newOrderCond = new sync.Cond();
|
|
utils.registerEventHandler(
|
|
C.REMOVE_PENDING_ORDER_EVENT,
|
|
(orderId) => {
|
|
this.removePendingOrder(orderId);
|
|
}
|
|
);
|
|
}
|
|
|
|
init() {
|
|
setTimeout(this.start.bind(this), 1000 * 0.1);
|
|
}
|
|
|
|
async start() {
|
|
const {err, row} = await db.execQueryOne(
|
|
'SELECT max(idx) max_idx FROM t_box_order', []);
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
const hisMaxIdx = Math.max(0, utils.emptyReplace(row['max_idx'], 0));
|
|
setTimeout(this.historyOrder.bind(this, hisMaxIdx), 1000 * 0.1);
|
|
setTimeout(this.newOrder.bind(this, hisMaxIdx), 1000 * 0.1);
|
|
}
|
|
|
|
async historyOrder(hisMaxIdx) {
|
|
let lastSyncIdx = hisMaxIdx;
|
|
while (lastSyncIdx > 0) {
|
|
const startIdx = Math.max(0, lastSyncIdx - 1000);
|
|
const {err, rows} = await db.execQuery(
|
|
'SELECT * FROM t_box_order WHERE idx > ? AND idx <= ? AND done = 0 AND suspend = 0 LIMIT 1000',
|
|
[startIdx, lastSyncIdx]);
|
|
if (!err) {
|
|
this.procHisOrderList(rows);
|
|
lastSyncIdx = startIdx;
|
|
if (rows.length <= 0) {
|
|
continue;
|
|
}
|
|
}
|
|
await this.historyOrderCond.wait(1000 * 1);
|
|
}
|
|
log.info('history order loaded done hisMaxIdx:' + hisMaxIdx);
|
|
}
|
|
|
|
async newOrder(hisMaxIdx) {
|
|
let lastSyncIdx = hisMaxIdx;
|
|
while (true) {
|
|
const {err, rows} = await db.execQuery(
|
|
'SELECT * FROM t_box_order WHERE idx > ? AND done = 0 AND suspend = 0 LIMIT 100',
|
|
[lastSyncIdx]);
|
|
if (!err) {
|
|
const maxIdx = this.procNewOrderList(rows);
|
|
if (maxIdx > lastSyncIdx) {
|
|
lastSyncIdx = maxIdx;
|
|
}
|
|
}
|
|
await this.newOrderCond.wait(1000 * 3);
|
|
}
|
|
}
|
|
|
|
newOrderNotify() {
|
|
this.newOrderCond.notifyAll();
|
|
}
|
|
|
|
procHisOrderList(rows) {
|
|
return this.internalProcOrderList(rows, false);
|
|
}
|
|
|
|
procNewOrderList(rows) {
|
|
return this.internalProcOrderList(rows, true);
|
|
}
|
|
|
|
internalProcOrderList(rows, isNewOrder) {
|
|
let maxIdx = 0;
|
|
rows.forEach(function (orderDb) {
|
|
this.addPendingOrder(orderDb, isNewOrder);
|
|
if (orderDb['idx'] > maxIdx) {
|
|
maxIdx = orderDb['idx'];
|
|
}
|
|
}.bind(this));
|
|
return maxIdx;
|
|
}
|
|
|
|
addPendingOrder(orderDb, isNewOrder) {
|
|
if (orderDb['order_id'] in this.pendingOrderHash) {
|
|
utils.throwError('order_id exists ' + orderDb['order_id']);
|
|
}
|
|
const boxorder = require("./boxorder");
|
|
const boxOrder = new boxorder.BoxOrder(orderDb, isNewOrder);
|
|
this.pendingOrderHash[orderDb['order_id']] = boxOrder;
|
|
boxOrder.init();
|
|
}
|
|
|
|
removePendingOrder(orderId) {
|
|
delete this.pendingOrderHash[orderId];
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new OrderMgr();
|