I've got two node threads running, one watching a directory for file consumption and another that is responsible for writing files to given directories.
Typically they won't be operating on the same directory, but for an edge case I'm working on they will be.
It appears that the consuming app is grabbing the files before they are fully written, resulting in corrupt files.
Is there a way I can lock the file until the writing is complete? I've looked into the lockfile
module, but unfortunately I don't believe it will work for this particular application.
=====
The full code is far more than makes sense to put here, but the gist of it is this:
- App spins off the watchers and listener
Listener:
- listen for file being added to db, export it using
fs.writeFile
Watcher:
- watcher uses
chokidar
to track added files in each watched directory - when found
fs.access
is called to ensure we have access to the filefs.access
seems to be unfazed by file being written
- the file is consumed via
fs.createReadStream
and then sent to server- filestream is necessary as we need the file hash
In this case the file is exported to the watched directory and then reimported by the watch process.