diff --git a/config.yml b/config.yml index b76fb2f..8dcea0a 100644 --- a/config.yml +++ b/config.yml @@ -5,7 +5,7 @@ pingInterval: 300 flushInterval: 30 restartTimeout: 30 queueFlushDelay: 200 -maxBulkLog: 100 +maxBulkLog: 25 upperCaseTable: true serverId: 1 srcDb: diff --git a/mylogger.js b/mylogger.js index d176d03..5038af3 100644 --- a/mylogger.js +++ b/mylogger.js @@ -541,9 +541,9 @@ module.exports = class MyLogger { if (!changes.length) return; - if (this.debug) { - console.debug('Log:'.blue, `[${action}]`.yellow, tableName); - } + if (this.debug) + console.debug('Evt:'.blue, + `[${action}]`[actionColor[action]], `${tableName}: ${changes.length} changes`); this.queue.push({ tableInfo, @@ -562,31 +562,36 @@ module.exports = class MyLogger { async flushQueue() { if (this.isFlushed || this.isFlushing || !this.isOk) return; this.isFlushing = true; - const {conf, db} = this; + const {conf, db, queue} = this; let op; try { - if (this.queue.length) { + if (queue.length) { do { - let appliedOps; + const ops = []; + let txStarted; try { await db.query('START TRANSACTION'); - appliedOps = []; + txStarted = true; - for (let i = 0; i < conf.maxBulkLog && this.queue.length; i++) { - op = this.queue.shift(); - appliedOps.push(op); + for (let i = 0; i < conf.maxBulkLog && queue.length; i++) { + op = queue.shift(); + ops.push(op); await this.applyOp(op); } + this.debug('Queue', `applied: ${ops.length}, remaining: ${queue.length}`); await this.savePosition(op.binlogName, op.evt.nextPosition) await db.query('COMMIT'); } catch(err) { - this.queue = appliedOps.concat(this.queue); - await db.query('ROLLBACK'); + queue.unshift(...ops); + if (txStarted) + try { + await db.query('ROLLBACK'); + } catch (err) {} throw err; } - } while (this.queue.length); + } while (queue.length); } else { await this.savePosition(this.binlogName, this.binlogPosition); } @@ -638,6 +643,9 @@ module.exports = class MyLogger { : modelId; let deleteRow; + if (this.debug) + console.debug('Log:'.blue, + `[${action}]`[actionColor[action]], `${modelName}: ${modelId}`); try { if (isDelete) { @@ -723,6 +731,12 @@ const actions = { deleterows: 'delete' }; +const actionColor = { + insert: 'green', + update: 'yellow', + delete: 'red' +}; + function toUpperCamelCase(str) { str = str.replace(/[-_ ][a-z]/g, match => match.charAt(1).toUpperCase()); diff --git a/package-lock.json b/package-lock.json index 4037aaa..4d6190b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mylogger", - "version": "0.1.12", + "version": "0.1.13", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mylogger", - "version": "0.1.12", + "version": "0.1.13", "license": "GPL-3.0", "dependencies": { "colors": "^1.4.0", diff --git a/package.json b/package.json index 5f5fd5a..dc396e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mylogger", - "version": "0.1.12", + "version": "0.1.13", "author": "Verdnatura Levante SL", "description": "MySQL and MariaDB logger using binary log", "license": "GPL-3.0",