在Cloud Functions中使用setTimeout可以吗? [英] Is it ok to use setTimeout in Cloud Functions?

查看:66
本文介绍了在Cloud Functions中使用setTimeout可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Firebase Cloud Functions中使用setTimeout是否可以?我的意思是说这对我来说在本地很有效,但是它有一个非常奇怪的行为:超时功能的执行无法预测.

I'm wondering if it's okay to use setTimeout in Firebase Cloud Functions? I mean it's kinda working for me locally, but it has a very weird behavior: Unpredictable execution of the timeout functions.

示例:我将超时设置为5分钟.所以5分钟后执行我的回调.在大多数情况下,它可以正确执行此操作,但有时回调执行的时间要晚于5分钟.

Example: I set the timeout with a duration of 5 minutes. So after 5 minutes execute my callback. Most of the time it does that correctly, but sometimes the callback gets executed a lot later than 5 minutes.

但这只是在我的本地计算机上执行.将功能部署到Firebase时也会发生这种情况吗?

But it's only doing so on my local computer. Is this behavior also happening when I'm deploying my functions to firebase?

推荐答案

Cloud Functions具有可以运行的最大时间,这在

Cloud Functions have a maximum time they can run, which is documented in time limits. If your timeout makes its callback after that time limit expired, the function will likely already have been terminated. The way expiration happens may be different between the local emulator and the hosted environments.

通常,我建议不要超过几秒钟的任何setTimeout.在Cloud Functions中,只要您的功能处于活动状态,就会向您收费.如果您的setTimeout几分钟,那么您就需要为这整个时间计费,即使您的所有代码正在做的事情都是在等待时钟到期.查看您正在等待的服务是否具有调用网络挂钩或

In general I'd recommend against any setTimeout of more than a few seconds. In Cloud Functions you're being billed for as long as your function is active. If you have a setTimeout of a few minutes, you're being billed for all that time, even when all your code is doing is waiting for a clock to expire. It's likely more (cost) efficient to see if the service you're waiting for has the ability to call a webhook, or to use a cron-job to check if it has completed

这篇关于在Cloud Functions中使用setTimeout可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆