在Lambda中长时间运行http GET超时

时间:2016-06-05 06:17:41

标签: node.js aws-lambda

我希望使用Lambda获取主列表并定期将其保存到s3。我已经开始使用快速运行GET请求,但是对于我需要获得的实际数据文件,最终结果是过早退出,因为需要一段时间来获取完整内容。

'use strict';
let http = require('http');


/**
 * get the data and store it in S3
 */
exports.handler = (event, context, callback) => {
    console.log("STARTING?");
    let aws = require('aws-sdk');
    aws.config.region = 'us-west-2';

    let s3 = new aws.S3({ apiVersion: '2006-03-01' });


    var url = "http://long.running.url";

    // do the request of the company list
    http.get(url, (res) => {

        var body = "";

        console.log(`Got response: ${res.statusCode}`);

        // consume response body
        res.setEncoding('utf8');
        res.on('data', (chunk) => {
            body += chunk;
            //console.log(`BODY: ${chunk}`);
        });
        res.on('end', () => {
            console.log('No more data in response.')

            // S3
            var param = {
                Bucket: 'my/s3/bucket',
                Key: 'big.masterlist.txt',
                Body: body
            };
            s3.putObject(param, function(err, data) {
                if (err) {
                    console.log("error", err);
                }
                console.log(data);
                context.done(null, data);
            });

        });

    }).on('error', (e) => {
        console.log(`Got error: ${e.message}`);
    });

};

这导致{"errorMessage":"Process exited before completing request"}

我不确定这是合法的超时还是别的。它可能是什么?

1 个答案:

答案 0 :(得分:2)

在亚马逊的Lambda FAQ

  

问:AWS Lambda函数可以执行多长时间?

     

对AWS Lambda的所有调用必须在300内完成执行   秒。默认超时为3秒,但您可以设置超时   到1到300秒之间的任何值。

可以使用Amazon's Lambda documentation中描述的[AttributeUsage(AttributeTargets.Method)] public sealed class CandidateApiForMenuItem : Attribute { public CandidateApiForMenuItem(string caption) { this.Caption = caption; } public string Caption { get; set; } } public class FormDataElementBase { public FormDataElementBase() { } [CandidateApiForMenuItem("Add PanelGroup")] public void AddPanelGroup() { ///... } [CandidateApiForMenuItem("Add BoxGroup")] public void AddBoxGroup() { ///... } [CandidateApiForMenuItem("Remove")] public void Remove() { ///... } public void GenerateGroupPopupMenuItems() { foreach (MethodInfo methodInfo in this.GetType().GetMethods()) { if (methodInfo.GetCustomAttribute(typeof(CandidateApiForMenuItem)) != null) { // This is true both for FormDataElementBase and all derived // but I want to hide Remove method inside MainGroup class // However it is displayed again }; }; } } public class BoxGroup : FormDataElementBase { } public class PanelGroup : FormDataElementBase { } public class MainGroup : FormDataElementBase { private void Remove() { } } 来调整超时。