Skip to content

Instantly share code, notes, and snippets.

@thinkjson
Created October 20, 2011 16:14
Show Gist options
  • Select an option

  • Save thinkjson/1301560 to your computer and use it in GitHub Desktop.

Select an option

Save thinkjson/1301560 to your computer and use it in GitHub Desktop.
WriteableStream loses writes
var fs = require('fs');
exports = module.exports = function(config) {
/**
* Rotate the log
*/
this.rotateLog = function() {
var timestamp = Math.floor((new Date()).getTime() / 1000);
self.oldLogfile = this.logfile;
self.oldLog = self.log;
self.logfile = self.config.buffer + self.config.input + timestamp + ".csv";
self.log = fs.createWriteStream(self.logfile);
// Push log file to archive
if (self.oldLog) {
self.oldLog.on('close', function() {
fs.rename(self.oldLogfile, self.config.archive + self.config.input + timestamp + ".csv", function(err) {
if (! err) fs.unlink(self.oldLogfile);
});
});
self.oldLog.end();
}
};
if (! config.fields) throw { message: "You must specify the fields to extract from the incoming data" };
var self = this;
self.config = config;
self.rotateLog();
self.interval = config.interval || 3600000;
setInterval(function() {
self.rotateLog();
}, self.interval);
/**
* Post a message to the telemetry server
* Required method
*/
this.post = function(data) {
// Pull selected fields out of data and write as CSV row
var row = [];
for (field in self.config.fields) {
row.push(data[self.config.fields[field]]);
}
var raw_data = '"' + row.join('","') + '"\n';
self.log.write(raw_data, "utf8");
};
};
15000 requests sent with same data
~2600 lines logged to file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment