AWS Lambda函数超时 [英] AWS Lambda function timing out

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

问题描述

在我的本地mocha测试中,以下处理程序功能正常运行.但是,当我上传到AWS(使用无服务器框架)时,它会超时(除非您没有提供uid参数,然后它会立即正确响应).

In my local mocha tests the following handler function works just fine. However, when I upload to AWS (using Serverless framework) it times out (unless you don't provide a uid parameter where it then correctly responds immediately).

尤其奇怪的是,在不到3秒的时间内(超时设置为5秒),作业完成,甚至输出了事实上的"日志消息,但它以某种方式调用了回调,但未完成Lambda功能

What's particularly odd is that in less than 3 seconds (timeout is set at 5 seconds), the job completes and even the "post-facto" log message is output but it somehow calling the callback and that is not completing the Lambda function

这是cloudwatch日志:

Here's the cloudwatch log:

] 这是处理程序函数:

export const handler = (event: IRequestInput, context: IContext, cb: IGatewayCallback) => {
  console.log('EVENT:\n', JSON.stringify(event, null, 2));
  const uid = _.get(event, 'queryStringParameters.uid', undefined);
  if(!uid) {
    cb(null, {
      statusCode: 412,
      body: 'no User ID was provided by frontend'
    });
    return;
  }

  oauth.getRequestToken()
    .then(token => {
      console.log('Token is:\n', JSON.stringify(token, null, 2));
      console.log('User ID: ', uid);
      token.uid = uid;
      return Promise.resolve(token);
    })
    .then((token) => {
      console.log('URL: ', token.url);
      cb(null, {
        statusCode: 200,
        body: token.url
      });
      console.log('post-facto');
    })
    .catch((err: PromiseError) => {
      console.log('Problem in getting promise token: ', err);
      cb(err.message);
    });

};

推荐答案

将以下内容添加为处理函数的第一行:

Add the following as the first line of your handler function:

context.callbackWaitsForEmptyEventLoop = false

这篇关于AWS Lambda函数超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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