rea*_*not 6 amazon-web-services node.js aws-lambda
我有一个简单的AWS Lambda函数,可以对保存到S3存储桶的映像进行一些验证.我正在使用async.waterfall下载图像并对其进行处理,但在我输入第一个瀑布函数之前,我对从S3 PutObject触发器获取的事件数据做了一些基本验证,特别是大小信息(event.Records[0].s3.object.size
) .如果事件引用的图像大于我的MAX_SIZE,我context.fail(new Error("Validation error: the file is too big."))
用来结束执行.
这一切都运行正常,但我在日志中注意到,在记录错误后,该函数会在退出之前继续运行一段时间.例如,我的async.waterfall调用中的第一个函数被调用(即来自该函数的消息显示在日志中).我甚至尝试context.done(errorMessage)
在context.fail之后立即添加一个,然后执行它(即我传入的消息被记录)以及几行代码.
这是预期的行为吗?我在文档中找不到任何提及.该函数是否需要一点时间退出,或者我是否误解了BEFORE中处理函数中代码的同步性async.waterfall
?
下面是我的一些代码.打印到日志console.log
后显示的所有消息,context.fail
我不希望发生.
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
if (event.Records[0].s3.object.size > MAX_FILE_SIZE) {
var error = new Error("Validation error: the file is too big.")
context.fail(error);
}
console.log('Event validation complete.')
var bucket = event.Records[0].s3.bucket.name;
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
var fileName = key.split("/")[1];
var params = {
Bucket: bucket,
Key: key
};
console.log('Starting processing of the following file: ' + JSON.stringify(params, null, 2));
async.waterfall([
function download(callback) {
// Download the image from S3 into a buffer.
console.log("Downloading the image from S3...");
s3.getObject(params, function(err, data) {
if (err) {
callback(err);
} else {
callback(null, data);
}
});
},
...
], ...)
}
Run Code Online (Sandbox Code Playgroud)
emi*_*der 11
我相信aws lambda结构已经改变了一点,因为这个问题被问到了(和James的回答).
现在,处理程序还有第三个参数 'callback',可以调用它来停止执行脚本.根据您的调用方式,它会成功退出流程或出错.有关详细信息,请参阅这些文档.
另外,请查看context属性callbackWaitsForEmptyEventLoop.它默认为true,但您需要将其设置为false,以便在调用回调后,节点进程不会等待运行时循环清除,即您的异步调用将被丢弃.
归档时间: |
|
查看次数: |
8230 次 |
最近记录: |