Node.js fs.writeFile() empties the file

后端 未结 3 1748
自闭症患者
自闭症患者 2021-01-15 05:56

I have an update method which gets called about every 16-40ms, and inside I have this code:

this.fs.writeFile(\"./data.json\", JSON.stringify({
    totalPlay         


        
3条回答
  •  鱼传尺愫
    2021-01-15 06:59

    I didn't run some real tests with this I just noticed with manually reloading my ide that sometime the file was empty. What I tried first was the rename method and noted the same problem, but recreating a new file was less desirable (considering file watches etc.).

    My suggestion or what I'm doing now is in your own readFileSync I check if the file is missing or data returned is empty and sleep for a 100 milliseconds before giving it another try. I suppose a third try with more delay would really push the sigma up a notch but currently not going do it as the added delay is hopefully an unnecessary negative (would consider a promise at that point). There are other recovery option opportunities relative to your own code you can add just in case I hopefully. File not found or empty? is basically a retry another way.

    My custom writeFileSync has an added flag to toggle between using the rename method (with write sub-dir '._new' creation) or the normal direct method as your code's need may vary. Possible based on file size is my recommendation.

    In this use case the files are small and only updated by one node instance / server at a time. I can see adding the random file name as another option with rename to allow multiple machines to write another option for later if needed. Maybe a retry limit argument as well?

    I was also thinking that you could write to a local temp and then copy to share target by some means (maybe also rename on target for speed increase), and then clean up (unlink from local temp) of course. I guess that idea is kind of pushing it to shell commands so not better. Anyway still the main idea here is to read twice if found empty. I'm sure it's safe from being partially written, via nodejs 8+ on to a shared Ubuntu type NFS mount right?

提交回复
热议问题