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

revert ping changes for HA

to address #662
This commit is contained in:
Dave Conway-Jones 2020-11-20 18:07:35 +00:00
parent 4884550215
commit 2d5d553c4d
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
2 changed files with 15 additions and 20 deletions

View File

@ -4,7 +4,7 @@ module.exports = function(RED) {
var spawn = require("child_process").spawn; var spawn = require("child_process").spawn;
var plat = require("os").platform(); var plat = require("os").platform();
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") {
@ -25,30 +25,27 @@ module.exports = function(RED) {
if (arrayMode) { if (arrayMode) {
msg.ping = hostOptions msg.ping = hostOptions
} }
if (plat == "linux" || plat == "android") { if (plat == "linux" || plat == "android") {
commandLineOptions = ["-n", "-w", timeoutS, "-c", "1"] commandLineOptions = ["-n", "-w", timeoutS, "-c", "1"]
} else if (plat.match(/^win/)) { } else if (plat.match(/^win/)) {
commandLineOptions = ["-n", "1", "-w", hostOptions.timeout] commandLineOptions = ["-n", "1", "-w", hostOptions.timeout]
} else if (plat == "darwin" || plat == "freebsd") { } else if (plat == "darwin" || plat == "freebsd") {
commandLineOptions = ["-n", "-t", timeoutS, "-c", "1"] commandLineOptions = ["-n", "-t", timeoutS, "-c", "1"]
} else { } else {
node.error("Sorry - your platform - "+plat+" - is not recognised.", msg); node.error("Sorry - your platform - "+plat+" - is not recognised.", msg);
return; //dont pass go - just return! return; //dont pass go - just return!
} }
//spawn with timeout in case of os issue //spawn with timeout in case of os issue
ex = spawn("ping", [...commandLineOptions, hostOptions.host]); ex = spawn("ping", [...commandLineOptions, hostOptions.host]);
//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 { try {
if (ex && ex.pid) { if (ex && ex.pid) { ex.kill("SIGINT"); }
ex.removeAllListeners(); }
ex.kill("SIGINT"); catch(e) {console.warn(e); }
}
}
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;
@ -56,11 +53,9 @@ 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\.]*)/;
if (ex && ex.hasOwnProperty("stdout")) { ex.stdout.on("data", function (data) {
ex.stdout.on("data", function (data) { line += data.toString();
line += data.toString(); });
});
}
ex.on("exit", function (err) { ex.on("exit", function (err) {
clearTimeout(spawnTout); clearTimeout(spawnTout);
}); });
@ -99,7 +94,7 @@ module.exports = function(RED) {
function generatePingList(str) { function generatePingList(str) {
return (str + "").split(",").map((e) => (e + "").trim()).filter((e) => e != ""); return (str + "").split(",").map((e) => (e + "").trim()).filter((e) => e != "");
} }
function clearPingInterval(){ function clearPingInterval() {
if (node.tout) { clearInterval(node.tout); } if (node.tout) { clearInterval(node.tout); }
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-ping", "name" : "node-red-node-ping",
"version" : "0.2.1", "version" : "0.2.2",
"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" : {
}, },