1

I'm wondering if it's okay to use setTimeout in Firebase Cloud Functions? I mean it's kinda working for me locally, but it has a very weird behavior: Unpredictable execution of the timeout functions.

Example: I set the timeout with a duration of 5 minutes. So after 5 minutes execute my callback. Most of the time it does that correctly, but sometimes the callback gets executed a lot later than 5 minutes.

But it's only doing so on my local computer. Is this behavior also happening when I'm deploying my functions to firebase?

Fortuna
  • 611
  • 6
  • 18
  • 1
    I'm not clear what the problem is. Are you asking if setTimeout works on Cloud Functions? (it does) Or are you wondering if it's a good idea to use setTimeout in Cloud Functions? (it's not usually a good idea). Or are you wondering why your code (which you're not showing here) isn't working? – Doug Stevenson Aug 05 '18 at 15:55

1 Answers1

7

Cloud Functions have a maximum time they can run, which is documented in time limits. If your timeout makes its callback after that time limit expired, the function will likely already have been terminated. The way expiration happens may be different between the local emulator and the hosted environments.

In general I'd recommend against any setTimeout of more than a few seconds. In Cloud Functions you're being billed for as long as your function is active. If you have a setTimeout of a few minutes, you're being billed for all that time, even when all your code is doing is waiting for a clock to expire. It's likely more (cost) efficient to see if the service you're waiting for has the ability to call a webhook, or to use a cron-job to check if it has completed

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Thanks Frank, so that's a problem for me unfortunately. What I want to achieve is that some specific code is executed exactly after a preset time. In my case I have a match between two players. And when player A is ready (`ready = true`) I want to run a "countdown" (via setTimeout) of 5 minutes. If player B is not ready in this timeframe, player A will get a default win. Do you have an idea how I could solve that problem? I'm afraid cronjobs won't since the timing is too inaccurate. – Fortuna Aug 05 '18 at 16:06
  • 1
    Such accurate time will need a process that stays alive. I'd probably use an app engine instance for that. Or use the clients themselves to do the countdown and then trigger a Cloud Function from there to determine the winner. – Frank van Puffelen Aug 05 '18 at 16:10
  • I'm afraid the clients can't handle that, since they could go offline. Do you have a link where I find more information about that "app engine instance" topic? Btw. I'm working on a web app if that helps. – Fortuna Aug 05 '18 at 16:13
  • If both clients go offline, does the exactness of the timeout really matter all that much. I.e. a cron-triggered function could probably handle that case just fine, scanning for expired-and-abandoned games every minute or so. It would still lead to lower functions usage than keeping an instance alive for 5 minutes for every game. – Frank van Puffelen Aug 05 '18 at 16:25
  • Hey Frank I just wanted to say thank you again. Your comment brought me to the idea to solve that issue on the client side, just like you suggested. I'm gonna let the client do the work and in case they go offline and come online again, I run a Cloud Function call in the background. The players can enter the match site only until the background function call has been resolved. This will make sure the view will display the right information. – Fortuna Aug 06 '18 at 09:20
  • Good to hear! It's very natural to fall back to our age-old "everything should happen on the server" mindset. :-) – Frank van Puffelen Aug 06 '18 at 14:16