From 4c9afda6dd67854e0da975f46e56588b9f1e1bd7 Mon Sep 17 00:00:00 2001 From: kaffetorsk <93540369+kaffetorsk@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:22:48 +0200 Subject: [PATCH] node-red-daemon append arguments (#939) * Allow msg.start to append arguments * Update description * Update README.md --- utility/daemon/README.md | 2 +- utility/daemon/daemon.js | 36 ++++++++++++++++-------- utility/daemon/locales/en-US/daemon.html | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/utility/daemon/README.md b/utility/daemon/README.md index 57577643..95f41c16 100644 --- a/utility/daemon/README.md +++ b/utility/daemon/README.md @@ -34,7 +34,7 @@ to restart the command automatically. Setting `msg.kill` to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the restart flag is set it will then auto restart. -Sending `msg.start` will also re-start the process. +Sending `msg.start` will also re-start the process. Additional arguments can be specified in `msg.args`. **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 bbee3332..928f8143 100644 --- a/utility/daemon/daemon.js +++ b/utility/daemon/daemon.js @@ -15,16 +15,21 @@ module.exports = function(RED) { this.closer = n.closer || "SIGKILL"; this.autorun = true; if (n.autorun === false) { this.autorun = false; } - if (this.args.match(/^\[.*\]$/)) { - try { this.args = JSON.parse(this.args); } - catch(e) { - node.warn(RED._("daemon.errors.badparams")) - } - } - else { this.args = this.args.match(/("[^"]*")|[^ ]+/g); } + this.args = parseArgs(this.args); var node = this; var lastmsg = {}; + function parseArgs(args) { + if (args.match(/^\[.*\]$/)) { + try { args = JSON.parse(args); } + catch(e) { + node.warn(RED._("daemon.errors.badparams")) + } + } + else { args = args.match(/("[^"]*")|[^ ]+/g); } + return args; + } + function inputlistener(msg) { if (msg != null) { if (msg.hasOwnProperty("kill") && node.running) { @@ -32,7 +37,11 @@ module.exports = function(RED) { node.child.kill(msg.kill.toUpperCase()); } else if (msg.hasOwnProperty("start") && !node.running) { - runit(); + let args = ""; + if (msg.hasOwnProperty("args") && msg.args.length > 0) { + args = parseArgs(msg.args.trim()); + } + runit(args); } else { if (!Buffer.isBuffer(msg.payload)) { @@ -48,15 +57,20 @@ module.exports = function(RED) { } } - function runit() { + function runit(appendArgs) { var line = ""; if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) { node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")}); return; } + let args = node.args; + if (appendArgs !== undefined && appendArgs.length > 0) { + args = args.concat(appendArgs); + } + try { - node.child = spawn(node.cmd, node.args); - node.debug(node.cmd+" "+JSON.stringify(node.args)); + node.child = spawn(node.cmd, args); + node.debug(node.cmd+" "+JSON.stringify(args)); node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")}); node.running = true; diff --git a/utility/daemon/locales/en-US/daemon.html b/utility/daemon/locales/en-US/daemon.html index fd1ec70b..06d750d7 100644 --- a/utility/daemon/locales/en-US/daemon.html +++ b/utility/daemon/locales/en-US/daemon.html @@ -5,7 +5,7 @@
Parameters can be space separated, space separated with quotes, or a javascript array. For example `aa bb` or `"cc dd"` or `["aa","bb cc""]`.
If the called program stops (i.e. a return code is produced), this node can attempt to restart the command.
Setting msg.kill
to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the
- restart flag is set it will then auto restart. Sending msg.start
will also re-start the process.
msg.start
will also re-start the process. Additional arguments can be specified in msg.args
.
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.