Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Provide an atomic way for systems/plugins to write files #66

Open
elisee opened this issue Jan 17, 2016 · 2 comments
Open

Provide an atomic way for systems/plugins to write files #66

elisee opened this issue Jan 17, 2016 · 2 comments

Comments

@elisee
Copy link
Contributor

elisee commented Jan 17, 2016

If the Superpowers server happens to crash, it is important that files are fully written to minimize the risk of user data corruption.

We should have a facility that writes to a temporary file and then renames it in an atomic operation on top of the existing file when it is done writing. There are probably npm modules to do this. The following is important:

  • The API we expose to plugins and systems should be simple to use and fool-proof
  • The temporary files should have a unique name and be created next to the final files, so that if and when we crash, the temporary files can be easily located and used for data recovery.
@florentpoujol
Copy link
Contributor

If we are going to write all the time on the disk, wouldn't it be simpler to decrease the interval between the "change" event and the actuel write operation ?

I believe the delay between the two was precisely to prevent writing on the disk all the time ?

@elisee elisee changed the title Use and provide an atomic way for systems/plugins to write files Provide an atomic way for systems/plugins to write files Jan 18, 2016
@elisee
Copy link
Contributor Author

elisee commented Jan 18, 2016

This issue is not about saving right away all the time (that would not be a good idea, for the reasons you mentioned). It's about making sure that when we do write (at most once every 60s for a particular asset), if there's a crash at that precise moment, we don't want to end up with empty or incomplete files because they were in the process of being overwritten. It's a rare bug but it has happened a few times.

So instead of doing:

fs.writeFile("existingFile.json", ...);`

We'll do

fs.writeFile("temporaryFile.json", () => { fs.rename("temporaryFile.json", "existingFile.json"); ... });

But with a helper function that deals with all the details for us.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants