I need a firebase cloud function to trigger every 20 seconds.

1,858 views
Skip to first unread message

RobG

unread,
Jul 26, 2020, 11:33:02 AM7/26/20
to Firebase Google Group
I need a firebase cloud function to trigger every 20 seconds, but since the cloud scheduler has a ground limit of "every 1 minutes" frequency I can't do that, what is the best way to solve this?

Michael Bleigh

unread,
Jul 27, 2020, 1:06:42 AM7/27/20
to Firebase Google Group
The simplest way to do this would be to use timeouts to control your execution so that it executes three times within a loop:

function sleep(duration) { return new Promise(resolve => setTimeout(duration, resolve)); }

// in your scheduled function that runs every minute
const promises = [];
promises.push(doThing());
await sleep(20000);
promises.push(doThing());
await sleep(20000);
promises.push(doThing());
await Promise.all(promises);

The downside to this is that you will be charged for the execution time while waiting, so you will be guaranteed to be charged for 40s of execution time every minute. There is not a simple answer outside of this, though -- schedules can only execute at fastest one minute so you would need to set up a different way to trigger an event (i.e. a VM that dumps a message into PubSub every 20s) to achieve it other ways.

On Sun, Jul 26, 2020 at 8:33 AM RobG <robg...@gmail.com> wrote:
I need a firebase cloud function to trigger every 20 seconds, but since the cloud scheduler has a ground limit of "every 1 minutes" frequency I can't do that, what is the best way to solve this?

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/0dd076f3-1432-4c87-8c41-36a114d95181o%40googlegroups.com.

Tim Schönborn

unread,
Jun 23, 2021, 4:28:14 AM6/23/21
to Firebase Google Group
In case anyone finds this and then wonders why the function doesn't sleep:

There's a typo in the first line, it should be setTimeout(resolve, duration) (instead of (duration, resolve)).

Kato Richardson

unread,
Jun 23, 2021, 10:38:27 AM6/23/21
to Firebase Google Group
How unfortunate. Additionally, the linter can't catch this since both are integers. So annoying. Yet another case for State patterns vs argument lists. 

Good eye on spotting a solution!



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages