I have a firebase function that looks like like this:
export const syncDataScheduled = onSchedule({
schedule: "every 1 minutes",
timeoutSeconds: 70, //we don't want the function to run for longer than 1 minute (will give it some extra time for cleanup)
maxInstances: 5,
timeZone: 'America/New_York',
concurrency: 5,
}, async () => {
let terminateFunction = false;
// firebase functions can only run max every minute, so we need to loop for 1 minute since we want to run this every 3 seconds.
setTimeout(() => terminateFunction = true, 59_000);
while (true) {
// we don't want the function to run for longer than 1 minute, since we schedule it to run every minute anyway.
if (terminateFunction) break;
await syncData();
await new Promise(resolve => setTimeout(resolve, 3_000));
}
});
I want to sync some company data every 3 seconds. But since Google Firebase functions has a minimum schedule time of every one minute, I have to loop till the minute is up.
The problem is, that Firebase sometimes skips a run. Which means that the function doesn't run for a whole minute.
See below the gaps in logs.
Can anyone help?
Turns out, the issue was that we used the fetch API on Node 18, which is experimental. This function makes around 12 http requests using fetch every 3 seconds, and sometimes a promise returned from one of the fetch calls wouldn't "fulfill" - meaning, it wouldn't either resolve or reject.
I simply changed the Node version number from 18 to 20, and it started working just fine.