Commit 2d396bac authored by Alan Szlosek's avatar Alan Szlosek

prelim log streamer

parent d0bd0042
Pipeline #17262569 passed with stage
in 1 minute and 16 seconds
......@@ -7,6 +7,7 @@ module.exports = {
Input: {
StatsD: require('./lib/input/statsd'),
LogStreamer: require('./lib/input/log-streamer'),
},
// DEPRECATED: use split module
......
/*
THOUGHTS
- do we really need to open a Read Stream?
- why not just open the file, seek to end?
- then when _read is called, read all we've got from the file to the end?
- DO use size
- watching will tell us when the file has changed
- but i don't see why we need to keep track of seek position
- don't see why we need to keep track of change locations, using repeated statSync like before
*/
const { Readable } = require('stream');
var fs = require('fs');
class LogStreamer extends Readable {
// bufferSize defaults to 512K
constructor(filename, options) {
super(options);
var self = this;
this.fileHandle = null;
this.watchHandle = null;
this.backoff = false;
self.position = 0;
this.bufferSize = 512000;
this.buffer = Buffer.alloc(this.bufferSize);
self.reopen(filename);
}
reopen(filename) {
var self = this;
var stats;
var reopening = false;
if (self.fileHandle) {
reopening = true;
fs.closeSync(self.fileHandle);
}
if (self.watchHandle) {
self.watchHandle.close();
}
var watchCallback = function(event, filename2) {
var stats;
//console.log('FileWatcher', event, filename);
switch (event) {
case 'rename':
//console.log('rename');
// Try to read the last bit before we handle the rename
if (!self.backoff) {
self._read();
}
self.reopen(filename);
break;
case 'change':
// If this.push() didn't tell us to stop, read to the end if we can
if (!self.backoff) {
self._read();
}
break;
}
};
setTimeout(function() {
stats = fs.statSync(filename);
if (!stats) {
console.log('Does file exist?');
return;
}
self.fileHandle = fs.openSync(filename, 'r');
if (reopening) {
self.position = 0;
} else {
self.position = stats.size;
}
self.watchHandle = fs.watch(filename, {persistent:true}, watchCallback);
}, 200);
}
_read(size) {
var self = this;
if (!self.fileHandle) {
return;
}
self.backoff = false;
fs.read(self.fileHandle, self.buffer, 0, self.bufferSize, self.position, function(error, bytesRead, data) {
if (error) {
console.log('Error', error);
return;
}
if (bytesRead == 0) {
return 0;
}
self.position += bytesRead;
//console.log('Data', data);
if (!self.push(data)) {
self.backoff = true;
}
});
}
};
module.exports = LogStreamer;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment