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