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

Add try/catch to daemon

This commit is contained in:
Dave Conway-Jones 2018-02-09 12:51:42 +00:00
parent 3b20b804ae
commit bb3d302bcb
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 52 additions and 43 deletions

View File

@ -36,58 +36,67 @@ module.exports = function(RED) {
} }
function runit() { function runit() {
var line = "";
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) { if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
node.status({fill:"grey",shape:"ring",text:"no command"}); node.status({fill:"grey",shape:"ring",text:"no command"});
return; return;
} }
node.child = spawn(node.cmd, node.args); try {
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); } node.child = spawn(node.cmd, node.args);
node.status({fill:"green",shape:"dot",text:"running"}); if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
node.running = true; node.status({fill:"green",shape:"dot",text:"running"});
var line = ""; node.running = true;
node.child.stdout.on('data', function (data) { node.child.stdout.on('data', function (data) {
if (node.op === "string") { data = data.toString(); } if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); } if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("out: "+data); } if (RED.settings.verbose) { node.log("out: "+data); }
if (node.op === "lines") { if (node.op === "lines") {
line += data.toString(); line += data.toString();
var bits = line.split("\n"); var bits = line.split("\n");
while (bits.length > 1) { while (bits.length > 1) {
node.send([{payload:bits.shift()},null,null]); node.send([{payload:bits.shift()},null,null]);
}
line = bits[0];
} }
line = bits[0]; else {
} if (data && (data.length !== 0)) {
else { node.send([{payload:data},null,null]);
if (data && (data.length !== 0)) { }
node.send([{payload:data},null,null]);
} }
} });
});
node.child.stderr.on('data', function (data) { node.child.stderr.on('data', function (data) {
if (node.op === "string") { data = data.toString(); } if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); } if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("err: "+data); } if (RED.settings.verbose) { node.log("err: "+data); }
node.send([null,{payload:data},null]); node.send([null,{payload:data},null]);
}); });
node.child.on('close', function (code,signal) { node.child.on('close', function (code,signal) {
if (RED.settings.verbose) { node.log("ret: "+code+":"+signal); } if (RED.settings.verbose) { node.log("ret: "+code+":"+signal); }
node.running = false;
node.child = null;
var rc = code;
if (code === null) { rc = signal; }
node.send([null,null,{payload:rc}]);
node.status({fill:"red",shape:"ring",text:"stopped"});
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
node.status({fill:"red",shape:"ring",text:"error"});
});
}
catch(e) {
if (e.errno === "ENOENT") { node.warn('Command not found'); }
else if (e.errno === "EACCES") { node.warn('Command not executable'); }
else { node.error(e); }
node.status({fill:"red",shape:"ring",text:"error"});
node.running = false; node.running = false;
node.child = null; }
var rc = code;
if (code === null) { rc = signal; }
node.send([null,null,{payload:rc}]);
node.status({fill:"red",shape:"ring",text:"stopped"});
});
node.child.on('error', function (err) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
node.status({fill:"grey",shape:"dot",text:"error"});
});
} }
if (node.redo === true) { if (node.redo === true) {

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-daemon", "name" : "node-red-node-daemon",
"version" : "0.0.17", "version" : "0.0.18",
"description" : "A Node-RED node that runs and monitors a long running system command.", "description" : "A Node-RED node that runs and monitors a long running system command.",
"dependencies" : { "dependencies" : {
}, },