AWS Lambda提前结束(没有任何明确的返回或回调) [英] AWS Lambda ending early (without any explicit return or callback)

查看:65
本文介绍了AWS Lambda提前结束(没有任何明确的返回或回调)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放入AWS Lambda的一些node.js代码有点问题.我需要进行几个异步调用,尽管第一个调用的行为与我预期的一样,但是lambda函数在第二个调用完成之前就终止了.

I'm having a bit of an issue with some node.js code I'm putting into AWS Lambda. I've got a couple of async calls that I need to make, and while the first is behaving like I expect, the lambda function is terminating before the second call is complete.

返回为null,这使我认为lambda正在达到其隐式回调,但是我认为在存在尚未解决的承诺的情况下,它不应该这样做.

The return is null, which makes me think that lambda is hitting its implicit callback, but I don't think it should be doing that while there is a promise that hasn't been resolved yet.

代码:

exports.handle = async function(event, context) {
  var AWS = require("aws-sdk");

  AWS.config.update({
    region: "eu-west-1",
    endpoint: "dynamodb.eu-west-1.amazonaws.com"
  });

  var docClient = new AWS.DynamoDB.DocumentClient();
  console.log("Scanning Catalogue...");

  var params = {
    TableName : "Catalogue"
  };

  await docClient.scan(params).promise().then(function (data) {
    console.log("Scan succeeded.");
    data.Items.forEach(function (item) {
      //console.log(item.url);
      checkIfUpdateRequired(item);
    })
  })
}

async function checkIfUpdateRequired (catalogueItem) {
  var request = require("request-promise");
  console.log("Getting " + catalogueItem.url);

  await request(catalogueItem.url).then(function(response) {
    console.log(response.statusCode);
    console.log(response.headers['content-type']);
  });

}

日志输出:

START RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1 Version: $LATEST
2018-05-28T09:20:44.425Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scanning Catalogue...
2018-05-28T09:20:45.446Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Scan succeeded.
2018-05-28T09:20:47.967Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting https://raw.githubusercontent.com/BSData/wh40k/master/Aeldari%20-%20Craftworlds.cat
2018-05-28T09:20:48.028Z    634a55b7-6258-11e8-9f18-6b300c3b5de1    Getting https://raw.githubusercontent.com/BSData/wh40k/master/Imperium%20-%20Adeptus%20Custodes.cat
END RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1
REPORT RequestId: 634a55b7-6258-11e8-9f18-6b300c3b5de1  Duration: 7882.68 ms    Billed Duration: 7900 ms    Memory Size: 128 MB Max Memory Used: 49 MB

因此,日志告诉我checkIfUpdateRequired()正在被调用,但是在实现诺言之前,lambda会结束(报告成功,结果值为null).我没有对处理程序进行任何手动返回或回调,这似乎是与lambda结尾的"early"有关的问题的常态.

So the log tells me that checkIfUpdateRequired() is getting called, but the lambda ends (reporting success with result value null) before the promise is being fulfilled. I'm not doing any manual returns or callbacks to the handler that seem to be the norm for issues related to lambda's ending 'early'.

我不知所措,有人可以提供任何建议吗?

I'm at my wits end, can anyone offer any suggestions?

推荐答案

您不是在等待 checkIfUpdateRequired 承诺完成; docClient.scan 中的所有内容在您的原始代码中都是同步的.使用 Promise.all 等待所有诺言完成:

You're not waiting for the checkIfUpdateRequired promises to complete; everything in docClient.scan is synchronous in your original code. Use Promise.all to wait for all promises to complete:

await docClient.scan(params).promise().then(function (data) {
  console.log("Scan succeeded.");
  return Promise.all(data.Items.map(checkIfUpdateRequired));
});

请注意,如果您使用的是 await ,则如果习惯性地使用它而不是 .then ,则您的代码将更加扁平,更易于理解.例如,您可以重构为:

Note that if you're using await, your code will be flatter and easier to understand if you habitually use it instead of .then. For example, you could refactor to:

const data = await docClient.scan(params).promise();
return Promise.all(data.Items.map(checkIfUpdateRequired));

async function checkIfUpdateRequired (catalogueItem) {
  // actually, better to only require once, rather than on every function call
  const request = require("request-promise");
  console.log("Getting " + catalogueItem.url);
  const response = await request(catalogueItem.url)
  console.log(response.statusCode);
  console.log(response.headers['content-type']);
}

这篇关于AWS Lambda提前结束(没有任何明确的返回或回调)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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