Skip to content

Commit

Permalink
added tailhead age check for tailreadstream
Browse files Browse the repository at this point in the history
removed getfilesize for tailreadstream
  • Loading branch information
leeliu committed Mar 31, 2017
1 parent 2bd7d33 commit 61c176f
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions lib/tailreadstream/tailreadstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const DEFAULT_WATCH_INTERVAL = 1000;
const DEFAULT_READ_INTERVAL = 250;
const DEFAULT_READ_TIMEOUT = 3600000; // 1hr timeout
const DEFAULT_TAILHEAD_SIZE = 32768; // 32kb
const DEFAULT_TAILHEAD_AGE = 60000; // 1min


class TailReadStream extends Readable {
Expand All @@ -30,6 +31,7 @@ class TailReadStream extends Readable {
this._timeout = options.timeout || DEFAULT_READ_TIMEOUT;
this._watchinterval = options.watchinterval || DEFAULT_WATCH_INTERVAL;
this._tailheadsize = options.tailheadsize || DEFAULT_TAILHEAD_SIZE;
this._tailheadage = options.tailheadage || DEFAULT_TAILHEAD_AGE;
this._idle = 0;

this._reading = false;
Expand Down Expand Up @@ -58,7 +60,6 @@ class TailReadStream extends Readable {
}
} else {
// tail from end
debug(filepath + ': tailing from end');
file._getFileSizeAndReadUntilEof();
}
return file;
Expand Down Expand Up @@ -105,19 +106,10 @@ class TailReadStream extends Readable {
this._stream.on('end', this._handleEnd.bind(this));
}

_getFileSize(callback) {
fs.stat(this._filepath, (err, stats) => {
if (err) {
return callback(err);
}
callback(null, stats.size);
});
}

_getFileSizeAndReadUntilEof() {
var that = this;

this._getFileSize((err, size) => {
fs.stat(this._filepath, (err, stats) => {
if (err) {
that.readable = false;

Expand All @@ -138,12 +130,12 @@ class TailReadStream extends Readable {
return;
}

if (size < that._tailheadsize) {
debug(that._filepath + ': file is smaller than ' + that._tailheadsize + ' bytes, reading from beginning');
size = 0; // tail from beginning of file if small enough (e.g. newly created files)
if (stats.size < that._tailheadsize && Date.now() - stats.birthtime.getTime() < that._tailheadage) {
debug(that._filepath + ': file is smaller than ' + that._tailheadsize + ' bytes and is ' + (Date.now() - stats.birthtime.getTime()) + 'ms old, reading from beginning');
that._readFromOffsetUntilEof(0); // tail from beginning of file if small enough (e.g. newly created files)
} else {
that._readFromOffsetUntilEof(stats.size);
}

that._readFromOffsetUntilEof(size);
});
}

Expand Down Expand Up @@ -216,19 +208,19 @@ class TailReadStream extends Readable {
return;
}

this._getFileSize((err, size) => {
fs.stat(this._filepath, (err, stats) => {
if (err) {
return setTimeout(that._watchFile.bind(that), that._watchinterval);
}

if (size < that._offset) {
that.emit('truncate', size);
if (size < that._tailheadsize) {
if (stats.size < that._offset) {
that.emit('truncate', stats.size);
if (stats.size < that._tailheadsize) {
debug(that._filepath + ': file truncated but smaller than ' + that._tailheadsize + ' bytes, reading from beginning');
that._offset = 0;
} else {
debug(that._filepath + ': file truncated but larger than ' + that._tailheadsize + ' bytes, reading from ' + size);
that._offset = size;
debug(that._filepath + ': file truncated but larger than ' + that._tailheadsize + ' bytes, reading from ' + stats.size);
that._offset = stats.size;
}
}

Expand Down

0 comments on commit 61c176f

Please sign in to comment.