Hi,
So here’s what I want to do (and it works fine testing locally with netlify dev, but not when on Netlify):
I have a function that writes a file to /tmp and then waits and checks whether it’s been modified by another function before returning. Now while that first function is still running and waiting, the second function can’t read that file, I get an error that no file was found. When the first function finishes after waiting up to 10 seconds (I believe that’s the limit?), the second function can then read that file.
As an example, I could do this in the first function:
let data = {hello: 'world'};
fs.writeFileSync('/tmp/data.json', JSON.stringify(data));
await waitTimer(); //a simple function that returns a Promise that resolves after some amount of time, like 8 seconds, just for testing
return {statusCode: 200, body: JSON.stringify({status: 'Done'})};
Then in the second function:
let dataFileJson = fs.readFileSync('/tmp/data.json');
let data = JSON.parse(dataFileJson);
return {statusCode: 200, body: JSON.stringify({status: 'Done', data})};
This will fail while the first function is still running, with an error that the file can’t be found, and as I mentioned before it’ll work fine once the first function is finished, when it returns.
So why can’t I access that file while the function is running? I’m writing it to /tmp when the function begins and yet it can’t be found until after the function ends. I can even read the file from the first function while it’s waiting, just not the second. Is there any way to make this work?
My aim is to send some data on one device, save it to a file and then read it from another device, a way of syncing a small amount of non-important data without having to use a database. So the first device sends the data and waits to either receive some merged data back, or for the other device to receive the data, or just timing out because nothing happened, then removing the data from the file.
I’m doing a similar thing on a NodeJS server that I created myself, but really wanted to get it working on serverless functions.
I’m aware that I could just return the first function after saving the data and start querying multiple times from the client side to check on progress, I just would have rather done that on the server. I’m really just curious why my method isn’t working.
Thanks for any help.