2014-12-24 22:51:17 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
function DaemonNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.cmd = n.command;
|
2021-02-05 11:35:54 +01:00
|
|
|
//this.args = n.args.trim().split(" ") || [];
|
2021-03-11 20:09:13 +01:00
|
|
|
this.args = n.args.trim(); //.match(/("[^"]*")|[^ ]+/g);
|
2014-12-24 22:51:17 +01:00
|
|
|
this.cr = n.cr;
|
|
|
|
this.op = n.op;
|
|
|
|
this.redo = n.redo;
|
|
|
|
this.running = false;
|
2018-05-09 10:42:51 +02:00
|
|
|
this.closer = n.closer || "SIGKILL";
|
2018-08-23 09:57:15 +02:00
|
|
|
this.autorun = true;
|
|
|
|
if (n.autorun === false) { this.autorun = false; }
|
2021-03-11 20:09:13 +01:00
|
|
|
if (this.args.match(/^\[.*\]$/)) {
|
|
|
|
try { this.args = JSON.parse(this.args); }
|
|
|
|
catch(e) {
|
2021-10-18 22:12:00 +02:00
|
|
|
node.warn(RED._("daemon.errors.badparams"))
|
2021-03-11 20:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { this.args = this.args.match(/("[^"]*")|[^ ]+/g); }
|
2014-12-24 22:51:17 +01:00
|
|
|
var node = this;
|
2019-05-20 20:32:11 +02:00
|
|
|
var lastmsg = {};
|
2014-12-24 22:51:17 +01:00
|
|
|
|
|
|
|
function inputlistener(msg) {
|
|
|
|
if (msg != null) {
|
2017-05-03 18:46:10 +02:00
|
|
|
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) {
|
|
|
|
runit();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!Buffer.isBuffer(msg.payload)) {
|
|
|
|
if (typeof msg.payload === "object") { msg.payload = JSON.stringify(msg.payload); }
|
|
|
|
if (typeof msg.payload !== "string") { msg.payload = msg.payload.toString(); }
|
|
|
|
if (node.cr === true) { msg.payload += "\n"; }
|
|
|
|
}
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug("inp: "+msg.payload);
|
2017-05-03 18:46:10 +02:00
|
|
|
if (node.child !== null && node.running) { node.child.stdin.write(msg.payload); }
|
2021-10-18 22:12:00 +02:00
|
|
|
else { node.warn(RED._("daemon.errors.notrunning")); }
|
2019-05-20 20:32:11 +02:00
|
|
|
lastmsg = msg;
|
2014-12-24 22:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runit() {
|
2018-02-09 13:51:42 +01:00
|
|
|
var line = "";
|
2017-08-22 16:49:31 +02:00
|
|
|
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
|
2021-10-18 22:12:00 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")});
|
2017-08-22 16:49:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-02-09 13:51:42 +01:00
|
|
|
try {
|
|
|
|
node.child = spawn(node.cmd, node.args);
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug(node.cmd+" "+JSON.stringify(node.args));
|
2021-10-18 22:12:00 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")});
|
2018-02-09 13:51:42 +01:00
|
|
|
node.running = true;
|
2014-12-24 22:51:17 +01:00
|
|
|
|
2018-02-09 13:51:42 +01:00
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
if (node.op === "string") { data = data.toString(); }
|
|
|
|
if (node.op === "number") { data = Number(data); }
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug("out: "+data);
|
2018-02-09 13:51:42 +01:00
|
|
|
if (node.op === "lines") {
|
|
|
|
line += data.toString();
|
|
|
|
var bits = line.split("\n");
|
|
|
|
while (bits.length > 1) {
|
2020-03-31 18:07:22 +02:00
|
|
|
var m = RED.util.cloneMessage(lastmsg);
|
|
|
|
m.payload = bits.shift();
|
|
|
|
node.send([m,null,null]);
|
2018-02-09 13:51:42 +01:00
|
|
|
}
|
|
|
|
line = bits[0];
|
2016-04-18 22:38:44 +02:00
|
|
|
}
|
2018-02-09 13:51:42 +01:00
|
|
|
else {
|
|
|
|
if (data && (data.length !== 0)) {
|
2019-05-20 20:32:11 +02:00
|
|
|
lastmsg.payload = data;
|
|
|
|
node.send([lastmsg,null,null]);
|
2018-02-09 13:51:42 +01:00
|
|
|
}
|
2016-04-18 22:38:44 +02:00
|
|
|
}
|
2018-02-09 13:51:42 +01:00
|
|
|
});
|
2014-12-24 22:51:17 +01:00
|
|
|
|
2018-02-09 13:51:42 +01:00
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (node.op === "string") { data = data.toString(); }
|
|
|
|
if (node.op === "number") { data = Number(data); }
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug("err: "+data);
|
2019-05-20 20:32:11 +02:00
|
|
|
lastmsg.payload = data;
|
|
|
|
node.send([null,lastmsg,null]);
|
2018-02-09 13:51:42 +01:00
|
|
|
});
|
2014-12-24 22:51:17 +01:00
|
|
|
|
2018-02-09 13:51:42 +01:00
|
|
|
node.child.on('close', function (code,signal) {
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug("ret: "+code+":"+signal);
|
2018-02-09 13:51:42 +01:00
|
|
|
node.running = false;
|
|
|
|
node.child = null;
|
|
|
|
var rc = code;
|
|
|
|
if (code === null) { rc = signal; }
|
|
|
|
node.send([null,null,{payload:rc}]);
|
2021-10-18 22:12:00 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.stopped")});
|
2018-02-09 13:51:42 +01:00
|
|
|
});
|
2014-12-24 22:51:17 +01:00
|
|
|
|
2018-02-09 13:51:42 +01:00
|
|
|
node.child.on('error', function (err) {
|
2021-10-18 22:12:00 +02:00
|
|
|
if (err.errno === "ENOENT") { node.warn(RED._("daemon.errors.notfound")); }
|
|
|
|
else if (err.errno === "EACCES") { node.warn(RED._("daemon.errors.notexecutable")); }
|
2018-02-09 13:51:42 +01:00
|
|
|
else { node.log('error: ' + err); }
|
2021-10-18 22:12:00 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.error")});
|
2018-02-09 13:51:42 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
2021-10-18 22:12:00 +02:00
|
|
|
if (e.errno === "ENOENT") { node.warn(RED._("daemon.errors.notfound")); }
|
|
|
|
else if (e.errno === "EACCES") { node.warn(RED._("daemon.errors.notexecutable")); }
|
2018-02-09 13:51:42 +01:00
|
|
|
else { node.error(e); }
|
2021-10-18 22:12:00 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:RED._("daemon.status.error")});
|
2018-02-09 13:51:42 +01:00
|
|
|
node.running = false;
|
|
|
|
}
|
2014-12-24 22:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.redo === true) {
|
|
|
|
var loop = setInterval( function() {
|
|
|
|
if (!node.running) {
|
2021-10-18 22:12:00 +02:00
|
|
|
node.warn(RED._("daemon.errors.restarting") + " : " + node.cmd);
|
2014-12-24 22:51:17 +01:00
|
|
|
runit();
|
|
|
|
}
|
|
|
|
}, 10000); // Restart after 10 secs if required
|
|
|
|
}
|
|
|
|
|
2017-01-02 00:34:23 +01:00
|
|
|
node.on("close", function(done) {
|
|
|
|
clearInterval(loop);
|
2018-05-09 10:42:51 +02:00
|
|
|
if (node.child != null) {
|
|
|
|
var tout;
|
|
|
|
node.child.on('exit', function() {
|
|
|
|
if (tout) { clearTimeout(tout); }
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
tout = setTimeout(function() {
|
|
|
|
node.child.kill("SIGKILL"); // if it takes more than 3 secs kill it anyway.
|
|
|
|
done();
|
|
|
|
}, 3000);
|
|
|
|
node.child.kill(node.closer);
|
2021-12-23 11:58:33 +01:00
|
|
|
node.debug(node.cmd+" stopped");
|
2018-05-09 10:42:51 +02:00
|
|
|
}
|
|
|
|
else { setTimeout(function() { done(); }, 100); }
|
2016-04-18 22:38:44 +02:00
|
|
|
node.status({});
|
2014-12-24 22:51:17 +01:00
|
|
|
});
|
|
|
|
|
2018-10-24 22:54:13 +02:00
|
|
|
if (this.autorun) { runit(); }
|
2017-10-29 00:12:39 +02:00
|
|
|
|
|
|
|
node.on("input", inputlistener);
|
2014-12-24 22:51:17 +01:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("daemon",DaemonNode);
|
|
|
|
}
|