【问题标题】:Sequelize Code Not Executing Inside AWS LambdaSequelize 代码不在 AWS Lambda 内部执行
【发布时间】:2016-12-31 23:18:11
【问题描述】:

我有在 Node.js 项目中运行良好的基于​​ Sequelize 的代码。我将该代码移到 AWS Lambda 处理程序中,并使用 node-lambda 模块对其进行测试。现在 Sequelize 代码似乎被跳过了。我不确定在 Lambda 完成之前是否没有处理承诺,或者我是否遗漏了其他东西。以下代码跳过“期间”console.log,如下面的输出所示。

var models  = require('./models');

exports.handler = function( event, context, callback ) {
    console.log("Before");

    var body = JSON.parse(event.body);

    // Find the device ID from the devices table
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) {
        console.log("During");

        // Make sure that we got a device back
        if (device === null) {
            console.log("Device not found - could not insert data");
            return;
        }
        else {
            console.log("Device found, inserting data - " + body.device_uuid);

            //Insert the data for this device
            models.Data.create({
               device_id: device.id,              
               data: body.data
            });
        }
    });

    console.log("After");

    callback(null, "{\"status\": \"success\"}");
}

产量...

Before
After
Success:
"{\"status\": \"success\"}"

关于我哪里出错了有什么想法吗?我正在使用 Node v5.9.0。

【问题讨论】:

  • 我发现如果我删除回调引用,Sequelize 代码运行良好。
  • 我有同样的问题,但下面的答案都没有,删除回调引用对我有用。

标签: sequelize.js


【解决方案1】:

我刚开始玩 apigateway / lambda 和 sequelize,但据我所知 node 和 sequelize,回调应该在“then”块内。

昨天发现,如果您使用回调(null,successData),性能真的很差(在 Select top 1 上 >11 秒)。必须更改标志 context.callbackWaitsForEmptyEventLoop = false,现在 api 调用需要 24 毫秒。

    //Important to improve performance! 
    context.callbackWaitsForEmptyEventLoop = false

    // Find the device ID from the devices table
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) {
        console.log("During");

        // Make sure that we got a device back
        if (device === null) {
            callback(new Error("Device not found - could not insert data"))
        }
        else {
            console.log("Device found, inserting data - " + body.device_uuid);

            //Insert the data for this device
            models.Data.create({
               device_id: device.id,              
               data: body.data
            })
            .then(function(insertedData){
               callback(null, insertedData.toJSON())     
            })
            .catch(function(error){ 
                callback( new Error("Error creating")
            })
        }
    })    
    console.log("After")
}

【讨论】:

    【解决方案2】:

    我花了将近一天的时间来找出问题所在,因为我遇到了完全相同的问题。

    原来我需要等待那些使用 sequelize 与数据库交互的函数,否则 Lambda 将退出。

    module.exports.v2BlacklistEmails = async (event, context) => {    
    
      try {
        const message = JSON.parse(event.Records[0].Sns.Message)
    
        switch (message.notificationType) {
          case "Bounce":
            handleBounce(message);
            break;
        }
      } catch (err) {
        console.log("Error processing event: " + err.stack)
      }
    }
    
    function handleBounce(message) {
      const messageId = message.mail.messageId
      const addresses = message.bounce.bouncedRecipients.map(function (recipient) {
        return recipient.emailAddress
      })
      const bounceType = message.bounce.bounceType
    
      console.log("Message " + messageId + " bounced when sending to " + addresses.join(", ") + ". Bounce type: " + bounceType)
      for (var i = 0; i < addresses.length; i++) {
        if (addresses[i] !== process.env.BOUNCE_EMAIL_AU && addresses[i] !== process.env.BOUNCE_EMAIL_NZ) {
          utils.blacklistEmailsIntoDB(decryptedUser, decryptedPassword, addresses[i])
        } else {
          console.log("Mailbox " + addresses[i] + " maybe full, please check.")
        }
      }
    }
    

    所以我必须等待:

    handleBounce(message);
    

    和

    utils.blacklistEmailsIntoDB(decryptedUser, decryptedPassword, addresses[i])
    

    当然,您还需要将异步添加到这些函数中。

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 2023-02-05
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2016-01-26
      相关资源
      最近更新 更多