在Lambda上查询mysql时任务超时

时间:2017-05-29 14:03:06

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

我一直在使用带有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

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

感谢。

1 个答案:

答案 0 :(得分:3)

在处理函数中添加以下作为第一行:

context.callbackWaitsForEmptyEventLoop = false;