From d20297429a1695116f2427246b4edcd2e1b487c1 Mon Sep 17 00:00:00 2001 From: Moe Date: Fri, 26 Nov 2021 19:48:59 -0800 Subject: [PATCH] Child Node Framework Update : Child Node continuously attempt reconnect to Master Node --- libs/childNode.js | 31 ++++++++++++++++++++++--------- libs/childNode/childUtils.js | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/libs/childNode.js b/libs/childNode.js index d634244c..6057d4bf 100644 --- a/libs/childNode.js +++ b/libs/childNode.js @@ -111,9 +111,28 @@ module.exports = function(s,config,lang,app,io){ onDataFromMasterNode, } = require('./childNode/childUtils.js')(s,config,lang,app,io) s.connectedToMasterNode = false; - const childIO = createWebSocketClient('ws://'+config.childNodes.host + '/childNode',{ - onMessage: onDataFromMasterNode - }) + let childIO; + function createChildNodeConnection(){ + childIO = createWebSocketClient('ws://'+config.childNodes.host + '/childNode',{ + onMessage: onDataFromMasterNode + }) + childIO.on('open', function(){ + console.error(new Date(),'Child Nodes : Connected to Master Node! Authenticating...'); + initiateConnectionToMasterNode() + }) + childIO.on('close',function(){ + onDisconnectFromMasterNode() + setTimeout(() => { + console.error(new Date(),'Child Nodes : Connection to Master Node Closed. Attempting Reconnect...'); + createChildNodeConnection() + },3000) + }) + childIO.on('error',function(err){ + console.error(new Date(),'Child Nodes ERROR : ', err.message); + childIO.close() + }) + } + createChildNodeConnection() function sendDataToMasterNode(data){ childIO.send(JSON.stringify(data)) } @@ -140,11 +159,5 @@ module.exports = function(s,config,lang,app,io){ if(typeof onMoveOn === 'function')s.queuedSqlCallbacks[callbackId] = onMoveOn; sendDataToMasterNode({f:'knex',options:options,callbackId:callbackId}); } - childIO.on('open', function(){ - initiateConnectionToMasterNode() - }) - childIO.on('close',function(){ - onDisconnectFromMasterNode() - }) } } diff --git a/libs/childNode/childUtils.js b/libs/childNode/childUtils.js index 67af1b96..0574db83 100644 --- a/libs/childNode/childUtils.js +++ b/libs/childNode/childUtils.js @@ -13,6 +13,7 @@ module.exports = function(s,config,lang,app,io){ } break; case'init_success': + console.error(new Date(),'Child Nodes : Authenticated with Master Node!'); s.connectedToMasterNode = true; s.other_helpers = d.child_helpers; s.childNodeIdOnMasterNode = d.connectionId @@ -43,7 +44,6 @@ module.exports = function(s,config,lang,app,io){ } } function initiateConnectionToMasterNode(){ - console.log('CHILD CONNECTION SUCCESS') s.cx({ f: 'init', port: config.port,