1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

tidy up ping node listeners

This commit is contained in:
Dave Conway-Jones 2020-07-01 11:16:20 +01:00
parent 41d9f18608
commit 5ba989f09d
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
2 changed files with 21 additions and 15 deletions

View File

@ -7,7 +7,7 @@ module.exports = function(RED) {
function doPing(node, host, arrayMode){ function doPing(node, host, arrayMode){
const defTimeout = 5000; const defTimeout = 5000;
var ex, hostOptions, commandLineOptions; var ex, hostOptions, commandLineOptions;
if(typeof host === "string"){ if (typeof host === "string") {
hostOptions = { hostOptions = {
host: host, host: host,
timeout: defTimeout timeout: defTimeout
@ -22,7 +22,7 @@ module.exports = function(RED) {
var timeoutS = Math.round(hostOptions.timeout / 1000); //whole numbers only var timeoutS = Math.round(hostOptions.timeout / 1000); //whole numbers only
var msg = { payload:false, topic:hostOptions.host }; var msg = { payload:false, topic:hostOptions.host };
//only include the extra msg object if operating in advance/array mode. //only include the extra msg object if operating in advance/array mode.
if(arrayMode){ if (arrayMode) {
msg.ping = hostOptions msg.ping = hostOptions
} }
if (plat == "linux" || plat == "android") { if (plat == "linux" || plat == "android") {
@ -42,7 +42,13 @@ module.exports = function(RED) {
//monitor every spawned process & SIGINT if too long //monitor every spawned process & SIGINT if too long
var spawnTout = setTimeout(() => { var spawnTout = setTimeout(() => {
node.log(`ping - Host '${hostOptions.host}' process timeout - sending SIGINT`) node.log(`ping - Host '${hostOptions.host}' process timeout - sending SIGINT`)
try{if(ex && ex.pid){ ex.kill("SIGINT"); }} catch(e){console.warn(e)} try {
if (ex && ex.pid) {
ex.removeAllListeners();
ex.kill("SIGINT");
}
}
catch(e) { console.warn(e) }
}, hostOptions.timeout+1000); //add 1s for grace }, hostOptions.timeout+1000); //add 1s for grace
var res = false; var res = false;
@ -50,9 +56,11 @@ module.exports = function(RED) {
var fail = false; var fail = false;
//var regex = /from.*time.(.*)ms/; //var regex = /from.*time.(.*)ms/;
var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/; var regex = /=.*[<|=]([0-9]*).*TTL|ttl..*=([0-9\.]*)/;
ex.stdout.on("data", function (data) { if (ex && ex.hasOwnProperty("stdout")) {
line += data.toString(); ex.stdout.on("data", function (data) {
}); line += data.toString();
});
}
ex.on("exit", function (err) { ex.on("exit", function (err) {
clearTimeout(spawnTout); clearTimeout(spawnTout);
}); });
@ -95,14 +103,14 @@ module.exports = function(RED) {
if (node.tout) { clearInterval(node.tout); } if (node.tout) { clearInterval(node.tout); }
} }
if(node.mode === "triggered"){ if (node.mode === "triggered") {
clearPingInterval(); clearPingInterval();
} else if(node.timer){ } else if (node.timer) {
node.tout = setInterval(function() { node.tout = setInterval(function() {
let pingables = generatePingList(node.host); let pingables = generatePingList(node.host);
for (let index = 0; index < pingables.length; index++) { for (let index = 0; index < pingables.length; index++) {
const element = pingables[index]; const element = pingables[index];
if(element){ doPing(node, element, false); } if (element) { doPing(node, element, false); }
} }
}, node.timer); }, node.timer);
} }
@ -110,16 +118,16 @@ module.exports = function(RED) {
this.on("input", function (msg) { this.on("input", function (msg) {
let node = this; let node = this;
let payload = node.host || msg.payload; let payload = node.host || msg.payload;
if(typeof payload == "string"){ if (typeof payload == "string") {
let pingables = generatePingList(payload) let pingables = generatePingList(payload)
for (let index = 0; index < pingables.length; index++) { for (let index = 0; index < pingables.length; index++) {
const element = pingables[index]; const element = pingables[index];
if(element){ doPing(node, element, false); } if (element) { doPing(node, element, false); }
} }
} else if (Array.isArray(payload) ) { } else if (Array.isArray(payload) ) {
for (let index = 0; index < payload.length; index++) { for (let index = 0; index < payload.length; index++) {
const element = payload[index]; const element = payload[index];
if(element){ doPing(node, element, true); } if (element) { doPing(node, element, true); }
} }
} }
}); });
@ -127,8 +135,6 @@ module.exports = function(RED) {
this.on("close", function() { this.on("close", function() {
clearPingInterval(); clearPingInterval();
}); });
} }
RED.nodes.registerType("ping",PingNode); RED.nodes.registerType("ping",PingNode);
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-ping", "name" : "node-red-node-ping",
"version" : "0.2.0", "version" : "0.2.1",
"description" : "A Node-RED node to ping a remote server, for use as a keep-alive check.", "description" : "A Node-RED node to ping a remote server, for use as a keep-alive check.",
"dependencies" : { "dependencies" : {
}, },