Cloud Functions - 函数执行在 return 之前完成,状态为 'ok'

Cloud Functions - Function execution finished with status 'ok' before return

我有一个 (GCP) Cloud Function,旨在聚合每小时数据并写入 Cloud Bigtable,但它似乎返回消息:"Function execution took 100ms, finished with status: ok",在完成完整代码之前,后续行有时得到 运行,有时不会。如果有人对此有任何经验并可以提供建议,那就太好了,谢谢!

它在我 运行 运行脚本时在我的本地计算机上运行,​​仅在 Cloud Functions 中运行,我不确定是什么触发了代码终止。我尝试添加一个 try/catch 块,但它也没有抛出任何错误。代码的主要部分转载如下:

const Bigtable = require('@google-cloud/bigtable');
const bigtableOptions = { projectId: process.env.PROJECT_ID };
const bigtable = new Bigtable(bigtableOptions);
const cbt = bigtable.instance(process.env.BIGTABLE_INSTANCE);
const async = require("async");
const moment = require("moment");
require("moment-round");
const bigtableFetchRawDataForDmac = require("./fetchData").bigtableFetchRawDataForDmac;

exports.patchJob = (event, context) => {
    const pubsubMsg = Buffer.from(event.data, 'base64').toString();
    const jsonMsg = tryParseJSON(pubsubMsg);    // msg in format { time: "2018-12-24T02:00:00.000Z", dmac: ["abc", "def", "ghi] }
    if(!jsonMsg) return;
    else {
        if(!jsonMsg.time) { 
            console.log("Time not provided");
            // res.status(400).json({ err: 'TimeNotProvided', msg: `Time parameter is not provided` });
            return;
        }
        let date_range = {};
        date_range.lower = moment(jsonMsg.time).toISOString();
        date_range.upper = moment(jsonMsg.time).add(1,'hours').subtract(1,"milliseconds").toISOString();

        let queryData = [];
        let data = {};
        for(let i=0; i<jsonMsg.dmac.length; i++){
            data[jsonMsg.dmac[i]]=[];
            queryData.push(bigtableFetchRawDataForDmac(cbt, jsonMsg.dmac[i], date_range.lower, date_range.upper, data[jsonMsg.dmac[i]]));
        }
        async.parallel(queryData, function(err, result){
            console.log("cookie trail...");
            return;
        }   
    }
}

对于 bigtableFetchRawDataForDmac,它位于不同的文件夹中:

function bigtableFetchRawDataForDmac(cbt, dmac, start, end, data) {
    return async function(cb){
        const table = cbt.table(process.env.BT_DATA_TABLE);
        try { var bigtable = await fetchFromBigtable(table, process.env.BT_DATA_TABLE, dmac, start, end, data, ['push', 'mode', 'val']); }
        catch (err) { console.log("bigtableFetchRawDataForDmac failed: ", err); cb(err); }
    }
}

一个pubsub Cloud Function receives an event and callback parameter. You're supposed to call the callback method to terminate the function when all the work is complete, as is the case for all types of background Cloud Functions.

您已调用回调 context。而且您根本没有使用它来终止该功能。您也可以 return 承诺在所有工作完成时解决,但您也没有这样做。

您将不得不想出一种只有在所有异步工作完成后才能正确终止您的功能的方法,否则它不会按您期望的方式工作。

在 Nodejs 8(测试版)运行时中,应提供 3 个参数(数据、上下文、回调),而不是 Cloud Functions 控制台内联编辑器中默认模板中提供的 2 个。 (文档参考: https://cloud.google.com/functions/docs/writing/background#functions_background_parameters-node8)。

代码应该是这样的:

exports.patchJob = (event, context, callback) => {
    doSomething();
    callback(); // To terminate Cloud Functions
}

感谢@Doug 的提示!