操作完成后,如何运行代码来访问Apache OpenWhisk操作的内存中状态?

问题描述 投票:0回答:1

我正在使用IBM Cloud Functions(托管的Apache OpenWhisk)进行测试,以在操作完成后在后台运行代码,但是调用setTimeout时提供的回调未在正确的时间运行,并且从未在除非我第二次调用该函数。它在那时运行(晚)。

详细信息:

我想到了两个用例:

  • 积累大量请求或在没有请求的情况下经过一定时间后,将大量请求的数据存储在内存中,然后将大对象放入云对象存储桶中。]
  • [针对每个容器管理API的数据库连接,以便可以在尚未关闭IBM Cloud Functions的未使用容器中关闭连接。
  • 我以为这行得通,因为我使用了其他平台,例如Google Cloud Run,我注意到在后台运行代码(使用setTimeout等),在请求完成后在Stackdriver中查看了该代码的日志。而且,甚至还有由AWS开发人员倡导者创建的整个库,该库在AWS Lambda(https://www.npmjs.com/package/serverless-mysql)的后台管理MySQL连接。

我测试了以下功能:

// from https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
function uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function main() {
    const runId = uuidv4().slice(31, 36);

    console.log(`function started (runId = ${runId})`);

    setTimeout(() => {
        console.log(`after 5s delay (runId = ${runId})`);
    }, 5000);

    return {
        msg: `ok (runId = ${runId})`,
    };
}

并且我使用命令ibmcloud fn action update logging-in-background src/index.js --kind nodejs:10部署了它。

我创建了一个LogDNA实例并将其设置为平台实例,以便我的功能日志可以进入它。这是我在用命令ibmcloud fn action invoke logging-in-background --blocking调用该函数3次后,在日志中看到的,每间隔10秒(编辑了CRN):

May 18 17:26:23 functions REDACTED 2020-05-18T21:26:23.956013Z    stdout: function started (runId = 9be7c)
May 18 17:26:23 functions REDACTED Activation record '3589870e8ce44cc089870e8ce4acc018' for entity 'logging-in-background'
May 18 17:26:34 functions REDACTED 2020-05-18T21:26:34.111745Z    stdout: after 5s delay (runId = 9be7c)
May 18 17:26:34 functions REDACTED 2020-05-18T21:26:34.115043Z    stdout: function started (runId = faba6)
May 18 17:26:34 functions REDACTED Activation record 'ac47c067177648f187c0671776b8f1c2' for entity 'logging-in-background'
May 18 17:26:44 functions REDACTED 2020-05-18T21:26:44.248470Z    stdout: after 5s delay (runId = faba6)
May 18 17:26:44 functions REDACTED 2020-05-18T21:26:44.253822Z    stdout: function started (runId = 0af34)
May 18 17:26:44 functions REDACTED Activation record 'bbad3eabb3d64ab1ad3eabb3d61ab1a7' for entity 'logging-in-background'

您可以看到我第一次调用该函数时,它仅记录了“函数已启动”消息。 5秒后,它没有记录“ 5s延迟”消息。但是随后,在第二次调用的开始(即第一次调用之后的10秒),它最终记录了与运行9be7c相关联的“ 5s延迟”消息。 setTimeout的回调似乎最早要到下一次调用该操作时才运行。

这是Apache OpenWhisk是按设计方式工作的方式,还是在操作完成后我没有正确地在后台运行代码?

我正在使用IBM Cloud Functions(托管的Apache OpenWhisk)进行测试,以在操作完成后在后台运行代码,但是调用setTimeout时提供的回调未在正确的位置运行...

node.js ibm-cloud settimeout openwhisk ibm-cloud-functions
1个回答
0
投票
setTimeout完成之前,您的函数正在返回。
© www.soinside.com 2019 - 2024. All rights reserved.