为什么AWS Lambda功能总是超时? [英] Why does AWS Lambda function always time out?

查看:456
本文介绍了为什么AWS Lambda功能总是超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am testing out aws lambda, using nodejs with the 4.3 version. I'm able to successfully complete all the statements in my handler function within the console test, which includes connecting to a mongodb host within our vpc. But, the function always times out. I've found several posts and resources that discuss using the callback, and setting properties on the context, and IAM role permissions, but no matter what I do, it always ends up timing out. Current code:

'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;


这是文档说明 callbackWaitsForEmptyEventLoop 的行为:


Here's what the documentation says about the behavior of callbackWaitsForEmptyEventLoop:

callbackWaitsForEmptyEventLoop

默认值为true.此属性仅在修改回调的默认行为时有用.默认情况下,回调将等待,直到Node.js运行时事件循环为空,然后再冻结进程并将结果返回给调用方.您可以将此属性设置为false,以请求AWS Lambda在调用回调后立即冻结进程,即使事件循环中有事件也是如此. AWS Lambda将冻结进程,Node.js事件循环中的任何状态数据和事件(事件循环中任何剩余的事件将在下次调用Lambda函数且AWS Lambda选择使用冻结的过程时进行处理).有关回调的更多信息,请参见使用回调参数.

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');
};

这篇关于为什么AWS Lambda功能总是超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆