[英]Firebase/google cloud functions time based trigger prevent concurrent executions
I have a firebase function that runs every 2 minutes.我有一个 firebase function 每 2 分钟运行一次。 The problem is that sometimes it takes over 540sec.
问题是有时需要超过 540 秒。 to finish.
完成。 Hence two executions of the function occur which messes up things.
因此,发生了两次 function 的执行,这使事情变得混乱。 Is there a way to ensure that the function does not fire till a previous instance finishes?
有没有办法确保 function 在前一个实例完成之前不会触发?
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.我尝试使用存储在 firestore 中的标志来处理它,该标志在 function 开始运行时设置为
true
,在 function 完成时设置为false
。 However sometimes function execution times out hence the flag is never set to false
, thereby stopping all future executions.但是有时 function 执行超时,因此该标志永远不会设置为
false
,从而停止所有未来的执行。
So how do I make sure that only one execution of the function is running at a time?那么如何确保一次只运行一次 function 的执行?
You can limit the number of instances using the runWith
method and using the maxInstances
parameter.您可以使用
runWith
方法和使用maxInstances
参数来限制实例的数量。 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
您可以发布代码的相关部分,以便我们了解原因,或者您可以 在此处了解如何终止 function
Is there a way to ensure that the function does not fire till a previous instance finishes?
有没有办法确保 function 在前一个实例完成之前不会触发?
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.不,您必须像现在一样在数据库中存储一些值,如果实例处于活动状态,则终止 function。
However sometimes function execution times out hence the flag is never set to false, thereby stopping all future executions.
但是有时 function 执行超时,因此该标志永远不会设置为 false,从而停止所有未来的执行。
Checkout Cloud Functions V2 (beta) or Cloud Run itself that can run up to 1 hour .结帐Cloud Functions V2(测试版)或Cloud Run本身,可以运行长达1 小时。
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.此外,如果您知道 function 每次执行将花费超过 540 秒,那么最好增加两次调用之间的间隔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.