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

Modify Tail node to only send new messages

Now with test that works with it.
Part of Issue #326
Now need to redo with libs that support windows.
This commit is contained in:
Dave C-J 2014-08-22 12:44:40 +01:00
parent a8ade083d4
commit ecbf4add6b
4 changed files with 58 additions and 19 deletions

View File

@ -16,7 +16,7 @@
<script type="text/x-red" data-template-name="tail"> <script type="text/x-red" data-template-name="tail">
<div class="form-row node-input-filename"> <div class="form-row node-input-filename">
<label for="node-input-filename"><i class="icon-file"></i> Filename</label> <label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
<input type="text" id="node-input-filename" placeholder="Filename"> <input type="text" id="node-input-filename" placeholder="Filename">
</div> </div>
<div class="form-row"> <div class="form-row">
@ -25,15 +25,15 @@
<label for="node-input-split" style="width: 70%;">Split lines if we see \n ?</label> <label for="node-input-split" style="width: 70%;">Split lines if we see \n ?</label>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">WON'T work on Windows.</div> <!-- <div class="form-tips">WON'T work on Windows.</div> -->
</script> </script>
<script type="text/x-red" data-help-name="tail"> <script type="text/x-red" data-help-name="tail">
<p>Tails (watches for things to be added) to the configured file. (Linux/Mac ONLY)</p> <p>Tails (watches for things to be added) to the configured file. (Linux/Mac ONLY)</p>
<p>This won't work on Windows filesystems (as it relies on the tail -F command) so we will probably have to hide it in future.</p> <p>This won't work on Windows filesystems, as it relies on the tail -F command.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013, 2014 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,11 @@ module.exports = function(RED) {
"use strict"; "use strict";
var fs = require("fs"); var fs = require("fs");
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var plat = require('os').platform();
if (plat.match(/^win/)) {
throw "Info : Currently not supported on Windows.";
}
function TailNode(n) { function TailNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -28,7 +33,7 @@ module.exports = function(RED) {
var err = ""; var err = "";
// TODO: rewrite to use node-tail // TODO: rewrite to use node-tail
var tail = spawn("tail", ["-F", this.filename]); var tail = spawn("tail", ["-F", "-n", "0", this.filename]);
tail.stdout.on("data", function (data) { tail.stdout.on("data", function (data) {
if (node.split) { if (node.split) {
// TODO: allow customisation of the line break - as we do elsewhere // TODO: allow customisation of the line break - as we do elsewhere

View File

@ -23,14 +23,17 @@ var tailNode = require("../../../../nodes/core/storage/28-tail.js");
var helper = require("../../helper.js"); var helper = require("../../helper.js");
describe('TailNode', function() { describe('TailNode', function() {
var resourcesDir = path.join(__dirname,"..","..","..","resources"); var resourcesDir = path.join(__dirname,"..","..","..","resources");
var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt"); var fileToTail = path.join(resourcesDir,"28-tail-test-file.txt");
fs.writeFile(fileToTail, "Tail message line 1\nTail message line 2\n", function (err) {
if (err) { console.log('cannot write to '+fileToTail); }
});
beforeEach(function(done) { beforeEach(function(done) {
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function(done) { afterEach(function(done) {
helper.unload(); helper.unload();
helper.stopServer(done); helper.stopServer(done);
@ -44,8 +47,8 @@ describe('TailNode', function() {
done(); done();
}); });
}); });
it('should tail a file', function(done) { it('tail should tail a file', function(done) {
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(tailNode, flow, function() { helper.load(tailNode, flow, function() {
@ -53,27 +56,57 @@ describe('TailNode', function() {
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
var inputCounter = 0; var inputCounter = 0;
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
console.log(msg); //console.log(msg);
msg.should.have.property('topic', fileToTail); msg.should.have.property('topic', fileToTail);
msg.payload.should.equal("Tail message line" + (++inputCounter)); msg.payload.should.equal("Tail message line " + (++inputCounter + 2));
if(inputCounter === 2) { if (inputCounter === 2) {
done(); done();
} }
}); });
fs.appendFileSync(fileToTail, "Tail message line 3\nTail message line 4\n");
}); });
}); });
it('work in non-split mode', function(done) { it('tail should work in non-split mode', function(done) {
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]}, var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":false, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(tailNode, flow, function() { helper.load(tailNode, flow, function() {
var tailNode1 = helper.getNode("tailNode1"); var tailNode1 = helper.getNode("tailNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
//console.log(msg);
msg.should.have.property('topic', fileToTail); msg.should.have.property('topic', fileToTail);
msg.payload.should.equal("Tail message line1\nTail message line2\n"); msg.payload.should.equal("Tail message line 5\nTail message line 6\n");
done(); done();
}); });
fs.appendFile(fileToTail, "Tail message line 5\nTail message line 6\n");
}); });
}); });
it('tail should handle file going away and coming back', function(done) {
var flow = [{id:"tailNode1", type:"tail", name: "tailNode", "split":true, "filename":fileToTail, "wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(tailNode, flow, function() {
var tailNode1 = helper.getNode("tailNode1");
var helperNode1 = helper.getNode("helperNode1");
var inputCounter = 0;
var warned = false;
tailNode1.on("log", function(msg) {
if (msg.level == "warn") { warned = true; }
if ((inputCounter === 3)&&(warned === true)) { done(); }
});
helperNode1.on("input", function(msg) {
msg.should.have.property('topic', fileToTail);
msg.payload.should.equal("Tail message line A");
inputCounter += 1;
if ((inputCounter === 3)&&(warned === true)) { done(); }
});
fs.writeFileSync(fileToTail, "Tail message line A\n");
setTimeout( function() {
fs.appendFileSync(fileToTail, "Tail message line A\n");
fs.appendFileSync(fileToTail, "Tail message line A\n");
},250);
});
});
}); });

View File

@ -1,2 +1,3 @@
Tail message line1 Tail message line A
Tail message line2 Tail message line A
Tail message line A