Created
October 20, 2011 16:14
-
-
Save thinkjson/1301560 to your computer and use it in GitHub Desktop.
WriteableStream loses writes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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