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() {
var line = "";
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
node.status({fill:"grey",shape:"ring",text:"no command"});
return;
}
node.child = spawn(node.cmd, node.args);
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
node.status({fill:"green",shape:"dot",text:"running"});
node.running = true;
var line = "";
try {
node.child = spawn(node.cmd, node.args);
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
node.status({fill:"green",shape:"dot",text:"running"});
node.running = true;
node.child.stdout.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("out: "+data); }
if (node.op === "lines") {
line += data.toString();
var bits = line.split("\n");
while (bits.length > 1) {
node.send([{payload:bits.shift()},null,null]);
node.child.stdout.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("out: "+data); }
if (node.op === "lines") {
line += data.toString();
var bits = line.split("\n");
while (bits.length > 1) {
node.send([{payload:bits.shift()},null,null]);
}
line = bits[0];
}
line = bits[0];
}
else {
if (data && (data.length !== 0)) {
node.send([{payload:data},null,null]);
else {
if (data && (data.length !== 0)) {
node.send([{payload:data},null,null]);
}
}
}
});
});
node.child.stderr.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("err: "+data); }
node.send([null,{payload:data},null]);
});
node.child.stderr.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
if (node.op === "number") { data = Number(data); }
if (RED.settings.verbose) { node.log("err: "+data); }
node.send([null,{payload:data},null]);
});
node.child.on('close', function (code,signal) {
if (RED.settings.verbose) { node.log("ret: "+code+":"+signal); }
node.child.on('close', function (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.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) {

View File

@ -1,6 +1,6 @@
{
"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.",
"dependencies" : {
},