Skip to content

Instantly share code, notes, and snippets.

@diega
Created June 21, 2014 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diega/a7cd2d666b78124741f3 to your computer and use it in GitHub Desktop.
Save diega/a7cd2d666b78124741f3 to your computer and use it in GitHub Desktop.
var vertx = require("vertx");
var console = require("vertx/console");
var eb = require('vertx/event_bus');
var fs = vertx.fileSystem;
var path = "data.log";
registerAppend = function() {
var unwrittenBytes = 0;
fs.open(path, fs.OPEN_WRITE | fs.CREATE_NEW, true, function(err, asyncFile) {
if (!err) {
eb.registerHandler(
"append",
function(msg) {
var buff = new vertx.Buffer(java.util.Calendar.getInstance().getTimeInMillis() + "\n");
fs.props(path, function(err, props) {
if (!err) {
asyncFile.write(buff, props.size + unwrittenBytes, function(err) {
if (!err) {
unwrittenBytes -= buff.length();
} else {
console.log("Failed to write file: " + err);
}
});
unwrittenBytes += buff.length();
} else {
console.log("Failed to retrieve file props: " + err);
}
});
},
function() {
for (var i = 0; i < 1000; i++) {
eb.publish("append");
}
});
} else {
console.error("Failed to open file: " + path, err);
}
});
};
fs.exists(path, function(err, exists) {
if (!err) {
if (exists) {
registerAppend();
} else {
fs.createFile(path, function(err) {
if (!err) {
registerAppend();
} else {
console.log("Failed to create file: " + err);
}
});
}
} else {
console.log("Failed to check file existence: " + err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment