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:
parent
61681bb1d6
commit
a345089c8b
@ -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);
|
||||||
|
@ -46,14 +46,14 @@ describe('file Nodes', function() {
|
|||||||
it('should be loaded', function(done) {
|
it('should be loaded', function(done) {
|
||||||
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":true}];
|
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest, "appendNewline":true, "overwriteFile":true}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
try {
|
try {
|
||||||
var fileNode1 = helper.getNode("fileNode1");
|
var fileNode1 = helper.getNode("fileNode1");
|
||||||
fileNode1.should.have.property('name', 'fileNode');
|
fileNode1.should.have.property('name', 'fileNode');
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -64,16 +64,16 @@ describe('file Nodes', function() {
|
|||||||
var n1 = helper.getNode("fileNode1");
|
var n1 = helper.getNode("fileNode1");
|
||||||
var n2 = helper.getNode("helperNode1");
|
var n2 = helper.getNode("helperNode1");
|
||||||
n2.on("input", function(msg) {
|
n2.on("input", function(msg) {
|
||||||
try {
|
try {
|
||||||
var f = fs.readFileSync(fileToTest);
|
var f = fs.readFileSync(fileToTest);
|
||||||
f.should.have.length(4);
|
f.should.have.length(4);
|
||||||
fs.unlinkSync(fileToTest);
|
fs.unlinkSync(fileToTest);
|
||||||
msg.should.have.property("payload", "test");
|
msg.should.have.property("payload", "test");
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
n1.receive({payload:"test"});
|
n1.receive({payload:"test"});
|
||||||
});
|
});
|
||||||
@ -93,26 +93,26 @@ describe('file Nodes', function() {
|
|||||||
var data = ["test2", true, 999, [2]];
|
var data = ["test2", true, 999, [2]];
|
||||||
|
|
||||||
n2.on("input", function (msg) {
|
n2.on("input", function (msg) {
|
||||||
try {
|
try {
|
||||||
msg.should.have.property("payload");
|
msg.should.have.property("payload");
|
||||||
data.should.containDeep([msg.payload]);
|
data.should.containDeep([msg.payload]);
|
||||||
if (count === 3) {
|
if (count === 3) {
|
||||||
var f = fs.readFileSync(fileToTest).toString();
|
var f = fs.readFileSync(fileToTest).toString();
|
||||||
if (os.type() !== "Windows_NT") {
|
if (os.type() !== "Windows_NT") {
|
||||||
f.should.have.length(19);
|
f.should.have.length(19);
|
||||||
f.should.equal("test2\ntrue\n999\n[2]\n");
|
f.should.equal("test2\ntrue\n999\n[2]\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
f.should.have.length(23);
|
f.should.have.length(23);
|
||||||
f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n");
|
f.should.equal("test2\r\ntrue\r\n999\r\n[2]\r\n");
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
n1.receive({payload:"test2"}); // string
|
n1.receive({payload:"test2"}); // string
|
||||||
@ -142,37 +142,37 @@ describe('file Nodes', function() {
|
|||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
n2.on("input", function (msg) {
|
n2.on("input", function (msg) {
|
||||||
try {
|
try {
|
||||||
msg.should.have.property("payload");
|
msg.should.have.property("payload");
|
||||||
data.should.containDeep([msg.payload]);
|
data.should.containDeep([msg.payload]);
|
||||||
try {
|
try {
|
||||||
if (count === 1) {
|
if (count === 1) {
|
||||||
// Check they got appended as expected
|
// Check they got appended as expected
|
||||||
var f = fs.readFileSync(fileToTest).toString();
|
var f = fs.readFileSync(fileToTest).toString();
|
||||||
f.should.equal("onetwo");
|
f.should.equal("onetwo");
|
||||||
|
|
||||||
// Delete the file
|
// Delete the file
|
||||||
fs.unlinkSync(fileToTest);
|
fs.unlinkSync(fileToTest);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
// Send two more messages to the file
|
// Send two more messages to the file
|
||||||
n1.receive({payload:"three"});
|
n1.receive({payload:"three"});
|
||||||
n1.receive({payload:"four"});
|
n1.receive({payload:"four"});
|
||||||
}, wait);
|
}, wait);
|
||||||
}
|
}
|
||||||
if (count === 3) {
|
if (count === 3) {
|
||||||
var f = fs.readFileSync(fileToTest).toString();
|
var f = fs.readFileSync(fileToTest).toString();
|
||||||
f.should.equal("threefour");
|
f.should.equal("threefour");
|
||||||
fs.unlinkSync(fileToTest);
|
fs.unlinkSync(fileToTest);
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
done(err);
|
done(err);
|
||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send two messages to the file
|
// Send two messages to the file
|
||||||
@ -197,7 +197,7 @@ describe('file Nodes', function() {
|
|||||||
n2.on("input", function (msg) {
|
n2.on("input", function (msg) {
|
||||||
try {
|
try {
|
||||||
msg.should.have.property("payload");
|
msg.should.have.property("payload");
|
||||||
data.should.containDeep([msg.payload]);
|
data.should.containDeep([msg.payload]);
|
||||||
if (count == 1) {
|
if (count == 1) {
|
||||||
// Check they got appended as expected
|
// Check they got appended as expected
|
||||||
var f = fs.readFileSync(fileToTest).toString();
|
var f = fs.readFileSync(fileToTest).toString();
|
||||||
@ -256,25 +256,25 @@ describe('file Nodes', function() {
|
|||||||
var n2 = helper.getNode("helperNode1");
|
var n2 = helper.getNode("helperNode1");
|
||||||
|
|
||||||
n2.on("input", function (msg) {
|
n2.on("input", function (msg) {
|
||||||
try {
|
try {
|
||||||
msg.should.have.property("payload", "fine");
|
msg.should.have.property("payload", "fine");
|
||||||
msg.should.have.property("filename", fileToTest);
|
msg.should.have.property("filename", fileToTest);
|
||||||
|
|
||||||
var f = fs.readFileSync(fileToTest).toString();
|
var f = fs.readFileSync(fileToTest).toString();
|
||||||
if (os.type() !== "Windows_NT") {
|
if (os.type() !== "Windows_NT") {
|
||||||
f.should.have.length(5);
|
f.should.have.length(5);
|
||||||
f.should.equal("fine\n");
|
f.should.equal("fine\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
f.should.have.length(6);
|
f.should.have.length(6);
|
||||||
f.should.equal("fine\r\n");
|
f.should.equal("fine\r\n");
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
n1.receive({payload:"fine", filename:fileToTest});
|
n1.receive({payload:"fine", filename:fileToTest});
|
||||||
});
|
});
|
||||||
@ -551,27 +551,27 @@ describe('file Nodes', function() {
|
|||||||
var n2 = helper.getNode("helperNode1");
|
var n2 = helper.getNode("helperNode1");
|
||||||
var count = 0;
|
var count = 0;
|
||||||
n2.on("input", function(msg) {
|
n2.on("input", function(msg) {
|
||||||
try {
|
try {
|
||||||
count++;
|
count++;
|
||||||
if (count == file_count) {
|
if (count == file_count) {
|
||||||
for(var i = 0; i < file_count; i++) {
|
for(var i = 0; i < file_count; i++) {
|
||||||
var name = path.join(tmp_path, String(i));
|
var name = path.join(tmp_path, String(i));
|
||||||
var f = fs.readFileSync(name);
|
var f = fs.readFileSync(name);
|
||||||
f.should.have.length(len);
|
f.should.have.length(len);
|
||||||
f[0].should.have.equal(i);
|
f[0].should.have.equal(i);
|
||||||
}
|
}
|
||||||
fs.removeSync(tmp_path);
|
fs.removeSync(tmp_path);
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
try {
|
try {
|
||||||
fs.removeSync(tmp_path);
|
fs.removeSync(tmp_path);
|
||||||
}
|
}
|
||||||
catch (e1) {
|
catch (e1) {
|
||||||
}
|
}
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for(var i = 0; i < file_count; i++) {
|
for(var i = 0; i < file_count; i++) {
|
||||||
var data = new Buffer(len);
|
var data = new Buffer(len);
|
||||||
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -615,7 +659,7 @@ describe('file Nodes', function() {
|
|||||||
|
|
||||||
it('should read in a file and output a buffer', function(done) {
|
it('should read in a file and output a buffer', function(done) {
|
||||||
var flow = [{id:"fileInNode1", type:"file in", name:"fileInNode", "filename":fileToTest, "format":"", wires:[["n2"]]},
|
var flow = [{id:"fileInNode1", type:"file in", name:"fileInNode", "filename":fileToTest, "format":"", wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
var n1 = helper.getNode("fileInNode1");
|
var n1 = helper.getNode("fileInNode1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
@ -632,7 +676,7 @@ describe('file Nodes', function() {
|
|||||||
|
|
||||||
it('should read in a file and output a utf8 string', function(done) {
|
it('should read in a file and output a utf8 string', function(done) {
|
||||||
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "filename":fileToTest, "format":"utf8", wires:[["n2"]]},
|
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "filename":fileToTest, "format":"utf8", wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
var n1 = helper.getNode("fileInNode1");
|
var n1 = helper.getNode("fileInNode1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
@ -653,7 +697,7 @@ describe('file Nodes', function() {
|
|||||||
|
|
||||||
it('should read in a file and output split lines with parts', function(done) {
|
it('should read in a file and output split lines with parts', function(done) {
|
||||||
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"lines", wires:[["n2"]]},
|
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"lines", wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
var n1 = helper.getNode("fileInNode1");
|
var n1 = helper.getNode("fileInNode1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
@ -691,7 +735,7 @@ describe('file Nodes', function() {
|
|||||||
var line = data.join("\n");
|
var line = data.join("\n");
|
||||||
fs.writeFileSync(fileToTest, line);
|
fs.writeFileSync(fileToTest, line);
|
||||||
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"lines", wires:[["n2"]]},
|
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"lines", wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
var n1 = helper.getNode("fileInNode1");
|
var n1 = helper.getNode("fileInNode1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
@ -724,7 +768,7 @@ describe('file Nodes', function() {
|
|||||||
|
|
||||||
it('should read in a file and output a buffer with parts', function(done) {
|
it('should read in a file and output a buffer with parts', function(done) {
|
||||||
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"stream", wires:[["n2"]]},
|
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", filename:fileToTest, format:"stream", wires:[["n2"]]},
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
helper.load(fileNode, flow, function() {
|
helper.load(fileNode, flow, function() {
|
||||||
var n1 = helper.getNode("fileInNode1");
|
var n1 = helper.getNode("fileInNode1");
|
||||||
var n2 = helper.getNode("n2");
|
var n2 = helper.getNode("n2");
|
||||||
|
Loading…
Reference in New Issue
Block a user