My Firebase function is set to run every 20 minutes using setInterval, but it seems to be executing more frequently than expected. Here is an excerpt from my code:
try {
const response = await axios.get(
"https://ll.thespacedevs.com/2.0.0/event/upcoming/?limit=50&offset=0",
{ timeout: 90000 }
);
return response.data.results;
} catch (error) {
console.error("Error fetching space events:", error);
if (retryCount < 3) {
// Retry up to 3 times
const waitTime = 900000 * (retryCount + 1); // 15, 30, 45 minutes
console.log(`Retrying in ${waitTime / 60000} minutes...`);
await new Promise((resolve) => setTimeout(resolve, waitTime));
return fetchSpaceEvents(retryCount + 1);
} else {
throw new Error("Max retries reached");
}
}
};
// Database update logic
const updateDatabase = async () => {
console.log("UPDATED THE DATABASE AT", new Date());
try {
const spaceEvents = await fetchSpaceEvents();
if (spaceEvents) {
const client = await getClient();
for (const event of spaceEvents) {
const query = { id: event.id };
const update = { $set: event };
const options = { upsert: true };
event.interested = event.interested ?? 0;
event.comments = event.comments ?? [];
await client
.db()
.collection<SpaceEvent>("SpaceEvents")
.updateOne(query, update, options);
}
}
} catch (error) {
console.error("Error updating database with space events:", error);
}
};
setInterval(updateDatabase, 1200000); // 20 minutes
The database update logs are showing more frequent updates than the specified 20-minute interval. For example, I see updates at times like 11:47:04, then 11:49:41, 11:49:53, and so on, which is shorter than intended.
I am using Firebase and running firebase emulators:start --only functions for local testing. I'm not sure why the function is triggering more often than expected. Is this a known issue with Firebase functions or the emulator, or is there a problem in my code causing this behavior?
If you have any insights or advice on troubleshooting this issue, please share them. Your help would be greatly appreciated.