[英]Async.waterfall function exiting after saving Parse object
I have an async function inside my aws-lambda function that works great. 我的aws-lambda函数内部有一个异步函数,效果很好。
It does this: 它这样做:
grabs an image ('download' function), 抓取图像(“下载”功能),
crops and resizes it to a thumbnail ('transform' function), 裁剪并调整其大小为缩略图(“转换”功能),
uploads that thumbnail to a new bucket ('upload' function), 将该缩略图上传到新的存储桶(“上传”功能),
Updates a venue object (this is a Parse object) with the url to that thumbnail ('updateVenue' function) 使用该缩略图的网址更新场所对象(这是一个Parse对象)(“ updateVenue”功能)
Lastly, it creates a new scene object (this is also a parse object) ('saveScene' function). 最后,它创建一个新的场景对象(这也是一个解析对象)(“ saveScene”功能)。
**I left out the code specifying what the venueObj and sceneObj is to make it simpler as i do not believe that is the problem. **由于我不认为这是问题所在,因此我省略了代码,指定了consoleObj和sceneObj使其变得更简单。
My problem is that after the updateVenue function has been logged as successfully completed, the next log is: Process exited before completing request. 我的问题是,在将updateVenue函数记录为成功完成之后,下一个日志是: 在完成请求之前退出了进程。 Aka the saveScene function never gets called. 又称saveScene函数永远不会被调用。
Even when I flip the order of the updateVenue and saveScene functions, the process is exited after the first Parse function - saveScene is completed. 即使当我翻转updateVenue和saveScene函数的顺序时,在第一个Parse函数-saveScene完成之后,也会退出该过程。 Therefore, I believe the error is in the way I am calling these. 因此,我认为错误在于我所说的这些方式。
Also I am using context.succeed() ,maybe that has something to do with it? 另外我正在使用context.succeed() ,也许与它有关?
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
WIDTH = size.width;
HEIGHT = size.height;
if (WIDTH > HEIGHT) {
var side = HEIGHT;
}
else{
var side = WIDTH;
}
var scalingFactor = Math.min(
MAX_WIDTH / side,
MAX_HEIGHT / side
);
var width = scalingFactor * side;
var height = scalingFactor * side;
// Transform the image buffer in memory.
this.gravity("Center").crop(side, side).resize(width, height)
.toBuffer(imageType, function(err, buffer) {
if (err) {
next(err);
console.log(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
},
function updateVenue(next) {
venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
context.fail();
next
}
}); // end of venueObj.save
},
function saveScene(next) {
sceneObj.save(null, {
success: function(response){
console.log('Saved sceneObj succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to create new sceneObj, with error code: ' + error.description);
context.fail();
next
}
}); // end of sceneObj.save
}
], function (err) {
if (err) {
console.error(
'Unable to resize ' + srcBucket + '/' + srcKey +
' and upload to ' + dstBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully resized ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey
);
}
callback(null, "message");
}
);
I believe you just have to invoke next
within updateVenue and saveScence. 我相信您只需要在updateVenue和saveScence中调用next
即可。 async.waterfall passes a callback to each function in the series, where you are currently using next
. async.waterfall将回调传递给系列中的每个函数,您当前在其中使用next
。 If you need to pass data to the next fn, you pass it as the second argument to the callback. 如果需要将数据传递给下一个fn,请将其作为第二个参数传递给回调。
Here's an example of how this would apply in updateVenue
: 这是在updateVenue
中如何应用的updateVenue
:
function updateVenue(next) {
return venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
return next(null, response);
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
return next(error);
}
}); // end of venueObj.save
},...
Hope that helps! 希望有帮助!
I found out how to solve this. 我发现了解决方法。 Unfortunately I wasn't able to keep the functions separate, however, I was able to imbed the second one into the completion block of the first one: 不幸的是,我无法将功能分开,但是,我能够将第二个功能嵌入第一个功能的完成模块中:
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
WIDTH = size.width;
HEIGHT = size.height;
if (WIDTH > HEIGHT) {
var side = HEIGHT;
}
else{
var side = WIDTH;
}
var scalingFactor = Math.min(
MAX_WIDTH / side,
MAX_HEIGHT / side
);
var width = scalingFactor * side;
var height = scalingFactor * side;
// Transform the image buffer in memory.
this.gravity("Center").crop(side, side).resize(width, height)
.toBuffer(imageType, function(err, buffer) {
if (err) {
next(err);
console.log(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
},
function updateVenue(next) {
venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
sceneObj.save(null, {
success: function(response){
console.log('Saved sceneObj succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to create new sceneObj, with error code: ' + error.description);
context.fail();
next
}
}); // end of sceneObj.save
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
context.fail();
next
}
}); // end of venueObj.save
}
], function (err) {
if (err) {
console.error(
'Unable to resize ' + srcBucket + '/' + srcKey +
' and upload to ' + dstBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully resized ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey
);
}
callback(null, "message");
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.