Skip to content

Commit 306fb7a

Browse files
committed
Kill processes run with exec node when flows redeployed
1 parent 0839b6f commit 306fb7a

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

nodes/core/core/75-exec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function(RED) {
2727
this.addpay = n.addpay;
2828
this.append = (n.append || "").trim();
2929
this.useSpawn = n.useSpawn;
30+
this.activeProcesses = {};
3031

3132
var node = this;
3233
this.on("input", function(msg) {
@@ -35,13 +36,18 @@ module.exports = function(RED) {
3536
// make the extra args into an array
3637
// then prepend with the msg.payload
3738

38-
var arg = node.cmd+" "+msg.payload+" "+node.append;
39+
var arg = node.cmd;
40+
if (node.addpay) {
41+
arg += " "+msg.payload;
42+
}
43+
arg += " "+node.append;
3944
// slice whole line by spaces (trying to honour quotes);
4045
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
4146
var cmd = arg.shift();
4247
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
4348
if (cmd.indexOf(" ") == -1) {
4449
var ex = spawn(cmd,arg);
50+
node.activeProcesses[ex.pid] = ex;
4551
ex.stdout.on('data', function (data) {
4652
//console.log('[exec] stdout: ' + data);
4753
if (isUtf8(data)) { msg.payload = data.toString(); }
@@ -56,11 +62,13 @@ module.exports = function(RED) {
5662
});
5763
ex.on('close', function (code) {
5864
//console.log('[exec] result: ' + code);
65+
delete node.activeProcesses[ex.pid];
5966
msg.payload = code;
6067
node.status({});
6168
node.send([null,null,msg]);
6269
});
6370
ex.on('error', function (code) {
71+
delete node.activeProcesses[ex.pid];
6472
node.error(code,msg);
6573
});
6674
}
@@ -88,8 +96,19 @@ module.exports = function(RED) {
8896
}
8997
node.status({});
9098
node.send([msg,msg2,msg3]);
99+
delete node.activeProcesses[child.pid];
91100
});
101+
child.on('error',function(){})
102+
node.activeProcesses[child.pid] = child;
103+
}
104+
});
105+
this.on('close',function() {
106+
for (var pid in node.activeProcesses) {
107+
if (node.activeProcesses.hasOwnProperty(pid)) {
108+
node.activeProcesses[pid].kill();
109+
}
92110
}
111+
node.activeProcesses = {};
93112
});
94113
}
95114
RED.nodes.registerType("exec",ExecNode);

0 commit comments

Comments
 (0)