Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make fs.appendFile method of Node.js flush to disk right away?

Tags:

node.js

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);

~

like image 329
Sunny Avatar asked Feb 16 '17 15:02

Sunny


People also ask

What is true about appendFile function of fs module?

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.

Does fs writeFile overwrite?

fs. writeFileSync and fs. writeFile both overwrite the file by default. Therefore, we don't have to add any extra checks.

What is the difference between writeFile and appendFile?

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 .

Which method of fs module is used to close a file?

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.


1 Answers

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.

like image 55
Chad Robinson Avatar answered Nov 03 '22 17:11

Chad Robinson