Home > Enterprise >  Firebase/google cloud functions time based trigger prevent concurrent executions
Firebase/google cloud functions time based trigger prevent concurrent executions

Time:09-19

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?

CodePudding user response:

You can limit the number of instances using the runWith method and using the maxIntances 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

CodePudding user response:

Is there a way to ensure that the function does not fire till a previous instance finishes?

No. You'll have to store some value in a database as you are doing now and terminate the function if an instance is active.

However sometimes function execution times out hence the flag is never set to false, thereby stopping all future executions.

Checkout Cloud Functions V2 (beta) or Cloud Run itself that can run up to 1 hour.

Also, if you know a function execution is going to take more than 540 seconds every time, it might be best to increase the interval between 2 invocations.

  • Related