AWS Lambda函数从不调用回调 [英] AWS Lambda function never calls the callback
问题描述
我创建了一个节点lambda函数,该函数对Aurora数据库进行了简单的调用.当我在控制台中测试该函数时,查询返回,我可以在日志中看到结果,但是似乎从未调用过回调,因此我的lambda函数超时.我不知道是什么问题.希望这里有人可以指出我的问题.
I've created a node lambda function that does a simple call to an Aurora database. When I test the function in the console, the query returns, I can see the results in the log, but the callback never seems to get called and so my lambda function times out. I can't figure out what the problem is. Hopefully someone here can point me to the problem.
var mysql = require("mysql");
module.exports.handler = function(event, context, cb) {
console.log('start\n');
var con = mysql.createConnection({
...
});
console.log('call data\n');
con.query('SELECT * FROM Tags', function(err, rows) {
console.log('Data received from Db:\n');
console.log(rows);
console.log('calling callback');
cb(null, 'Success');
console.log('callback called');
});
console.log('data called\n');
};
生成的Cloudwatch日志如下...
The resulting Cloudwatch log is as follows...
2016-07-25T14:20:05.343Z daf5cd6b-5272-11e6-9036-e73ad17006df start
2016-07-25T14:20:05.398Z daf5cd6b-5272-11e6-9036-e73ad17006df call data
2016-07-25T14:20:05.405Z daf5cd6b-5272-11e6-9036-e73ad17006df data called
2016-07-25T14:20:05.440Z daf5cd6b-5272-11e6-9036-e73ad17006df Data received from Db:
2016-07-25T14:20:05.440Z daf5cd6b-5272-11e6-9036-e73ad17006df [
RowDataPacket {
id: 1,
externalId:
'a87ead34de7e',
orgId: 1,
name: 'lacinia sapien',
createdDate: 1448598369,
modifiedDate: 0
},
...,
RowDataPacket {
id: 50,
externalId: '9ebaaab372e3',
orgId: 1,
name: 'et commodo',
createdDate: 1451551837,
modifiedDate: 0
}
]
2016-07-25T14:20:05.483Z daf5cd6b-5272-11e6-9036-e73ad17006df calling callback
2016-07-25T14:20:05.483Z daf5cd6b-5272-11e6-9036-e73ad17006df callback called
END RequestId: daf5cd6b-5272-11e6-9036-e73ad17006df
REPORT RequestId: daf5cd6b-5272-11e6-9036-e73ad17006df Duration: 300000.12 ms Billed Duration: 300000 ms Memory Size: 1024 MB Max Memory Used: 52 MB
2016-07-25T14:25:05.341Z daf5cd6b-5272-11e6-9036-e73ad17006df Task timed out after 300.00 seconds
推荐答案
感谢这个问题...
我发现了问题. Node mysql模块将保持打开状态,直到服务器关闭连接为止,除非处理程序逻辑明确关闭了该连接.
I found the problem. the Node mysql module keeps the connection open until the server closes it unless it is explicitly closed by the handler logic.
因此,节点事件循环永远不会清空,因此永远不会返回回调.在上面的代码中,我做了一个...
So the node event loop never empties and so never returns the callback. In the above code, I did a ...
con.end();
在调用回调之前,它就起作用了.
before calling the callback and it worked.
这篇关于AWS Lambda函数从不调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!