node-red-daemon append arguments (#939)

* Allow msg.start to append arguments

* Update description

* Update README.md
This commit is contained in:
kaffetorsk 2022-10-26 16:22:48 +02:00 committed by GitHub
parent c15fa79e95
commit 4c9afda6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 13 deletions

View File

@ -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.

View File

@ -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;

View File

@ -5,7 +5,7 @@
<p>Parameters can be space separated, space separated with quotes, or a javascript array. For example `aa bb` or `"cc dd"` or `["aa","bb cc""]`.</p>
<p>If the called program stops (i.e. a return code is produced), this node can attempt to restart the command.</p>
<p>Setting <code>msg.kill</code> 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 <code>msg.start</code> will also re-start the process.</p>
restart flag is set it will then auto restart. Sending <code>msg.start</code> will also re-start the process. Additional arguments can be specified in <code>msg.args</code>.</p>
<p><b>Note:</b> Some applications will automatically buffer lines of output. It is advisable to turn off this behaviour.
For example, if running a Python app, the <code>-u</code> parameter will stop the output being buffered.</p>
</script>