From bb3d302bcbea690c0c92d8db0c4a96e050c2f351 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 9 Feb 2018 12:51:42 +0000 Subject: [PATCH] Add try/catch to daemon --- utility/daemon/daemon.js | 93 ++++++++++++++++++++----------------- utility/daemon/package.json | 2 +- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/utility/daemon/daemon.js b/utility/daemon/daemon.js index ee2d045a..59dd6f6f 100644 --- a/utility/daemon/daemon.js +++ b/utility/daemon/daemon.js @@ -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) { diff --git a/utility/daemon/package.json b/utility/daemon/package.json index 018f0db9..089a8c26 100644 --- a/utility/daemon/package.json +++ b/utility/daemon/package.json @@ -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" : { },