完成的Firebase函数中的异步调用会怎样? [英] What happens to async calls in completed firebase functions?

查看:87
本文介绍了完成的Firebase函数中的异步调用会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我选择不等待Firebase函数内部的异步调用完成,那么函数完成后,异步调用会如何处理?它继续在哪个线程上运行,它是否仍在函数的runtimeOpts中运行,这如何影响使用情况?

If I choose not to wait for an async call to complete inside a firebase function, what happens to the async call when the function completes? On what thread does it continue, does it still run within the function's runtimeOpts and how does this affect usage?

import * as firebase from 'firebase';
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

exports.action = functions.https.onRequest((req, res) => {
    admin.database().ref('action').add({ 1: 2 });
    res.end(); // or just `return;` for other functions
});

也许甚至

exports.action2 = functions.https.onRequest((req, res) => {
    new Promise(resolve => setTimeout(() => admin.database().ref('action').add({ 1: 2 })}, 10_000));
    res.end();
});

推荐答案

看看这个

Have a look at this section of the documentation on Cloud Functions, which explains why it is "important to manage the lifecycle of a function to ensure that it resolves properly".

如果您要确保运行CF功能(CF)的Cloud Functions实例在CF成功达到其终止条件或状态之前不会关闭"(在您的情况下,异步工作完成时),您需要通过返回JavaScript承诺来解析CF".

If you want to be sure that "the Cloud Functions instance running your Cloud Function (CF) does not shut down before your CF successfully reaches its terminating condition or state" (in your case, when the asynchronous work is done), you need to "resolve your CF by returning a JavaScript promise".

因此,如果您不这样做,则存在运行CF的Cloud Functions实例在对一个或多个异步函数的调用完成之前关闭的风险.

So, if you don't do that, there is the risk that the Cloud Functions instance running your CF shuts down before your calls to one or more async functions complete.

但是,不是并不意味着这种将一直发生.将响应发送回客户端(使用res.end();)导致异步工作完成之后,运行CF的Cloud Functions实例很有可能继续运行.

However, it does NOT mean that this will always happen. It may be very well possible that the Cloud Functions instance running your CF continues to run after you sent back the response to the client (with res.end();) resulting in the async work being completed.

问题是这完全不受您控制(取决于Cloud Functions平台如何管理您的Cloud Function实例):有时它可能会继续运行(完成异步工作),有时可能不会.

The problem is that this is totally out of your control (it depends on how the Cloud Functions platform manages you Cloud Function instances): sometimes it may continue running (completing the async work), sometimes it may not.

您会发现许多有关Stack Overflow上的Cloud Function的问题,这些问题解释了他们的问题是Cloud Function有时正确地执行异步工作,有时却无法正确执行:原因仅仅是因为它们未返回由以下对象返回的Promises:异步调用.

You will find a lot of questions on Cloud Functions on Stack Overflow which explain that their problem is that the Cloud Function sometimes executes the async work correctly and sometimes not: the reason is simply because they don't return the Promises returned by the async calls.

这会影响使用情况吗?

Does this affect usage?

是的,正如文档中明确指出的那样:通过正确终止函数,可以避免运行太长时间或无限循环的函数产生过多的费用."

Yes, as clearly said in the doc: "By terminating functions correctly, you can avoid excessive charges from functions that run for too long or loop infinitely."

因此,具体来说,您应该执行以下操作:

So, concretely, in your case you should do something like:

exports.action = functions.https.onRequest(async (req, res) => {  //  <-- Note the async keyword here
    await admin.database().ref('action').add({ 1: 2 });
    res.end(); // or just `return;` for other functions
});


请注意,如果您的Cloud Function是同步(它不调用任何异步方法),则必须使用简单的return;语句(或通过调用send()redirect())将其终止,或使用end()表示HTTPS云功能).例如:


Note that if your Cloud Function is synchronous (it does not call any asynchronous method) you have to terminate it with a simple return; statement (or by calling send(), redirect(), or end() for an HTTPS Cloud Function). For example:

exports.action = functions.https.onRequest((req, res) => {
    const result = 1 + 3;
    res.status(200).send(result);
});

exports.unusefulCloudFunction = functions.firestore
    .document('users/{userId}').onWrite((change, context) => {
      const result = 1 + 3;
      console.log(result);
      return;
    });

这篇关于完成的Firebase函数中的异步调用会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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