Sequelize 代码不在 AWS Lambda 内部执行 [英] Sequelize Code Not Executing Inside AWS Lambda

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

问题描述

我有在 Node.js 项目中运行良好的基于​​ Sequelize 的代码.我将该代码移到 AWS Lambda 处理程序中,并使用 node-lambda 模块对其进行测试.现在 Sequelize 代码似乎被跳过了.我不确定在 Lambda 完成之前是否没有处理承诺,或者我是否遗漏了其他东西.以下代码跳过期间"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.

昨天发现,如果您使用回调(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")
}

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

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