【问题标题】:firebase HTTP Cloud Function TimeOut?firebase HTTP Cloud 函数超时?
【发布时间】:2019-06-07 03:57:52
【问题描述】:

我正在开发在线预订安卓应用项目。它使用 Firestore 和 Cloud Function。

应用内预订经历三个阶段

  1. phase(Initiate Order) :我收到订单请求并点击 makeOrder() http 可调用云函数,该函数在预订集合中创建一个文档,其中包含字段 status= "inProcess" 并使用 CHECKSUMHASH 发送响应(用于稍后付款)给客户。

  2. phase(do payment):如果第一阶段成功完成,现在用户必须使用第一阶段生成的CHECKSUMHASH进行支付。如果支付 TXN 成功,那么支付 API (more) 也会给出 CHECKSUMHASH 作为响应。现在我必须验证 在服务器端CHECKSUMHASH 确保请求没有被篡改。

  3. phase(Confirm Order):在这个阶段CHECKSUMHASH(由支付APT生成)必须被验证,所以我调用另一个HTTP云函数confirmOrder()。如果方法verifychecksum() 返回true,那么我将状态"inProcess" 更改为"pending"

现在的问题是它总是需要超过 60 秒,所以给出错误 TimeOut。

我的问题是

  1. 创建文档后我正在检查文档是否存在

    db.collection(targetColRef).doc(data.TARGET_ID).collection('orders').doc(data.ORDER_ID).get();
    const time = getCurrentDate().time;
    if (doc.exists && (time - doc.orderTime) < 300) 
    
  2. 我按这三个阶段预订订单,这样做是否正确?

    exports.confirmOrder = functions.https.onCall((data, context) => __awaiter(this, void 0, void 0, function* () {
        if(validateData()){
    
             yield new Promise((reject, resolve) => {
                      return verifychecksum(data,paytm_config.MERCHANT_KEY);
    
                  }).then((result) => __awaiter(this, void 0, void 0, function* () {
                      if (result) {
                         const doc = yield db.collection(targetColRef).doc(data.TARGET_ID).collection('orders').doc(data.ORDER_ID).get();
                           const time = getCurrentDateNumber().time;
                           if (doc.exists && (time - doc.orderTime) < 300) {
                          yield db.collection(targetColRef).doc(data.TARGET_ID).collection('orders').doc(data.ORDER_ID).update({ status: 'pending' });
                          }
                          else { //doc not exists
                             console.log("Order not exist Or time diff more than 300 sec ! /DATA=>" + JSON.stringify(data) + "/Context=>" + JSON.stringify(context));
                              throw new functions.https.HttpsError('permission-denied', 'Time Out!');
                          }
                      }
                      else { // result==false
                          console.log("Failed to verify checksum! /DATA=>" + JSON.stringify(data) + "/Context=>" + JSON.stringify(context));
                          throw new functions.https.HttpsError('permission-denied', 'bad request');
                      }
                      //..
                  })).catch((err) => {
                      throw new functions.https.HttpsError('permission-denied', err);
                  });
    
                  console.log("successfull");
    
                  return "successfull";
     }else{
            //.... }})):
    

【问题讨论】:

  • 没有必要为你的英语道歉,因为它是完全可以理解的。但请使用 Stack Overflow 的格式化工具,让您的问题看起来不错。
  • 当然先生@Frankvanpuffelen

标签: android google-cloud-firestore google-cloud-functions


【解决方案1】:

您的 Cloud Functions 执行环境要求您返回一个值或一个 Promise,以便它能够可靠地知道您的函数何时完成其工作。

如果您的云函数在 60 秒后超时,则非常很可能您没有返回任何结果。就你而言,我对function* 高度怀疑,并且:

yield new Promise((reject, resolve) => {

我看不到您在哪里完成生成器功能。我很确定 __awaiter 与它有关,但我也很确定它不适合你。

我建议重构代码以使用更简单和更常用的原语,例如:

return new Promise((reject, resolve) => {

如果你有多个 promise 需要在函数完成之前完成,请查看Promise.all(),它比生成器函数更常见。

我强烈建议在 promises in Cloud Functions 上查看 Doug 的视频。

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2018-01-03
    • 2020-06-09
    • 2019-07-03
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多