Sunny
Sunny

Reputation: 10105

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

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

~

Upvotes: 7

Views: 7166

Answers (2)

Scotty Jamison
Scotty Jamison

Reputation: 13289

As of version 21, Node now supports a "flush" parameter.

Use it like so:

await fs.promises.appendFile(path, data, { flush: true });

See the usage docs for more information.

Upvotes: 0

Chad Robinson
Chad Robinson

Reputation: 4623

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.

Upvotes: 8

Related Questions