Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support stop msg for daemon node #1018

Merged
merged 4 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions utility/daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
32 changes: 24 additions & 8 deletions utility/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -32,16 +33,30 @@ module.exports = function(RED) {

function inputlistener(msg) {
if (msg != null) {
if (msg.hasOwnProperty("kill") && node.running) {
if (msg.hasOwnProperty("stop")) {
node.stopped = true;
if (node.running) {
node.child.kill(node.closer);
}
node.debug(node.cmd+" stopped by msg");
gmarzot marked this conversation as resolved.
Show resolved Hide resolved
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")) {
node.stopped = false;
if (!node.running) {
let args = "";
if (msg.hasOwnProperty("args") && msg.args.length > 0) {
args = parseArgs(msg.args.trim());
}
runit(args);
} else {
gmarzot marked this conversation as resolved.
Show resolved Hide resolved
node.child.kill(node.closer);
// should this also re-start the process incase redo is not set XXX
}
runit(args);
}
else {
if (!Buffer.isBuffer(msg.payload)) {
Expand Down Expand Up @@ -111,7 +126,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) {
Expand All @@ -138,7 +154,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();
}
Expand Down