Prevent EBUSY error with FileSystem and Node.js

I've an async queue with 30 workers, it writes to a single file on Windows 8.1.

My problem is that, being 30 workers, lot of times I get an EBUSY error.

This is how I write to the file:

fs.appendFile("queue-done.json", JSON.stringify(element) + ",\n", function (err) {
    if (err) { console.error(err); }
    done();
});

How can I prevent this error in a clean way?

Use the following process:

  • Get the file handle
  • Rename the file
  • Append to the renamed file

References