superpowers / superpowers-core

:octopus: Superpowers — Extensible HTML5 app for real-time collaborative projects
http://superpowers-html5.com/
Other
1.97k stars 149 forks source link

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

Open elisee opened 8 years ago

elisee commented 8 years ago

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:

florentpoujol commented 8 years ago

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 commented 8 years ago

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.