【问题标题】:Async.waterfall function exiting after saving Parse object保存 Parse 对象后退出 Async.waterfall 函数
【发布时间】:2016-08-14 21:08:06
【问题描述】:

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

这样做:

  1. 抓取图片(“下载”功能),

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

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

  4. 使用缩略图的 url 更新场地对象(这是一个 Parse 对象)('updateVenue' 函数)

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

**我省略了指定场地对象和场景对象的代码以使其更简单,因为我认为这不是问题。

我的问题是在 updateVenue 函数被记录为成功完成后,下一个日志是:Process exited before completed request.也就是 saveScene 函数永远不会被调用。

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

我也在使用 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");
            }


        );

【问题讨论】:

    标签: javascript node.js amazon-web-services parse-platform aws-lambda


    【解决方案1】:

    我相信您只需要在 updateVenue 和 saveScence 中调用 next。 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
       },...
    

    希望有帮助!

    【讨论】:

    • 表示return next(null, response);不是函数:/
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2015-10-08
      • 2015-01-19
      相关资源
      最近更新 更多