在 Lambda 上查询 mysql 时任务超时

Task time out when query to mysql on Lambda

我一直在将 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;