admin.messaging()。sendToTopic超出每日CPU分配限制

时间:2019-01-05 12:03:22

标签: javascript google-cloud-platform firebase-cloud-messaging google-cloud-functions firebase-admin

我有以下Firebase Functions代码:

globalPricingRequest.forEach(data -> {
      if (data.getFeePerTransact() != null && data.getFeePerTransact().intValue() < 0) {
        // you can only throw unchecked exception in lambda function
        throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");
      }
      data.getEventTypePricingList().forEach(event-> {
        if (event.getFeePerRevenue() != null && event.getFeePerRevenue().intValue() < 0) {
          // you can only throw unchecked exception in lambda function
          throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");
        }// this if statemnt in your code is twice
      });
    });

该函数会正确执行,并在控制台中给出“成功发送消息:”响应。不幸的是,每个函数调用都消耗大量的“函数调用中的CPU分配”。仅仅打了几次电话,我的每日限额就超出了:“错误:配额超出了(配额组'CPUMilliSecondsDailyNonbillable”超出了配额” enter image description here

enter image description here

enter image description here 可能是什么原因呢?如何改善我的代码?

1 个答案:

答案 0 :(得分:0)

感谢Frank van Puffelen comment,我终于获得了OK200。看来我的代码缺少发送响应状态。这是有效的代码:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.https.onRequest((request, response) => {
    const payload = {
        data: {
            execute_noise_measure: "true"
        }
    };

    const options = {
        priority: "high"
    };

    admin.messaging().sendToTopic("all", payload).then(() => {
        response.status(200).send("ok");

        return true;
    }).catch((err) => {
        response.status(500).send(err);

        return true;
    });
});