next.js Firebase云函数获取404超时错误

gcuhipw9  于 2022-12-29  发布在  其他
关注(0)|答案(1)|浏览(94)

我试着运行一个firebase云函数,它读取5k文档,在失眠状态下运行一分钟多,最后崩溃显示404超时错误,我该怎么修复它?我来自巴西,函数区域默认为美国,但我不认为改变它会有帮助。
我试着让代码更简洁,但没有用。
这是云功能之一:

exports.getTodosPedidos = functions.https.onRequest(
   async (req: RequestType, res: ResponseType) => {
    try {
      const idProjeto = req.query.idProjeto;

      if (!idProjeto) {
        onFailureResponseStructure(res, "ID do Projeto não fornecido.", 404);
      }

      const projeto =
        idProjeto &&
        (await admin.firestore().collection("projetos").doc(idProjeto.toString()).get());

      if (projeto != "" && !projeto?.createTime) {
        onFailureResponseStructure(res, "O projeto fornecido não existe.", 405);
      }

      const snapshot = await admin.firestore().collection("pedidos").get();
      const result = snapshot.docs.map(doc => ({ ...doc.data(), id: doc.id }));

      let idsDosPedidos = result.map(elemento => elemento.id);

      let somaDosArquivos = idsDosPedidos.map(async pedidoId => {
        let bucketRef = await admin
          .storage()
          .bucket()
          .getFiles({
            prefix: `${idProjeto}/arquivos/${pedidoId}/`,
          });
        return bucketRef[0].map(elemento => parseInt(elemento.metadata.size));
      });
      const resultado = await Promise.all(somaDosArquivos);

      onSuccessResponseStructure(res, { resultado, idsDosPedidos });
    } catch (error: any) {
      onFailureResponseStructure(res, error.message, 500);
    }
  }
);

const onSuccessResponseStructure = (res: ResponseType, data: any) => {
  return res.status(200).json({
    status: "OK",
    data,
    messages: [],
    statusCode: "200",
  });
};

const onFailureResponseStructure = (
  res: ResponseType,
  messages: string,
  statusCode: number
) => {
  return res.status(statusCode).json({ status: "ERRO", data: [], messages, statusCode });
};
xqkwcwgp

xqkwcwgp1#

我对firebase不是很熟悉,但是其他的云服务允许你增加函数的超时时间,看起来firebase也可以... https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation
不过,对于长时间运行的任务,您可能需要调查其他服务。

相关问题