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

Merge pull request #1894 from node-red-hitachi/fix-overlapping-file-node-execution

fix multiple input message processing of file node
This commit is contained in:
Nick O'Leary 2018-10-15 03:15:05 -07:00 committed by GitHub
commit 6a9575e9f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 286 additions and 137 deletions

View File

@ -28,9 +28,11 @@ module.exports = function(RED) {
this.createDir = n.createDir || false; this.createDir = n.createDir || false;
var node = this; var node = this;
node.wstream = null; node.wstream = null;
node.data = []; node.msgQueue = [];
node.closing = false;
node.closeCallback = null;
this.on("input",function(msg) { function processMsg(msg, done) {
var filename = node.filename || msg.filename || ""; var filename = node.filename || msg.filename || "";
if ((!node.filename) && (!node.tout)) { if ((!node.filename) && (!node.tout)) {
node.tout = setTimeout(function() { node.tout = setTimeout(function() {
@ -41,6 +43,7 @@ module.exports = function(RED) {
} }
if (filename === "") { if (filename === "") {
node.warn(RED._("file.errors.nofilename")); node.warn(RED._("file.errors.nofilename"));
done();
} else if (node.overwriteFile === "delete") { } else if (node.overwriteFile === "delete") {
fs.unlink(filename, function (err) { fs.unlink(filename, function (err) {
if (err) { if (err) {
@ -51,6 +54,7 @@ module.exports = function(RED) {
} }
node.send(msg); node.send(msg);
} }
done();
}); });
} else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) { } else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) {
var dir = path.dirname(filename); var dir = path.dirname(filename);
@ -59,6 +63,7 @@ module.exports = function(RED) {
fs.ensureDirSync(dir); fs.ensureDirSync(dir);
} catch(err) { } catch(err) {
node.error(RED._("file.errors.createfail",{error:err.toString()}),msg); node.error(RED._("file.errors.createfail",{error:err.toString()}),msg);
done();
return; return;
} }
} }
@ -70,21 +75,20 @@ module.exports = function(RED) {
if (typeof data === "boolean") { data = data.toString(); } if (typeof data === "boolean") { data = data.toString(); }
if (typeof data === "number") { data = data.toString(); } if (typeof data === "number") { data = data.toString(); }
if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; } if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; }
node.data.push({msg:msg,data:Buffer.from(data)});
while (node.data.length > 0) {
if (node.overwriteFile === "true") { if (node.overwriteFile === "true") {
(function(packet) { var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); node.wstream = wstream;
node.wstream.on("error", function(err) { wstream.on("error", function(err) {
node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); node.error(RED._("file.errors.writefail",{error:err.toString()}),msg);
done();
}); });
node.wstream.on("open", function() { wstream.on("open", function() {
node.wstream.end(packet.data, function() { wstream.end(data, function() {
node.send(packet.msg); node.send(msg);
done();
}); });
}) })
})(node.data.shift()); return;
} }
else { else {
// Append mode // Append mode
@ -123,32 +127,90 @@ module.exports = function(RED) {
}); });
node.wstream.on("error", function(err) { node.wstream.on("error", function(err) {
node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
done();
}); });
} }
if (node.filename) { if (node.filename) {
// Static filename - write and reuse the stream next time // Static filename - write and reuse the stream next time
var packet = node.data.shift() node.wstream.write(data, function() {
node.wstream.write(packet.data, function() { node.send(msg);
node.send(packet.msg); done();
}); });
} else { } else {
// Dynamic filename - write and close the stream // Dynamic filename - write and close the stream
var packet = node.data.shift() node.wstream.end(data, function() {
node.wstream.end(packet.data, function() { node.send(msg);
node.send(packet.msg);
});
delete node.wstream; delete node.wstream;
delete node.wstreamIno; delete node.wstreamIno;
done();
});
} }
} }
} }
else {
done();
}
}
function processQ(queue) {
var msg = queue[0];
processMsg(msg, function() {
queue.shift();
if (queue.length > 0) {
processQ(queue);
}
else if (node.closing) {
closeNode();
} }
}); });
this.on('close', function() { }
this.on("input", function(msg) {
var msgQueue = node.msgQueue;
if (msgQueue.push(msg) > 1) {
// pending write exists
return;
}
try {
processQ(msgQueue);
}
catch (e) {
node.msgQueue = [];
if (node.closing) {
closeNode();
}
throw e;
}
});
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({});
var cb = node.closeCallback;
node.closeCallback = null;
node.closing = false;
if (cb) {
cb();
}
}
this.on('close', function(done) {
if (node.closing) {
// already closing
return;
}
node.closing = true;
if (done) {
node.closeCallback = done;
}
if (node.msgQueue.length > 0) {
// close after queue processed
return;
}
else {
closeNode();
}
}); });
} }
RED.nodes.registerType("file",FileNode); RED.nodes.registerType("file",FileNode);

View File

@ -540,6 +540,93 @@ describe('file Nodes', function() {
}); });
}); });
it('should write to multiple files', 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);
}
});
});
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();
});
});
}); });