node.js - 在 Lambda 上查询 mysql 时任务超时

标签 node.js amazon-web-services aws-lambda amazon-lex

我一直在使用 amazon Lex 和 Lambda 以及 Nodejs。我的问题是,当我尝试 mysql 查询时,lambda 超时并且没有向 Lex 返回任何结果,

需要这方面的帮助。 这是我的代码

'use strict'; 
const lexResponse = require("../helper/responseBuilder");
const db = require('../config/db')

function dialog (intentRequest, callback) {

    const source = intentRequest.invocationSource;
    const userId = intentRequest.userId;
    const sessionAttributes = intentRequest.sessionAttributes || {};

    if (source === 'DialogCodeHook') {
        if (!companyRules) {
            getList(1, (results) => {
                console.log(results);
                callback(lexResponse.elicitSlot(
                    sessionAttributes,
                    intentRequest.currentIntent.name,
                    intentRequest.currentIntent.slots,
                    "Info",
                    { contentType: 'PlainText', content: results.name }
                ));
            });
            return;
        }
    }

    callback(lexResponse.close(intentRequest.sessionAttributes, 'Fulfilled',
    { contentType: 'PlainText', content: `Thanks` }));
}

function getList(data, callback)
{
    db.connection.getConnection( (err, connection) => {
        let statement = 'select `name` from tablename where id = ?';
        connection.query(statement, [data], (error, results, fields) => {
            if (error) throw error;
            connection.release();
            callback(results[0]);
        });
    });
}

function dispatch(intentRequest, callback) {
    console.log(`dispatch userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);

    const intentName = intentRequest.currentIntent.name;

    // Dispatch to your skill's intent handlers
    if (intentName === 'myIntent') {
        return dialog(intentRequest, callback);
    }
    throw new Error(`Intent with name ${intentName} not supported`);
}

exports.handler = (event, context, callback) => {
    try {
        process.env.TZ = 'America/New_York';
        console.log(`event.bot.name=${event.bot.name}`);
        dispatch(event, (response) => callback(null, response));
    } catch (err) {
        callback(err);
    }
};

这是我从 cloudwatch 获得的日志

11:34:14
2017-05-29T11:34:14.902Z    be549f47-4462-11e7-8d4a-e149b3395e95 event.bot.name=MyBot

11:34:14
2017-05-29T11:34:14.902Z    be549f47-4462-11e7-8d4a-e149b3395e95    dispatch userId=iv3an8pf9wmwtechg9gztkzlmtvwuanr, intentName=MyIntent

11:34:16
2017-05-29T11:34:16.146Z    be549f47-4462-11e7-8d4a-e149b3395e95     RowDataPacket { name: 'my name' }

11:34:17 END RequestId: be549f47-4462-11e7-8d4a-e149b3395e95

11:34:17
REPORT RequestId: be549f47-4462-11e7-8d4a-e149b3395e95  Duration: 3002.10 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 25 MB

11:34:17
2017-05-29T11:34:17.904Z be549f47-4462-11e7-8d4a-e149b3395e95 Task timed out after 3.00 seconds

我还增加了超时配置,但我认为这不是问题。

谢谢。

最佳答案

将以下内容添加为处理函数中的第一行:

context.callbackWaitsForEmptyEventLoop = false;

关于node.js - 在 Lambda 上查询 mysql 时任务超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244399/

相关文章:

amazon-web-services - CloudFormation API Gateway Lambda 集成请求未获得正确的权限?

javascript - 找不到 schema.graphql 文件( Node graphql-yoga)

javascript - Nodejs : Good practice to just use the index. js 导出?

javascript - 如何使用http 2实现cookie jar

python - 是否有用于向 AWS DynamoDB 提交批量获取请求的 Python API?

aws-lambda - 如何将外部层与无服务器框架一起使用?

node.js - OSX 上的 Enide Studio 2015 - 尝试调试

amazon-web-services - 将多个 IAM 角色与 CloudFormation 中的 AWS::IAM::InstanceProfile 关联

amazon-web-services - [fog][WARNING] 无法获取凭据:没有到主机的路由 - connect(2) -(Errno::EHOSTUNREACH)

amazon-web-services - 如何检测 Post Confirmation Lambda Trigger 中的密码更改?