I have a timed Cloud Function which I am trying to test in the Firebase Emulator. To do this, I am using the solution described here (How to invoke firebase Schedule functions locally using pubsub emulator) which triggers the scheduled function using a Pub/Sub message.
This solution successfully triggers the function, but it seems to be repeating its calls more than expected. For example, when I run the below code, it should be calling the scheduled function every minute: however, it runs the code once after a minute, then twice after 2 minutes (so it has run 3 times total), then three times after 3 times (so it has run 6 times total), and continues to increase its calls from there, which suggests setInterval() is being called more than once (I have setInterval() in my Firebase index.js file). Is there anyway to fix this? Thank you very much.
const pubsub = new PubSub({
apiEndpoint: 'localhost:8085'
});
setInterval(() => {
const SCHEDULED_FUNCTION_TOPIC = 'firebase-schedule-yourFunctionName';
console.log(`Trigger sheduled function via PubSub topic: ${SCHEDULED_FUNCTION_TOPIC}`);
const msg = await pubsub.topic(SCHEDULED_FUNCTION_TOPIC).publishJSON({
foo: 'bar',
}, { attr1: 'value1' });
}, 1 * 60 * 1000); // every 1 minute