I am using appendFile method to append content to a file. The following is an over-simplified version of that code. It does suffice to explain the problem. My problem is that if the process is killed, the content of the file lags the last data written thru appendFile. In other words, all the data that is passed to fs.appendFile to get appended does not get written to disk. How to get around this limitation? I would prefer NOT to use sync versions of any of the fs methods
fs = require('fs');
myVal = 1;
setInterval (function() {
++myVal;
fs.appendFile("/tmp/test.d", myVal +":",'utf8', function(err) {
console.log(myVal);
});
}, 10000);
~
The fs. appendFile() method is used to asynchronously append the given data to a file. A new file is created if it does not exist. The options parameter can be used to modify the behavior of the operation.
fs. writeFileSync and fs. writeFile both overwrite the file by default. Therefore, we don't have to add any extra checks.
writeFile with the filename, and data to write to that file (as a string or a buffer). That will overwrite the entire file, so to just append that data to the file instead, pass an options object with the flag key set to a . Or, you can use fs. appendFile .
The fs. close() method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files. Calling fs.
You can't do this in the call itself, but you can certainly call fs.fdatasync()
to flush the previous write to disk. To do so, you must change the appendFile
call to use a previously opened file descriptor rather than a string filename, so you'll end up with three steps:
// Open the file
fs.open(filename, "a+",(err, fd) => {
// Write our data
fs.writeFile(fd, data, (err) => {
// Force the file to be flushed
fs.fdatasync(fd /*, optional callback here */);
});
});
Make sure you close the file when you are done with it. Personally, I question the value of this approach when such an easy and obvious option as appendFileSync
exists precisely for this purpose. It will make the program more difficult to understand, without actually adding any value.
But it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With