繁体   English   中英

Google Cloud Functions - 重试

[英]Google Cloud Functions - Retry

我有这个幂等函数,其中包含我为 Google Cloud Functions 编写的多个 promise。 我想启用重试,因为我使用的 API 非常不一致。 这需要在需要重试时返回被拒绝的承诺。

因此,我尝试返回一个 promise.all([]) ,但是当其中一个承诺失败时,它不会终止/停止该函数。 然后它甚至继续进行 promise.all().then()? 只有当所有 4 个承诺都成功时才会发生这种情况。

谁能指出我正确的方向? 我正在尝试的事情甚至有意义吗?

exports.scheduleTask = functions
  .firestore.document("tasks_schedule/{servicebonnummer}")
  .onCreate((snap, context) => {

    servicebonnummer = snap.data().data.servicebonnummer;
    bondatum = snap.data().data.bondatum;
    servicestatus = snap.data().data.servicestatus;
    tijdstip = snap.data().data.tijdstip;
    firestorePromise = null;
    firestoreFinish = null;
    cashPromise = null;
    cashFinish = null;

    //Firebase
    //firestoreSchedule executes the required promise
    //checkFinished points to a database where it checks a boolean for idempotency
    //firestoreFinish writes to this database and sets the boolean to true when the promise is successful

    if (!checkFinished("tasks_schedule", servicebonnummer, "firestore")) {
      firestorePromise = scheduleFirestore(
        servicebonnummer,
        bondatum,
        servicestatus,
        tijdstip
      )
        .then(output => {
          firestoreFinish = markFinished(
            "tasks_schedule",
            servicebonnummer,
            "firestore"
          );
          return output;
        })
        .catch(error => {
          console.error(
            "scheduleFirestore - Error connecting to Firestore: ",
            error
          );
          return error;
        });
    }

    //SOAP API
    //cashSchedule executes the required promise
    //checkFinished points to a database where it checks a boolean for idempotency
    //cashFinish writes to this database and sets the boolean to true when the promise is successful

    if (!checkFinished("tasks_schedule", servicebonnummer, "cash")) {
      cashPromise = scheduleCash(
        servicebonnummer,
        moment(bondatum),
        servicestatus,
        tijdstip
      )
        .then(result => {
          if (result[0].response.code === "2") {
            cashFinish = markFinished(
              "tasks_schedule",
              servicebonnummer,
              "cash"
            );
            return result;
          }
          throw new Error("Validation error, response not successful");
        })
        .catch(error => {
          console.error("scheduleCash - Error connecting to CASH API: ", error);
          return error;
        });
    }

    //CHECK PROMISES

    return Promise.all([
      firestorePromise,
      firestoreFinish,
      cashPromise,
      cashFinish
    ])
      .then(result => {
        removeTask("tasks_schedule", servicebonnummer);
        return result;
      })
      .catch(error => {
        console.error("scheduleTask - Retry: ", error);
        return error;
      });
  });

如果你编码:

let somePromise = new Promise(...);
return somePromise.then(funcA).catch(funcB);

那么你确实是在回报一个承诺。 但是,由于您的代码中有该承诺的处理程序,我们需要更详细地查看发生了什么。 让我们假设somePromise被拒绝。 这意味着将调用catch()处理程序。 这是一个catch处理程序,这将是返回的承诺的最终解决的结果。

如果我们查看Promise.catch()的 MDN 文档,我们会发现以下内容:

如果 onRejected 抛出错误或返回一个本身被拒绝的 Promise,则 catch() 返回的 Promise 将被拒绝; 否则,它被解决。

如果我们查看您的代码,

catch(error => {
  console.error("scheduleTask - Retry: ", error);
  return error;
});

现在问:

  1. 这段代码会抛出错误吗? 不……它没有 throw 语句,因此只返回传入的值。
  2. 代码是否返回 Promise? 不......它传递了一个错误值并简单地返回该错误值,我很确定它本身不会是一个承诺。

这意味着返回的整体 Promise 以已解决状态而非拒绝状态结束,因此整体 Cloud Function 被视为已结束且不会重试。

您的选择可能是:

catch(error => {
  console.error("scheduleTask - Retry: ", error);
  throw error;
});

或者

catch(error => {
  console.error("scheduleTask - Retry: ", error);
  return Promise.reject(error);
});

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM