Skip to content

Commit

Permalink
LR v2.3.6: added connection checks in Board Manager @ boot.
Browse files Browse the repository at this point in the history
  • Loading branch information
npeditto committed Jun 19, 2019
1 parent 78d85e6 commit c5e2535
Show file tree
Hide file tree
Showing 11 changed files with 988 additions and 331 deletions.
7 changes: 7 additions & 0 deletions etc/rsyslog/rsyslog.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(load="imuxsock")
module(load="imklog")

$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

*.* /var/log/messages
*.* @RSYSLOG_SERVER
101 changes: 11 additions & 90 deletions lightning-rod.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@ manageBoard.Init_Ligthning_Rod(function (check) {
//----------------------------------------
var wampUrl = url_wamp+":"+port_wamp+"/ws";

wampIP = wampUrl.split("//")[1].split(":")[0];
try{
wampIP = wampUrl.split("//")[1].split(":")[0];

}
catch(err){
logger.warn('[WAMP] - Error in WAMP url format: '+ err);
process.exit(1);
}


//var wampRealm = nconf.get('config:wamp:realm');
var wampRealm = realm;
Expand Down Expand Up @@ -152,11 +160,12 @@ manageBoard.Init_Ligthning_Rod(function (check) {
logger.info('[WAMP] |--> Realm: '+ wampRealm);
logger.info('[WAMP] |--> Session ID: '+ session._id);
//logger.debug('[WAMP] |--> Connection details:\n'+ JSON.stringify(details));
logger.info('[WAMP] |--> socket check timing: '+ wamp_socket_recovery + " - Timing: " + rpc_alive_time + " seconds");

//--WAMP-RECOVERY-SYSTEM-INIT-------------------------------------------------------------------------------
if(wamp_socket_recovery == true || wamp_socket_recovery == "true") {

logger.info('[WAMP] |--> socket check timing: '+ wamp_socket_recovery + " - Timing: " + rpc_alive_time + " seconds");

RECOVERY_SESSION = session;
socketNotAlive = false;

Expand Down Expand Up @@ -303,94 +312,6 @@ manageBoard.Init_Ligthning_Rod(function (check) {
});


/*
// Check if the tcpkill process was killed after a previous connection recovery through this check we will avoid to start another tcpkill process
var tcpkill_status = running(tcpkill_pid);
logger.warn("[WAMP-RECOVERY] - TCPKILL STATUS: " + tcpkill_status + " - PID: " + tcpkill_pid);
// at LR startup "tcpkill_pid" is NULL and in this condition "is-running" module return "true" that is a WRONG result!
if (tcpkill_status === false || tcpkill_pid == null){
logger.warn("[WAMP-RECOVERY] - TCPKILL - Cleaning WAMP zombie-socket...");
var tcpkill_kill_count = 0;
//e.g.: tcpkill -9 port 8181
tcpkill = spawn('tcpkill',['-9','port', port_wamp]);
tcpkill_pid = tcpkill.pid;
tcpkill.stdout.on('data', function (data) {
logger.debug('[WAMP-RECOVERY] ... tcpkill stdout: ' + data);
});
tcpkill.stderr.on('data', function (data) {
logger.debug('[WAMP-RECOVERY] ... tcpkill stderr:\n' + data);
//it will check if tcpkill is in listening state on the port 8181
if(data.toString().indexOf("listening") > -1){
// LISTENING: to manage the starting of tcpkill (listening on port 8181)
}else if (data.toString().indexOf("win 0") > -1){
// TCPKILL DETECTED WAMP ACTIVITY (WAMP reconnection attempts)
// This is the stage triggered when the WAMP socket was killed by tcpkill and WAMP reconnection process automaticcally started:
// in this phase we need to kill tcpkill to allow WAMP reconnection.
try{
logger.debug('[WAMP-RECOVERY] ... killing tcpkill process with PID: ' + tcpkill_pid);
tcpkill.kill('SIGKILL'); //process.kill(tcpkill_pid);
//double check: It will test after a while if the tcpkill process has been killed, OTHERWISE reboot board.
setTimeout(function(){
if ( running(tcpkill_pid)){
//manageBoard.execAction(['reboot']);
tcpkill.kill('SIGKILL'); //process.kill(tcpkill_pid);
//wampConnection.close();
}
}, 2000);
}catch (e) {
logger.error('[WAMP-RECOVERY] ... tcpkill killing error: ', e);
}
}
});
tcpkill.on('close', function (code) {
logger.debug('[WAMP-RECOVERY] ... tcpkill killed [code: '+code+']');
logger.info("[WAMP-RECOVERY] - WAMP socket cleaned!");
//The previous WAMP socket was KILLED and the automatic WAMP recovery process will start
//so the connection recovery is completed and "online" flag is set again to TRUE
online = true;
socketNotAlive = false;
clearTimeout(expectIotronicResponse);
});
}
else{
logger.warn('[WAMP-RECOVERY] ...tcpkill already started!');
}
*/


}


Expand Down
170 changes: 150 additions & 20 deletions modules/board-manager/board-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ exports.moduleLoaderOnBoot = function() {
logger.info("[SYSTEM] - Modules loading:");

var configFile = JSON.parse(fs.readFileSync(SETTINGS, 'utf8'));

var modules = configFile.config["board"]["modules"]; //console.log(modules);

var modules_keys = Object.keys( modules ); //console.log(modules_keys);


//STARTING ENABLED MANAGERS
for (var i = 0; i < modules_keys.length; i++) {

(function (i) {
Expand Down Expand Up @@ -261,11 +261,9 @@ exports.moduleLoaderOnBoot = function() {
nodeRedManager.Boot();
break;


case 'vnets_manager':
logger.info("[SYSTEM] --> " + module_name + ": " + enabled);
break;

case 'vnets_manager':
logger.info("[SYSTEM] --> " + module_name + ": " + enabled);
break;

case 'gpio_manager':
logger.info("[SYSTEM] --> " + module_name + ": " + enabled);
Expand All @@ -285,7 +283,6 @@ exports.moduleLoaderOnBoot = function() {
break;



}


Expand All @@ -294,9 +291,145 @@ exports.moduleLoaderOnBoot = function() {
}


};
//START BOARD-MANAGER CONNECTIONS TESTS PROCEDURES
if(wamp_socket_recovery == true || wamp_socket_recovery == "true") {

logger.info('[BOOT] - Board Manager connection controller starting...');
logger.info('[BOOT] --> Crossbar IP: ' + wampIP + " - Port: " + port_wamp);

// connectionTester: library used to check the reachability of Iotronic-Server/WAMP-Server
var connectionTester = require('connection-tester');

conn_alive_timer = 60; //second between check-connection retries
conn_retry_counter = 0; //counter for check-connection retries

setTimeout(function () {

var output = connectionTester.test(wampIP, port_wamp, 10000);
var reachable = output.success;
var error_test = output.error;

if (!reachable) {

//CONNECTION STATUS: FALSE
logger.warn("[BOARD-CONNECTION-RECOVERY] - INTERNET CONNECTION STATUS: " + reachable + " - ERROR: " + error_test);

checkInternetWampConnection = setInterval(function () {

//logger.warn("[BOARD-CONNECTION-RECOVERY] - RETRY...");

connectionTester.test(wampIP, port_wamp, 10000, function (err, output) {

var reachable = output.success;
var error_test = output.error;

if (!reachable) {

//CONNECTION STATUS: FALSE
logger.warn("[BOARD-CONNECTION-RECOVERY] - INTERNET CONNECTION STATUS: " + reachable + " - ERROR: " + error_test);

} else {

try {

// Test if IoTronic is connected to the realm
board_session.call("s4t.iotronic.isAlive", [boardCode]).then(
function (response) {

conn_retry_counter = 0;
clearInterval(checkInternetWampConnection);

},
function (err) {

logger.warn("NO WAMP CONNECTION YET!")

}
);

} catch (err) {
logger.warn('[BOARD-CONNECTION-RECOVERY] - Error calling "s4t.iotronic.isAlive"');
}


}

});


}, conn_alive_timer * 1000);

} else {

checkInternetWampConnection = setInterval(function () {

conn_retry_counter = conn_retry_counter + 1;

try {

// Test if IoTronic is connected to the realm
board_session.call("s4t.iotronic.isAlive", [boardCode]).then(
function (response) {

conn_retry_counter = 0;
clearInterval(checkInternetWampConnection);

},
function (err) {

logger.warn("[CONN-RETRY] - " + conn_retry_counter);
logger.warn("[BOARD-CONNECTION-RECOVERY] - No WAMP connection yet!")

}
);

} catch (err) {

logger.warn("[CONN-RETRY] - " + conn_retry_counter);
logger.warn('[BOARD-CONNECTION-RECOVERY] - Internet connection available BUT wamp session not established!');
logger.warn("--> WAMP connection error:" + err);

}

if (conn_retry_counter >= 5) {

logger.warn("LR restarting in 5 seconds");

restart_time = 5;

// activate listener on-exit event after LR exit on-update-conf
process.on("exit", function () {

require("child_process").spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached: true,
stdio: "inherit"
});

});

//Restarting LR
setTimeout(function () {

process.exit();

}, restart_time * 1000);


}


}, conn_alive_timer * 1000);

}


}, 5000);

}


};


// Init() LR function in order to control the correct LR configuration:
Expand Down Expand Up @@ -347,8 +480,13 @@ exports.Init_Ligthning_Rod = function (callback) {

} else {

log4js.addAppender(log4js.appenders.file(logfile));
logger = log4js.getLogger('main'); //service logging configuration: "main"
try{
log4js.addAppender(log4js.appenders.file(logfile));
logger = log4js.getLogger('main'); //service logging configuration: "main"
}
catch (err) {
console.log("Error in log folder creation!")
}

LogoLR();

Expand Down Expand Up @@ -938,13 +1076,10 @@ exports.execAction = function(args){
case 'restart_lr':

try{

var params = JSON.parse(params);

}
catch (err) {


logger.debug("[SYSTEM] --> parsing parameters error: "+JSON.stringify(err));

}
Expand All @@ -959,9 +1094,6 @@ exports.execAction = function(args){

}




// activate listener on-exit event after LR exit on-update-conf
logger.debug("[SYSTEM] --> Listener on process 'exit' event activated:");
logger.debug("[SYSTEM] --> Lightning-rod PID: " + process.pid);
Expand Down Expand Up @@ -1002,15 +1134,12 @@ exports.execAction = function(args){
}




return d.promise;


};



var managePackage = function (pkg_mng, pkg_mng_cmd, pkg_opts, pkg_name, callback) {

var response = {
Expand Down Expand Up @@ -1141,6 +1270,7 @@ exports.getLRversion = function () {

};


// Login to Crossbar server and to Iotornic
exports.IotronicLogin = function (session) {

Expand Down
Loading

0 comments on commit c5e2535

Please sign in to comment.