Async.waterfall 函数在保存解析对象后退出

Async.waterfall function exiting after saving Parse object

本文关键字:对象 退出 保存 waterfall 函数 Async      更新时间:2023-09-26

我的aws-lambda函数中有一个异步函数,效果很好。

它这样做:

  1. 抓取图像("下载"功能),

  2. 裁剪并将其调整为缩略图("转换"功能),

  3. 将该缩略图上传到新存储桶("上传"功能),

  4. 使用指向该缩略图的 url 更新场地对象(这是一个解析对象)("updateVenue"函数)

  5. 最后,它创建一个新的场景对象(这也是一个解析对象)("saveScene"函数)。

**我省略了指定 venueObj 和 sceneObj 的代码,以使其更简单,因为我认为这不是问题所在。

我的问题是,在 updateVenue 函数记录为成功完成后,下一个日志是:进程在完成请求之前退出。 又名 saveScene 函数永远不会被调用。

即使我翻转 updateVenue 和 saveScene 函数的顺序,该过程也会在第一个 Parse 函数 - saveScene 完成后退出。 因此,我认为错误在于我称呼这些的方式。

我也在使用context.success(),也许这与它有关?

// 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");
            }

        );

我相信你只需要在updateVenue中调用next并保存Scence。 async.waterfall 将回调传递给系列中的每个函数,您当前正在使用 next 。 如果需要将数据传递给下一个 fn,请将其作为第二个参数传递给回调。

以下是如何在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
   },...

希望对您有所帮助!

我找到了解决这个问题的方法。不幸的是,我无法将函数分开,但是,我能够将第二个函数嵌入到第一个函数的完成块中:

// 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");
            }

        );