gpt4 book ai didi

node.js - 为什么 AWS Lambda 函数总是超时?

转载 作者:IT老高 更新时间:2023-10-28 23:14:01 25 4
gpt4 key购买 nike

我正在使用 4.3 版本的 nodejs 测试 aws lambda。我能够在控制台测试中成功完成我的处理程序函数中的所有语句,其中包括连接到我们的 vpc 中的 mongodb 主机。但是,函数总是超时。我发现了一些讨论使用回调、设置上下文属性和 IAM 角色权限的帖子和资源,但无论我做什么,它总是会超时。当前代码:

'use strict';

var Mongoose = require('mongoose');
var Device = require('./device_model');
var Alarm = require('./alarm_model');
var Event = require('./event_model');

var mongoConnection = process.env.MONGO_URL;

var connection = Mongoose.connect(mongoConnection);

Mongoose.connection.once('open', function() {
console.log("Connecting to mongo at: " + mongoConnection);
console.log("Mongoose connection in lambda opened");
});

Mongoose.connection.on('error', function(){
console.error("Error creating mongoose connection in lambda, exiting!");
process.exit(1);
});

exports.check_alarms = function(event, context, callback) {

context.callbackWaitsForEmtpyEventLoop = false;
console.log("The incoming event: " + JSON.stringify(event));

var device = null;
Device.findByUUID(event.uuid, function(error, result){
if(!error){
device = result;
console.log("the device: " + JSON.stringify(device));
if(event.Ale && event.Ale.length > 0) {
console.log("We have an alarm, checking if already set");
callback(null, {"status":"alarms"});
} else {
console.log("Event contains no alarm; checking for historic active");
callback(null, {"status":"no alarms"});
}
} else {
console.log("there's a problem on mongo");
callback("problem", "status not so good");
}
});

callback(null, {"status":"outside of device find block"});
}

最佳答案

你有一个错字:

context.callbackWaitsForEmtpyEventLoop = false;

应该是:

context.callbackWaitsForEmptyEventLoop = false;

这里是 documentation谈到 callbackWaitsForEmptyEventLoop 的行为:

callbackWaitsForEmptyEventLoop

The default value is true. This property is useful only to modify the default behavior of the callback. By default, the callback will wait until the Node.js runtime event loop is empty before freezing the process and returning the results to the caller. You can set this property to false to request AWS Lambda to freeze the process soon after the callback is called, even if there are events in the event loop. AWS Lambda will freeze the process, any state data and the events in the Node.js event loop (any remaining events in the event loop processed when the Lambda function is called next and if AWS Lambda chooses to use the frozen process). For more information about callback, see Using the Callback Parameter.

小例子:

// Times out due to typo
exports.function1 = (event, context, callback) => {
setInterval(() => console.log('Long wait'), 100000);
context.callbackWaitsForEmtpyEventLoop = false;
callback(null, 'Hello from Lambda');
};

// Returns successfully
exports.function2 = (event, context, callback) => {
setInterval(() => console.log('Long wait'), 100000);
context.callbackWaitsForEmptyEventLoop = false;
callback(null, 'Hello from Lambda');
};

关于node.js - 为什么 AWS Lambda 函数总是超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com