Created
June 21, 2014 23:09
-
-
Save diega/a7cd2d666b78124741f3 to your computer and use it in GitHub Desktop.
This file contains 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 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