1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

wait closing while penging messages exist

This commit is contained in:
Hiroyasu Nishiyama 2018-09-26 12:39:12 +09:00
parent 61681bb1d6
commit a345089c8b
2 changed files with 144 additions and 80 deletions

View File

@ -30,6 +30,7 @@ module.exports = function(RED) {
node.wstream = null; node.wstream = null;
node.data = []; node.data = [];
node.msgQueue = []; node.msgQueue = [];
node.closing = false;
function processMsg(msg, done) { function processMsg(msg, done) {
var filename = node.filename || msg.filename || ""; var filename = node.filename || msg.filename || "";
@ -170,12 +171,31 @@ module.exports = function(RED) {
return; return;
} }
processQ(msgQueue); processQ(msgQueue);
if (node.closing) {
closeNode();
}
}); });
this.on('close', function() { function closeNode() {
if (node.wstream) { node.wstream.end(); } if (node.wstream) { node.wstream.end(); }
if (node.tout) { clearTimeout(node.tout); } if (node.tout) { clearTimeout(node.tout); }
node.status({}); node.status({});
node.closing = false;
}
this.on('close', function() {
if (node.closing) {
// already closing
return;
}
node.closing = true;
if (node.msgQueue.length > 0) {
// close after queue processed
return;
}
else {
closeNode();
}
}); });
} }
RED.nodes.registerType("file",FileNode); RED.nodes.registerType("file",FileNode);

View File

@ -583,6 +583,50 @@ describe('file Nodes', function() {
}); });
}); });
it('should write to multiple files if node is closed', function(done) {
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "appendNewline":true, "overwriteFile":true, "createDir":true, wires: [["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
var tmp_path = path.join(resourcesDir, "tmp");
var len = 1024*1024*10;
var file_count = 5;
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
var n2 = helper.getNode("helperNode1");
var count = 0;
n2.on("input", function(msg) {
try {
count++;
if (count == file_count) {
for(var i = 0; i < file_count; i++) {
var name = path.join(tmp_path, String(i));
var f = fs.readFileSync(name);
f.should.have.length(len);
f[0].should.have.equal(i);
}
fs.removeSync(tmp_path);
done();
}
}
catch (e) {
try {
fs.removeSync(tmp_path);
}
catch (e1) {
}
done(e);
}
});
for(var i = 0; i < file_count; i++) {
var data = new Buffer(len);
data.fill(i);
var name = path.join(tmp_path, String(i));
var msg = {payload:data, filename:name};
n1.receive(msg);
}
n1.close();
});
});
}); });