在AWS lambda中使用Promise.all()

时间:2017-01-29 23:09:00

标签: amazon-web-services lambda promise aws-lambda bluebird

我不明白为什么我的promise.all()在开发工作中没有在Lambda中执行。

在AWS Lambda上,它已经超时,因为Promise.all()没有完成,我在SQS队列中没有收到新消息。

但是,console.log(promises)已退回:2017-01-29T22:55:46.191Z 0e82eeaf-e676-11e6-b69d-73a6bbd86272 [ Promise { <pending> } ]

var url = require('url');
var AWS = require('aws-sdk');
// var Promise = require("bluebird");

exports.handler = function (event, context, callback) {

    AWS.config = {
        region: 'us-east-1',
        apiVersions : {
            sqs: '2012-11-05'
        }
    };

    if (typeof Promise === 'undefined') {
        AWS.config.setPromisesDependency(require('bluebird'));
    }

    var sqs = new AWS.SQS(); // système de queue
    var date = new Date();

    var knex = require('knex')({
        client: 'mysql',
        connection: {
            host : event.database.host,
            user : event.database.user,
            password : event.database.pwd,
            database : event.database.name
        }
    });

    var sqsQueueUrl = event.sqsQueueUrl;
    var keywords = event.keywords;

    knex.select('keywords').from('keyword')
        .timeout(1000)
        .then(function(results) 
        {
            var keywordsInDbArray = returnArrayWithOnlyKeywords(results);

            var keywordsWillBeStored = [];

            for (var i in keywords) 
            {
                var isInDb = keywordsInDbArray.indexOf(keywords[i]);

                if(isInDb == -1)
                    keywordsWillBeStored.push({
                        keywords: keywords[i],
                        'created_at': date, 
                        'updated_at': date
                    });
            }
            if(keywordsWillBeStored.length > 0)
            {
                knex.batchInsert('keyword', keywordsWillBeStored, keywordsWillBeStored.length)
                    .then(function(firstId) 
                    { 
                        var newKeywordsStored = dataStoredInDb(firstId, keywordsWillBeStored);
                        console.log(newKeywordsStored);

                        sendKeywordInqueue(newKeywordsStored);
                    })
                    .catch(function(error) 
                    { 
                        console.error(error);
                        context.fail(error);
                        knex.destroy();
                    });
            }
            else
            {
                context.succeed('Aucun nouveau keyword a enregistré');
            }
        })
        .catch(function(error) 
        {
            console.error(error);
            context.fail(error);
            knex.destroy();
        });

    function sendKeywordInqueue(array)
    {
        var promises = [];
        for( var i in array)
        {
            var promise = sqs.sendMessage({
                MessageBody: JSON.stringify(array[i]),
                QueueUrl: sqsQueueUrl
            }).promise().catch(function(error) 
            { 
                console.error(error);
            });
            promises.push(promise);
        }

        console.log(promises);

        Promise.all(promises)
            .catch(function(error) 
            { 
                console.error(error);
            })
            .then(function() 
            {
                callback(null, "DONE");
                context.succeed('Il y a eu '+array.length+' nouveau keywords enregistré et qui va être scrapé');
                knex.destroy();
            });
    }

    function dataStoredInDb(firstIdSavedInDb, arrayWithKeywords) 
    {
        id = firstIdSavedInDb -1;
        return arrayWithKeywords.map(function (obj, i) {
            var rObj = {};
            rObj.id = id + i + 1;
            rObj.keywords = obj.keywords;
            return rObj;
        })
    }

    function returnArrayWithOnlyKeywords(resultDb)
    {
        var dataKeywords = [];

        for(var key in resultDb)
        {
            dataKeywords.push(resultDb[key].keywords);
        }

        return dataKeywords;
    }
};

4 个答案:

答案 0 :(得分:3)

如果有人回到这个问题,则需要等待Promise.all,即:

const res = await Promise.all([
    Do_Stuff(),
    Do_more_stuff()
    ])
    .then(() => callback(null,{
        'statusCode': 200,
        'headers' : {'Access-Control-Allow-Origin' : '*'},
        'body' : All Good)
      })
    .catch(() => callback('Something went wrong'))
  )

答案 1 :(得分:1)

我遇到了同样的问题,最后引入了一个polyfill,直到这个问题得到解决:

function promiseAll(promises) {
  return new Promise((resolve, reject) => {
    const result = [];
    let count = promises.length;
    const checkDone = () => {
      if (--count === 0) resolve(result);
    };
    promises.forEach((promise, i) => {
      promise
        .then(x => {
          result[i] = x;
        }, reject)
        .then(checkDone);
    });
  });
}

[来源:https://codereview.stackexchange.com/questions/134224/pseudo-promise-all-polyfill]

答案 2 :(得分:0)

我们必须看到你的实际lambda处理程序,但你可能在异步操作完成之前退出lambda。 你试过回调参数吗?

exports.handler = function(event, context, callback) {
    handleWhatever(context, callback);
};

完成后,您可以致电

callback(null, "DONE");

答案 3 :(得分:0)

你可以试试这个:

var url = require('url');
var AWS = require('aws-sdk');
var knex = require('knex');
AWS.config = {
  region: 'us-east-1',
  apiVersions : {
    sqs: '2012-11-05'
  }
};
var sqs = new AWS.SQS(); // système de queue

exports.handler = function (event, context, callback) {
  var db = knex({
    client: 'mysql',
    connection: {
      host : event.database.host,
      user : event.database.user,
      password : event.database.pwd,
      database : event.database.name
    }
  });

  function destroyDb (out) {
    return db.destroy().catch(function (err) {
      console.error('Unable to close connection to database', err)
    }).return(out)
  }

  return performKeywordQuery(db, event)
  .then(destroyDb)
  .then(context.succeed)
  .catch(function (error) {
    return destroyDb(error).then(context.fail)
  })
};

function performKeywordQuery (db, event) {
  var sqsQueueUrl = event.sqsQueueUrl;
  var keywords = event.keywords;

  return db.select('keywords').from('keyword')
  .timeout(1000)
  .then(function(results) 
  {
    var date = new Date();
    var keywordsInDbArray = returnArrayWithOnlyKeywords(results);

    var keywordsWillBeStored = [];

    for (var i in keywords) 
    {
      var isInDb = keywordsInDbArray.indexOf(keywords[i]);

      if(isInDb == -1)
      keywordsWillBeStored.push({
        keywords: keywords[i],
        created_at: date, 
        updated_at: date
      });
    }
    if (keywordsWillBeStored.length > 0)
    {
      return knex.batchInsert('keyword', keywordsWillBeStored, keywordsWillBeStored.length)
      .then(function(firstId) 
      { 
        var newKeywordsStored = dataStoredInDb(firstId, keywordsWillBeStored);
        console.log(newKeywordsStored);

        return Promise.all(newKeywordsStored.map(function (body) {
          return sqs.sendMessage({
            MessageBody: JSON.stringify(body),
            QueueUrl: sqsQueueUrl
          }).promise()
        }));
      })
      .then(function (array) {
        return 'Il y a eu '+array.length+' nouveau keywords enregistré et qui va être scrapé';
      })
    }
    else
    {
      return 'Aucun nouveau keyword a enregistré';
    }
  })
}

function dataStoredInDb(firstIdSavedInDb, arrayWithKeywords) 
{
  var id = firstIdSavedInDb -1;
  return arrayWithKeywords.map(function (obj, i) {
    var rObj = {};
    rObj.id = id + i + 1;
    rObj.keywords = obj.keywords;
    return rObj;
  })
}

function returnArrayWithOnlyKeywords(resultDb)
{
  var dataKeywords = [];

  for(var key in resultDb)
  {
    dataKeywords.push(resultDb[key].keywords);
  }

  return dataKeywords;
}