84 lines
2.2 KiB
JavaScript
84 lines
2.2 KiB
JavaScript
const app = require('j7/app');
|
|
const utils = require('j7/utils');
|
|
const bcutils = require('j7/bcutils');
|
|
const log = require('j7/log');
|
|
const BaseService = require('./baseservice');
|
|
const metaFactory = require('../metadata/factory');
|
|
|
|
class DbEventProcess extends BaseService {
|
|
|
|
static #tableMaxIdxHash = {};
|
|
|
|
static async staticInit() {
|
|
const {err, conn} = await app.getDbConn('BcEventDb0');
|
|
const tables = metaFactory.getAllTables();
|
|
await utils.serial
|
|
(
|
|
Object.values(tables),
|
|
async (tblObj) => {
|
|
{
|
|
const {err, maxIdx} = await conn.getMaxIdx(tblObj['table_name']);
|
|
if (err) {
|
|
throw 'DbEventProcess error:' + err;
|
|
}
|
|
DbEventProcess.#tableMaxIdxHash[tblObj['table_name']] = maxIdx;
|
|
}
|
|
const updateMaxIdxFunc = async () => {
|
|
while (true) {
|
|
const {err, maxIdx} = await conn.getMaxIdx(tblObj['table_name']);
|
|
if (!err) {
|
|
DbEventProcess.#tableMaxIdxHash[tblObj['table_name']] = maxIdx;
|
|
await utils.sleep(500 + utils.randRange(500, 1500));
|
|
} else {
|
|
await utils.sleep(5000 + utils.randRange(500, 1500));
|
|
}
|
|
}
|
|
};
|
|
updateMaxIdxFunc();
|
|
}
|
|
);
|
|
}
|
|
|
|
async init(net, event) {
|
|
const {err, conn} = await app.getDbConn('BcEventDb0');
|
|
this.conn = conn;
|
|
this.net = net;
|
|
this.event = event;
|
|
this.lastIdx = BigInt(0);
|
|
this.eventConf = this.event['eventConf'];
|
|
this.progInfo = this.event['progressInfo'];
|
|
await this.start();
|
|
}
|
|
|
|
async start() {
|
|
while (true) {
|
|
await this.pullEvent();
|
|
await utils.sleep(500 + utils.randRange(500, 1500));
|
|
}
|
|
}
|
|
|
|
async pullEvent() {
|
|
const logHead = this.getInstanceName() + ' pullEvent: ';
|
|
}
|
|
|
|
getEventName() {
|
|
return this.eventConf['event_name'];
|
|
}
|
|
|
|
getContractAddress() {
|
|
//return this.bc.getContractAddressByName(this.getContractName());
|
|
}
|
|
|
|
getContractName() {
|
|
return this.eventConf['contract_name'];
|
|
}
|
|
|
|
getInstanceName() {
|
|
const instName = this.getNetId() + '.' + this.getContractName() + '.' + this.getEventName();
|
|
return instName;
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = DbEventProcess;
|