javascript - Error: function crashed out of request scope Function invocation was interrupted
问题描述
I'm writing a Cloud Function that reads several lines from a .csv file and sends one HTTP request for each line, but I'm getting the error above.
This error has been asked before (here, here and here to list a few), but in all of these cases, the issue was that the function didn't return a promise that would resolve only after every asynchronous operation.
From my understanding of how async
works, my code doesn't seem to have this issue. Here's the gist of it, removing some specific details:
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}`);
}
};
The above code works just fine for a couple of lines (10), but the error begins showing up when trying with bigger numbers (1000). The "Done." message also didn't appear in the logs, so it doesn't seem that the function is reaching the end.
Any clues?
解决方案
我的项目也有同样的错误。我通过将运行时从 node8 降级到 node6 来解决它
推荐阅读
- python-3.x - 如何使用 selenium 和 python 查找不包含某个类名的元素
- mysql - MySQL 仅在 Workbench 连接到端口 3306 时运行
- composer-php - 为什么推荐的带有 Composer 的 Drupal 安装方法使用 dev 作为稳定性标志?
- php - 在 null zendframework 3 上调用成员函数 hello()
- vue.js - Vue - 单个文件组件中的翻译
- ios - 我可以控制 concurrentPerform 的并行性吗?
- javascript - javascript 从字符串中提取主题标签
- ghost-blog - 幽灵到媒体的整合
- java - 如何在 Javafx 中删除视频开头的主窗口闪烁?
- python - 在键级python下面的字典中添加新元素