2

I have a cloud function that downloads a file to the /tmp/ dir of node instance.

I have two questions:

1) If two instances of this cloud function run at the same time and both save files to this /tmp/ directory... will the files be seen by both instances?

2) Because of the possible collision of files in my first question. I want to know if cloud functions lets you create directories within /tmp/. I tried using fs.mkdirSync(newPath) but when I run code it always says that the new directory doesn't exist.

Badrush
  • 1,247
  • 1
  • 17
  • 35

1 Answers1

4

Cloud Functions only allows one function to run at a time in a particular server instances. Functions running in parallel run on different server instances, which have different /tmp spaces. Each function invocation runs in complete isolation from each other. You should always clean up files you write in /tmp so they don't accumulate and cause a server instance to run out of memory over time.

You may find it helpful to watch this video about function isolation:

https://www.youtube.com/watch?v=rCpKxpIMg6o

And this video about managing resources, especially /tmp:

https://www.youtube.com/watch?v=2mjfI0FYP7Y

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you. I was hoping that might be the case. Do you know if I'm allowed to create directories within /tmp? – Badrush Feb 27 '19 at 16:46
  • 2
    You can do whatever you want in /tmp, as long as it fits in memory. – Doug Stevenson Feb 27 '19 at 16:46
  • Okay thanks for clearing that up. The `ENOENT: no such file or directory` I'm getting must not be because of that. Last question, do I need to use `fs.mkdir()` to create a directory before writing files to it or can I save files directly to a new subdirectory of /tmp ? – Badrush Feb 27 '19 at 16:48
  • If you have questions beyond the one you first asked, please post a separate question and explain the new problem so that it's easier for people to find it in search results. Comments is not the best place to ask new questions. – Doug Stevenson Feb 27 '19 at 16:50
  • 1
    fair enough. https://stackoverflow.com/questions/54910772/is-fs-mkdir-required-to-create-a-sub-directory-within-tmp-in-firebase-cloud-f – Badrush Feb 27 '19 at 17:00