如何解决Firebase的Google Cloud函数上的截止日期超出错误?

时间:2019-09-09 18:17:10

标签: node.js firebase google-cloud-firestore google-cloud-functions

我们正在使用带有Google Cloud功能的Firebase项目。我们有出色的计划,但是在处理从Firebase读取/向Firebase写入大量文档时,后台功能面临一些执行问题。

函数会在执行一段时间后抛出一些错误,或者根本无法完成任务。

我试图扩大分配给该功能的RAM(2 GB)和等待时间(540秒)

Index.js:

exports.createRDTCode = functions.runWith({ timeoutSeconds: 540,
  memory: '2GB'}).firestore.document("xxxxx/{project}").onUpdate((change, context, callback) => { 
  return createRDTCode.handler(change,context,callback)
})

createRDTCode.js:

const admin = require("firebase-admin");

const firestore = admin.firestore();

exports.handler = async (change, context) => {
  const newValue = change.after.data();
  const previousValue = change.before.data();
  let barsConsecutivo = newValue.barras;

  if (
    newValue.writeCode != previousValue.writeCode &&
    newValue.writeCode == true
  ) {
    const drillingRDTRef = admin
      .firestore()
      .collection("xxxxxxx")
      .doc(context.params.project);

    drillingRDTRef.update({
      writeCode: false
    });
    const project = context.params.project.split("_", 2)[0];
    const company = context.params.project.split("_", 2)[1];

    const projectRef = admin
      .firestore()
      .collection("xxxxxxx")
      .doc(context.params.project);

    const barsRef = admin
      .firestore()
      .collection("xxxxx")
      .doc(context.params.project)
      .collection("xxxxx")
      .where("idRDT", "==", null);


    barsRef.get().then(snapshot => {
      snapshot.forEach(rdtDoc => {
        let barCode = (
          company.substr(0, 2) +
          project.substr(0, 2) +
          "BA" +
          barsConsecutivo.toString()
        ).toUpperCase();

        admin
          .firestore()
          .collection("xxxxxx")
          .doc(context.params.project)
          .collection("xxxxx")
          .doc(rdtDoc.id)
          .update({
            idRDT: barCode
          });

        projectRef.update({
          drillingRDT: admin.firestore.FieldValue.arrayUnion(barCode),
          inventory: admin.firestore.FieldValue.arrayUnion(barCode)
        });

        admin
          .firestore()
          .collection("xxxxxxx")
          .doc(context.params.project)
          .collection(rdtDoc.data().typeInventory.toLowerCase())
          .doc(barCode)
          .set({
            typeRDT: rdtDoc.data().tipoRDT,
            codeRDT: barCode,
            statusRDT: rdtDoc.data().statusRDT,
            rigArray: rdtDoc.data().rigArray,
            typeInventory: rdtDoc.data().typeInventory.toLowerCase(),
            numberHoles: rdtDoc.data().numberHoles,
            lengthHole: rdtDoc.data().lengthHole
          });

        barsConsecutivo += 1;
      });

      const drillingRDTRef = admin
        .firestore()
        .collection("xxxxxxx")
        .doc(context.params.project);


      drillingRDTRef.update({
        barras: barsConsecutivo

      });


    });
  }


};


我们得到的错误是:

  

错误:已超过最后期限   在Http2CallStream.call.on(/srv/node_modules/@grpc/grpc-js/build/src/client.js:96:45)   在emitOne(events.js:121:20)   在Http2CallStream.emit(events.js:211:7)   在process.nextTick(/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22)   在_combinedTickCallback(内部/进程/next_tick.js:132:7)   在process._tickDomainCallback(internal / process / next_tick.js:219:9)

0 个答案:

没有答案