Compare commits

..

No commits in common. "master" and "6449-summaryId" have entirely different histories.

2 changed files with 22 additions and 16 deletions

View File

@ -57,9 +57,12 @@ module.exports = class MyLogger {
async init() {
const {conf} = this;
this.debug('MyLogger', 'Initializing.');
this.onErrorListener = err => this.onError(err);
// DB connection
const db = this.db = await mysql.createConnection(conf.dstDb);
db.on('error', this.onErrorListener);
await this.modelLoader.loadSchema();
await this.showDb.loadSchema();
@ -131,6 +134,8 @@ module.exports = class MyLogger {
if (!this.running) return;
this.running = false;
this.debug('MyLogger', 'Ending.');
this.db.off('error', this.onErrorListener);
clearInterval(this.flushInterval);
clearInterval(this.pingInterval);
@ -201,6 +206,7 @@ module.exports = class MyLogger {
zongji.once('error', onError);
zongji.start(zongjiOpts);
});
zongji.on('error', this.onErrorListener);
this.zongji = zongji;
this.debug('Zongji', 'Started.');
}
@ -214,6 +220,7 @@ module.exports = class MyLogger {
this.zongji = null;
zongji.off('binlog', this.onBinlogListener);
zongji.off('error', this.onErrorListener);
// FIXME: Cannot call Zongji.stop(), it doesn't wait to end connection
zongji.connection.destroy(() => {
@ -243,7 +250,7 @@ module.exports = class MyLogger {
}
}
async handleError(err) {
async onError(err) {
if (!this.isOk) return;
this.isOk = false;
console.log(`Error: ${err.code}: ${err.message}`);
@ -252,19 +259,19 @@ module.exports = class MyLogger {
await this.end(true);
} catch(e) {}
// FIXME: Error of mysql2/promise
if (err.message === `Can't add new command when connection is in closed state`)
switch (err.code) {
case 'PROTOCOL_CONNECTION_LOST':
case 'ECONNRESET':
console.log('Trying to restart process.');
await this.tryRestart();
else
switch (err.code) {
case 'PROTOCOL_CONNECTION_LOST':
case 'ECONNRESET':
console.log('Trying to restart process.');
await this.tryRestart();
break;
default:
process.exit();
}
break;
default:
process.exit();
}
}
handleError(err) {
console.error(err);
}
async onBinlog(evt) {
@ -485,7 +492,6 @@ module.exports = class MyLogger {
const isDelete = action == 'delete';
const isUpdate = action == 'update';
const created = new Date(evt.timestamp);
const showId = tableInfo.conf.showId;
for (const change of changes) {
let newI, oldI;
@ -508,7 +514,7 @@ module.exports = class MyLogger {
oldI = change.instance;
break;
}
const summaryId = showId ? row[showId] : null;
const summaryId = row[tableInfo.conf.showId];
const modelId = row[tableInfo.idName];
const modelValue = change.modelValue ?? null;
const oldInstance = oldI ? JSON.stringify(oldI) : null;

View File

@ -1,6 +1,6 @@
{
"name": "mylogger",
"version": "1.1.5",
"version": "1.1.4",
"author": "Verdnatura Levante SL",
"description": "MySQL and MariaDB logger using binary log",
"license": "GPL-3.0",