Last active
August 4, 2023 09:07
-
-
Save numtel/5b37b2a7f47b380c1a099596c6f3db2f to your computer and use it in GitHub Desktop.
Restart ZongJi gracefully on error
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
var ZongJi = require('zongji'); | |
var RETRY_TIMEOUT = 4000; | |
function zongjiManager(dsn, options, onBinlog) { | |
var newInst = new ZongJi(dsn, options); | |
newInst.on('error', function(reason) { | |
newInst.removeListener('binlog', onBinlog); | |
setTimeout(function() { | |
// If multiple errors happened, a new instance may have already been created | |
if(!('child' in newInst)) { | |
newInst.child = zongjiManager(dsn, Object.assign({}, options, { | |
binlogName: newInst.binlogName, | |
binlogNextPos: newInst.binlogNextPos | |
}), onBinlog); | |
newInst.emit('child', newInst.child, reason); | |
newInst.child.on('child', child => newInst.emit('child', child)); | |
} | |
}, RETRY_TIMEOUT); | |
}); | |
newInst.on('binlog', onBinlog); | |
newInst.start(options); | |
return newInst; | |
} | |
// To check if it works | |
var eventCount = 0; | |
setInterval(function() { console.log('Events:', eventCount) }, 2000); | |
var zongji = zongjiManager( | |
// Pass the connection settings | |
{ | |
host : 'localhost', | |
user : 'root', | |
password : 'numtel', | |
}, | |
// Pass the options | |
// Must include rotate events for binlogName and binlogNextPos properties | |
{ | |
includeEvents: ['rotate', 'tablemap', 'writerows', 'updaterows', 'deleterows'], | |
}, | |
// Binlog callback that will be attached each time Zongji is restarted | |
function(event) { | |
eventCount++ | |
event.dump(); | |
}); | |
var newest = zongji; | |
zongji.on('child', function(child, reason) { | |
console.log('New Instance Created', reason); | |
newest.stop(); | |
newest = child; | |
}); | |
process.on('SIGINT', function() { | |
console.log('Got SIGINT.'); | |
newest.stop(); | |
process.exit(); | |
}); |
correct variables to access binlog filename/position are
newInst.options.filename,
newInst.options.position
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@numtel this will create a new Zongji instance, but the new instance doesn't have the TableMap yet right?
This would mean that events might be filtered out, until we get a new tablemap? Are my assumptions correct? What approach would you recommend here?