mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Kill processes run with exec node when flows redeployed
This commit is contained in:
parent
0839b6f58e
commit
306fb7a3d1
@ -27,6 +27,7 @@ module.exports = function(RED) {
|
|||||||
this.addpay = n.addpay;
|
this.addpay = n.addpay;
|
||||||
this.append = (n.append || "").trim();
|
this.append = (n.append || "").trim();
|
||||||
this.useSpawn = n.useSpawn;
|
this.useSpawn = n.useSpawn;
|
||||||
|
this.activeProcesses = {};
|
||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
@ -35,13 +36,18 @@ module.exports = function(RED) {
|
|||||||
// make the extra args into an array
|
// make the extra args into an array
|
||||||
// then prepend with the msg.payload
|
// then prepend with the msg.payload
|
||||||
|
|
||||||
var arg = node.cmd+" "+msg.payload+" "+node.append;
|
var arg = node.cmd;
|
||||||
|
if (node.addpay) {
|
||||||
|
arg += " "+msg.payload;
|
||||||
|
}
|
||||||
|
arg += " "+node.append;
|
||||||
// slice whole line by spaces (trying to honour quotes);
|
// slice whole line by spaces (trying to honour quotes);
|
||||||
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
|
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
|
||||||
var cmd = arg.shift();
|
var cmd = arg.shift();
|
||||||
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
|
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
|
||||||
if (cmd.indexOf(" ") == -1) {
|
if (cmd.indexOf(" ") == -1) {
|
||||||
var ex = spawn(cmd,arg);
|
var ex = spawn(cmd,arg);
|
||||||
|
node.activeProcesses[ex.pid] = ex;
|
||||||
ex.stdout.on('data', function (data) {
|
ex.stdout.on('data', function (data) {
|
||||||
//console.log('[exec] stdout: ' + data);
|
//console.log('[exec] stdout: ' + data);
|
||||||
if (isUtf8(data)) { msg.payload = data.toString(); }
|
if (isUtf8(data)) { msg.payload = data.toString(); }
|
||||||
@ -56,11 +62,13 @@ module.exports = function(RED) {
|
|||||||
});
|
});
|
||||||
ex.on('close', function (code) {
|
ex.on('close', function (code) {
|
||||||
//console.log('[exec] result: ' + code);
|
//console.log('[exec] result: ' + code);
|
||||||
|
delete node.activeProcesses[ex.pid];
|
||||||
msg.payload = code;
|
msg.payload = code;
|
||||||
node.status({});
|
node.status({});
|
||||||
node.send([null,null,msg]);
|
node.send([null,null,msg]);
|
||||||
});
|
});
|
||||||
ex.on('error', function (code) {
|
ex.on('error', function (code) {
|
||||||
|
delete node.activeProcesses[ex.pid];
|
||||||
node.error(code,msg);
|
node.error(code,msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -88,9 +96,20 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
node.status({});
|
node.status({});
|
||||||
node.send([msg,msg2,msg3]);
|
node.send([msg,msg2,msg3]);
|
||||||
|
delete node.activeProcesses[child.pid];
|
||||||
});
|
});
|
||||||
|
child.on('error',function(){})
|
||||||
|
node.activeProcesses[child.pid] = child;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.on('close',function() {
|
||||||
|
for (var pid in node.activeProcesses) {
|
||||||
|
if (node.activeProcesses.hasOwnProperty(pid)) {
|
||||||
|
node.activeProcesses[pid].kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node.activeProcesses = {};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("exec",ExecNode);
|
RED.nodes.registerType("exec",ExecNode);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user