0

我的aws-lambda函數內部有一個異步函數,效果很好。保存Parse對象後退出Async.waterfall函數

它這樣做是:

  1. 抓取的圖像( '下載' 功能),

  2. 作物,它會調整到縮略圖( '轉換' 功能),

  3. 上傳該縮略圖到一個新的桶('上傳'功能),

  4. 更新場地對象(這是一個Parse對象)與URL縮略圖('updateVenue'功能)

  5. 最後,它創建一個新的場景對象(這也是一個解析對象)('saveScene'函數)。

**我遺漏了指定場景對象和場景對象的代碼,使其更簡單,因爲我不相信這是問題所在。

我的問題是,在成功完成updateVenue函數記錄之後,下一個日誌是:進程在完成請求之前退出。也就是說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"); 
      } 


     ); 

回答

0

我相信,你只需要內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 
    },... 

希望幫助!

+0

它說,返回下一個(空,響應);不是一個功能:/ – ian

0

我發現如何解決這個問題。遺憾的是我沒能保持獨立的功能,但是,我能第二個以嵌入到第一個完成塊:

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


     );