2

我一直在使用带有 nodejs 的 amazon Lex 和 Lambda。我的问题是,当我尝试 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

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

谢谢。

4

1 回答 1

3

在您的处理程序函数中添加以下内容作为第一行:

context.callbackWaitsForEmptyEventLoop = false;
于 2017-05-29T16:21:54.887 回答