[SOLVED] Firebase/google cloud functions time based trigger prevent concurrent executions

Issue

This Content is from Stack Overflow. Question asked by rahulserver

I have a firebase function that runs every 2 minutes. The problem is that sometimes it takes over 540sec. to finish. Hence two executions of the function occur which messes up things.
Is there a way to ensure that the function does not fire till a previous instance finishes?

I tried to handle it using a flag stored in firestore which was set to true when function would start running, and false when function would finish. However sometimes function execution times out hence the flag is never set to false, thereby stopping all future executions.

So how do I make sure that only one execution of the function is running at a time?



Solution

You can limit the number of instances using the runWith method and using the maxInstances parameter. Read more here.

By the way, why are your functions taking too long to execute? are you terminating them correctly? You can post relevant part of you code so we can see why or you can learn about how to terminate your function here


This Question was asked in StackOverflow by rahulserver and Answered by RobLjm It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?