在 AWS Lambda 中未执行的 Sequelize 代码 [英] Sequelize Code Not Executing Inside AWS Lambda

查看:35
本文介绍了在 AWS Lambda 中未执行的 Sequelize 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got Sequelize-based code that runs fine in a Node.js project. I moved that code into an AWS Lambda handler and am testing it with the node-lambda module. Now the Sequelize code seems to be skipped. I'm not sure if the promise isn't being handled before the Lambda finishes, or if I'm missing something else. The following code skips the "during" console.log as shown in the output below.

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.

Any ideas on where I'm going wrong? I'm using Node v5.9.0.

推荐答案

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

I just started playing with apigateway / lambda and sequelize, but as far as I know of node and sequelize, the callback should be inside the "then" block.

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

Yesterday found that if you're using callback(null, successData), the performance was really poor (>11 secs on a Select top 1). Had to change the flag context.callbackWaitsForEmptyEventLoop = false and now the api call takes 24ms.

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

这篇关于在 AWS Lambda 中未执行的 Sequelize 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆