【问题标题】:Error: function crashed out of request scope Function invocation was interrupted错误:函数在请求范围外崩溃 函数调用被中断
【发布时间】:2019-01-09 22:59:23
【问题描述】:

我正在编写一个云函数,它从 .csv 文件中读取多行并为每一行发送一个 HTTP 请求,但出现上述错误。

这个错误之前已经被问过(hereherehere 列出了一些),但在所有这些情况下,问题是函数没有返回只能解决的承诺在每次异步操作之后。

根据我对async 工作原理的理解,我的代码似乎没有这个问题。以下是它的要点,删除了一些具体细节:

const request = require('request-promise-native');
const storage = require('@google-cloud/storage')();

exports.sendData = async (data, context) => {
    const [file] = await storage.bucket(data.bucket).file(data.name).download();
    const [header, ...table] = file.toString('utf8').split('\n').map(row => row.split(','));
    const rows = table.map(
        row => header.reduce((obj, col, i) => {
            obj[col] = row[i];
            return obj;
        }, {})
    );

    const paramList = rows.map(({ csv_field1, csv_field2 }) => ({
        req_field1: csv_field1,
        req_field2: csv_field2,
        // etc.
    }));

    let errors = 0;
    console.log(`Sending ${paramList.length} rows...`);
    await Promise.all(paramList.map(async (param, i) => {
        try {
            await request.get({ url: "", qs: param }); // Endpoint removed
            if ((i + 1) % 10000 == 0) {
                console.log(`${i + 1} rows ${paramList.length} sent.`);
            }
        } catch (e) {
            console.log(`Request failed for row ${i + 1} .\n\n${e}`);
            errors++;
        }
    }));
    console.log(`Done.`);
    if (errors > 0) {
        console.log(`Total errors: ${errors}`);
    }
};

上面的代码在几行 (10) 上工作得很好,但是当尝试使用更大的数字 (1000) 时错误开始出现。 “完成”。消息也没有出现在日志中,所以函数似乎没有结束。

有什么线索吗?

【问题讨论】:

  • 重写了代码以使用 Promise 并尝试了 Node.js 6 运行时,它工作正常。不确定代码是否有问题,或者是否是 Node.js 8 运行时的问题。
  • 我有完全相同的症状,但在一个相当大的项目中,我真的不想为 Node 6 重写......它工作正常,直到传出 http 请求的数量达到几百,然后开始崩溃并出现这个有用的错误。
  • 听起来和我有同样的问题......有什么想法吗?

标签: javascript node.js google-cloud-functions


【解决方案1】:

我在我的项目中遇到了同样的错误。 我通过将运行时从 node8 降级到 node6 来解决它

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多