简体   繁体   English

Firebase/谷歌云功能基于时间的触发器防止并发执行

[英]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.

相关问题 Firebase 的云功能是否准时触发? - Cloud Functions for Firebase trigger on time? 带有 Firebase 数据库触发器的第二代 Google Cloud Functions - Google Cloud Functions 2nd Generation with Firebase Database trigger 在特定日期和时间触发云 function firebase - Trigger cloud function on specific date and time firebase 如何防止 Google Cloud Engine 运行多个并发实例? - How to prevent Google Cloud Engine to run multiple concurrent instances? 如何设置firebase触发邮件和云功能 - How to setup firebase trigger-mail and cloud functions Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists AWS Step Functions 标准工作流程并发执行限制 - AWS Step Functions standard workflows concurrent executions limit 我可以在 Cloud Functions Http 触发器中创建 Firebase 身份验证用户吗? - Can I create a user on Firebase Authentication in Cloud Functions Http Trigger? firebase 的谷歌云函数中的“获取”函数是否每次都读取每个文档? - Are "get" functions in Google cloud functions for firebase reading each document everytime? Cloud Functions for Firebase 超时 - Cloud Functions for Firebase timeout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM