node.js - AWS lambda 永远不会完成,但似乎也没有超时
问题描述
我正在尝试创建一个简单的应用程序。用户通过电子邮件将附件发送到特殊收件箱,AWS SES 获取电子邮件并将其存储在 S3 中,触发 lambda 函数,lambda 在 S3 中找到电子邮件,解析出附件(在本例中为 jpg),然后将其存储在不同的 S3 存储桶中。最后,应用程序在 Airtable 中创建一个新行,并将图像作为附件。
当我使用无服务器在本地调用此函数时,一切正常。带有图像的电子邮件已经存储在 S3 存储桶中,因此我创建了一个模拟,它将密钥显式传递给我的 lambda。但是,当我部署应用程序并发送测试电子邮件时,会发生以下情况:
- 电子邮件存储在 S3 存储桶“to-airtable-temp”中
- 调用 Lambda 函数
在找到电子邮件并将附件剥离并存储在单独的 S3 存储桶中之前,该功能将停止。没有错误消息或超时。它只是停止。Cloudwatch 日志如下所示:
START RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650 Version: $LATEST
2020-02-17T07:39:55.465Z 6a0364ae-0879-4ee1-9dcd-c8747de1a650 INFO {
s3SchemaVersion: '1.0',
configurationId: 'emailtoairtable-dev-parse-224c3963d3a3f9c35018ae93a9fffea4',
bucket: {
name: 'to-airtable-temp',
ownerIdentity: { principalId: 'AI5RGHFD5AFHE' },
arn: 'arn:aws:s3:::to-airtable-temp'
},
object: {
key: 'mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1',
size: 3804230,
eTag: 'c821fb0a2a9c3b060e20e7d177f8b972',
sequencer: '005E4A434810147365'
}
}
2020-02-17T07:39:55.465Z 6a0364ae-0879-4ee1-9dcd-c8747de1a650 INFO key mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1
2020-02-17T07:39:55.465Z 6a0364ae-0879-4ee1-9dcd-c8747de1a650 INFO Key pushed to list. mtvuuiokqj55l2a8b0qser7tn9dhfignoh9c1vg1
END RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650
REPORT RequestId: 6a0364ae-0879-4ee1-9dcd-c8747de1a650 Duration: 1113.17 ms Billed Duration: 1200 ms Memory Size: 1024 MB Max Memory Used: 114 MB Init Duration: 119.56 ms
这是我的 handler.js 文件:
'use strict';
module.exports.parse = async event => {
try {
const aws = require('aws-sdk');
const s3 = new aws.S3();
const simpleParser = require('mailparser').simpleParser;
const Airtable = require('airtable');
const dateformat = require('dateformat');
var base = new Airtable({ apiKey: process.env.airtableApiKey}).base(process.env.airtableBaseId);
var data = [];
var keys = [];
event["Records"].forEach(async record => {
console.log(record["s3"]);
console.log('key', record["s3"]["object"]["key"]);
keys.push(record["s3"]["object"]["key"]);
console.log('Key pushed to list. ', record["s3"]["object"]["key"]); // <-- this is the last line that I am sure processes because I see it in the CloudWatch logs.
var temp_data = await s3.getObject(
{
Bucket: 'to-airtable-temp',
Key: record["s3"]["object"]["key"]
}).promise();
console.log('temp_data', temp_data);
data.push(temp_data);
});
setTimeout( async function() {
// console.log('data', data[0].Body.toString());
let parsed = await simpleParser(data[0].Body.toString());
console.log(parsed);
// save the file to a public S3 bucket so it can be uploaded to airtable
parsed["attachments"].forEach(function(attachment) {
let now = new Date();
s3.upload({
Bucket: 'to-airtable-images',
Key: keys[0] + dateformat(now, "yyyy-mm-dd") + '.jpg',
Body: attachment.content,
ContentType: "image/jpeg"
},
function(error, data) {
if (error) {
throw error;
}
console.log('File uploaded successfully. ' +data.Location);
// Now upload to airtable
base('Input').create([
{
"fields": {"Attachments": [
{
"url": data.Location
}
]}
}
], function(err, records) {
if (err) {
console.error(err);
return;
}
records.forEach(function (record) {
console.log(record.getId());
});
});
});
});
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
data: JSON.stringify(data),
},
null,
2
),
};
}, 500); // I've tried increasing this time but it still hangs.
} catch (error) {
console.error(error);
}
};
解决方案
您不应该使用async/await
该forEach
功能。将 async/await 与 forEach 循环一起使用。相反,使用更现代的for of
语法:
for (let record of event["Records"]) {
// you can include await calls in this block
}
推荐阅读
- python - 我的 while 循环不会随机化我的列表,而是开始重复列表中的相同单词
- macos - 带有 -f 选项的 grep 根据模式文件的顺序返回不同的结果
- html - Mat-Dialog 在页面底部打开而不是弹出窗口?
- laravel - 为什么数据没有保存在 laravel 的 cookie 中?
- c++ - 使用变量在类中存储自定义函数
- android - Android Studio,缺少 Gradle 设置
- c# - 为什么我的 azure 函数有时无法读取静态配置变量?
- android - 意外行为改造 + 协程
- angular - 传单 GeoJSON 类型不与 GeoJSON 对象关联
- r - 按数据框中的因子列着色行 - R