Slave: wait before sync on master-change

When Update master event occurs, master needs time to sync its missing data, so slaves should sync only after BACKUP_INTERVAL
This commit is contained in:
sairajzero 2022-02-18 00:11:55 +05:30
parent 15d36eb088
commit 1fcd62f2e5
2 changed files with 6 additions and 5 deletions

View File

@ -154,7 +154,7 @@ function connectToMaster(i = 0, init = false) {
connectWS(floID).then(ws => {
ws.floID = floID;
ws.onclose = () => connectToMaster(i);
serveAsSlave(ws);
serveAsSlave(ws, init);
}).catch(error => {
console.log(`Node(${floID}) is offline`);
connectToMaster(i + 1, init)
@ -170,10 +170,10 @@ function serveAsMaster(init) {
app.resume();
}
function serveAsSlave(ws) {
function serveAsSlave(ws, init) {
console.debug('Starting slave process');
app.pause();
slave.start(ws);
slave.start(ws, init);
mod = SLAVE_MODE;
}

View File

@ -13,7 +13,7 @@ var masterWS = null; //Container for Master websocket connection
var intervalID = null;
function startSlaveProcess(ws) {
function startSlaveProcess(ws, init) {
if (!ws) throw Error("Master WS connection required");
//stop existing process
stopSlaveProcess();
@ -30,6 +30,7 @@ function startSlaveProcess(ws) {
message.sign = floCrypto.signData(message.type + "|" + message.req_time, global.myPrivKey);
ws.send(JSON.stringify(message));
//start sync
if (init)
requestInstance.open();
intervalID = setInterval(() => requestInstance.open(), BACKUP_INTERVAL);
}