From 95ace249b3a87c92d7f22bd2bc821dd958f83028 Mon Sep 17 00:00:00 2001 From: G S Marzot Date: Thu, 20 Jul 2023 07:15:42 -0400 Subject: [PATCH] add msg.stop handler and docs, msg.start restarts process as per existing doc --- utility/daemon/README.md | 2 ++ utility/daemon/daemon.js | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/utility/daemon/README.md b/utility/daemon/README.md index 95f41c16..52b61eff 100644 --- a/utility/daemon/README.md +++ b/utility/daemon/README.md @@ -36,6 +36,8 @@ Setting `msg.kill` to a signal name (e.g. SIGINT, SIGHUP) will stop the process Sending `msg.start` will also re-start the process. Additional arguments can be specified in `msg.args`. +Sending `msg.stop` will stop the process and prevent automatic re-start until reset with `msg.start`. + **Note:** Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour. For example, if running a Python app, the `-u` parameter will stop the output being buffered. diff --git a/utility/daemon/daemon.js b/utility/daemon/daemon.js index d3ab3817..74b0fdb5 100644 --- a/utility/daemon/daemon.js +++ b/utility/daemon/daemon.js @@ -12,6 +12,7 @@ module.exports = function(RED) { this.op = n.op; this.redo = n.redo; this.running = false; + this.stopped = false; this.closer = n.closer || "SIGKILL"; this.autorun = true; if (n.autorun === false) { this.autorun = false; } @@ -32,16 +33,29 @@ module.exports = function(RED) { function inputlistener(msg) { if (msg != null) { - if (msg.hasOwnProperty("kill") && node.running) { + if (msg.hasOwnProperty("stop")) { + this.stopped = true; + if (node.running) { + node.child.kill("SIGINT"); + } + node.debug(node.cmd+" stopped by msg"); + node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.stopped")}); + } + else if (msg.hasOwnProperty("kill") && node.running) { if (typeof msg.kill !== "string" || msg.kill.length === 0 || !msg.kill.toUpperCase().startsWith("SIG") ) { msg.kill = "SIGINT"; } node.child.kill(msg.kill.toUpperCase()); } - else if (msg.hasOwnProperty("start") && !node.running) { - let args = ""; - if (msg.hasOwnProperty("args") && msg.args.length > 0) { - args = parseArgs(msg.args.trim()); + else if (msg.hasOwnProperty("start")) { + this.stopped = false; + if (!node.running) { + let args = ""; + if (msg.hasOwnProperty("args") && msg.args.length > 0) { + args = parseArgs(msg.args.trim()); + } + runit(args); + } else { + node.child.kill("SIGINT"); } - runit(args); } else { if (!Buffer.isBuffer(msg.payload)) { @@ -111,7 +125,8 @@ module.exports = function(RED) { var rc = code; if (code === null) { rc = signal; } node.send([null,null,{payload:rc}]); - node.status({fill:"red",shape:"ring",text:RED._("daemon.status.stopped")}); + const color = node.stopped ? "grey" : "red"; + node.status({fill:color,shape:"ring",text:RED._("daemon.status.stopped")}); }); node.child.on('error', function (err) { @@ -138,7 +153,7 @@ module.exports = function(RED) { if (node.redo === true) { var loop = setInterval( function() { - if (!node.running) { + if (!node.running && !node.stopped) { node.warn(RED._("daemon.errors.restarting") + " : " + node.cmd); runit(); }