forked from wj32/mysql-live-select
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* mysql-live-select, MIT License [email protected], [email protected] | ||
lib/LiveMysql.js - Main class */ | ||
lib/LiveMysql.js - Main class */ | ||
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var _ = require('lodash'); | ||
|
@@ -9,14 +9,21 @@ var EJSON = require('ejson'); | |
|
||
// Maximum duration to wait for Zongji to initialize before timeout error (ms) | ||
var ZONGJI_INIT_TIMEOUT = 10000; | ||
var ZONGJI_RETRY_TIMEOUT = 4000; | ||
// First retry (no delay) and then incremented by 2s | ||
let ZONGJI_RETRY_DELAY = 2000; | ||
let retryCount = 0; | ||
const resetRetryCount = () => { | ||
retryCount = 0; | ||
} | ||
|
||
function zongjiManager(dsn, options, onBinlog) { | ||
var newInst = new ZongJi(dsn); | ||
newInst.on('error', function(reason) { | ||
console.log("ZongJi error:"); | ||
console.log(reason); | ||
newInst.removeListener('binlog', onBinlog); | ||
newInst.removeListener('ready', resetRetryCount); | ||
console.log("Retry in (ms): ", ZONGJI_RETRY_DELAY * retryCount) | ||
setTimeout(function() { | ||
// If multiple errors happened, a new instance may have already been created | ||
if(!('child' in newInst)) { | ||
|
@@ -29,13 +36,15 @@ function zongjiManager(dsn, options, onBinlog) { | |
} else { | ||
console.log("Resuming binlog at end"); | ||
} | ||
|
||
newInst.child = zongjiManager(dsn, Object.assign({}, options, resumeoptions), onBinlog); | ||
newInst.emit('child', newInst.child, reason); | ||
newInst.child.on('child', child => newInst.emit('child', child, reason)); | ||
// Inc. retry count | ||
retryCount += 1; | ||
} | ||
}, ZONGJI_RETRY_TIMEOUT); | ||
}, ZONGJI_RETRY_DELAY * retryCount); | ||
}); | ||
newInst.on('ready', resetRetryCount); | ||
newInst.on('binlog', onBinlog); | ||
newInst.start(options); | ||
return newInst; | ||
|
@@ -62,9 +71,9 @@ function LiveMysql(settings, callback) { | |
const binlogSettingsList = ['serverId', 'minInterval', 'checkConditionWhenQueued']; | ||
|
||
binlogSettingsList.forEach(binlogSetting => { | ||
if (binlogSetting in settings) { | ||
binlogSettings[binlogSetting] = settings[binlogSetting]; | ||
delete settings[binlogSetting]; | ||
if (binlogSetting in settings) { | ||
binlogSettings[binlogSetting] = settings[binlogSetting]; | ||
delete settings[binlogSetting]; | ||
} | ||
}); | ||
|
||
|
@@ -75,23 +84,23 @@ function LiveMysql(settings, callback) { | |
self.poolpromise = self.pool.promise(); | ||
self.execute = self.pool.execute.bind(self.pool); | ||
|
||
self.endDbOrPool = function() { | ||
self.pool.end(); | ||
}; | ||
self.endDbOrPool = function() { | ||
self.pool.end(); | ||
}; | ||
|
||
initialConnect = process.nextTick; | ||
initialConnect = process.nextTick; | ||
} | ||
else | ||
{ | ||
self.db = mysql.createConnection(settings); | ||
self.dbpromise = self.db.promise(); | ||
self.execute = self.db.execute.bind(self.db); | ||
|
||
self.endDbOrPool = function() { | ||
self.db.destroy(); | ||
}; | ||
self.endDbOrPool = function() { | ||
self.db.destroy(); | ||
}; | ||
|
||
initialConnect = self.db.connect.bind(self.db); | ||
initialConnect = self.db.connect.bind(self.db); | ||
} | ||
|
||
self.settings = settings; | ||
|
@@ -134,13 +143,13 @@ function LiveMysql(settings, callback) { | |
}); | ||
}); | ||
|
||
var newest = zongji; | ||
var newest = zongji; | ||
|
||
zongji.on('child', function(child, reason) { | ||
//Stop old ZongJi instance and update reference | ||
newest.stop(); | ||
newest = child; | ||
}); | ||
zongji.on('child', function(child, reason) { | ||
//Stop old ZongJi instance and update reference | ||
newest.stop(); | ||
newest = child; | ||
}); | ||
|
||
// Wait for Zongji to be ready before executing callback | ||
var zongjiInitTime = Date.now(); | ||
|