成功完成后,AWS Lambda函数超时

时间:2019-05-20 13:23:29

标签: node.js amazon-web-services aws-lambda

我有一个lambda函数,可以正常工作。我可以在S3和另一个FTP服务器上创建文件。问题是即使成功完成,它也会超时。它只是没有完成执行。从日志中可以看到,大约需要2秒,而指定的超时时间是5秒。如何使我的功能在完成后立即停止?这是完整的代码:

"use strict";

var config = require('./config/dev');
var sql = require("mssql");
var AWS = require('aws-sdk');
var PromiseFtp = require('promise-ftp');
var fs = require('fs');
const request = require('request');


exports.handler = (event, context, callback) => {
    GetDataFromServer(event, context, callback);
};

function GetDataFromServer(event, context, callback) {
    console.log("Fetching data from database...");
    var keys = [], outputText = '';
    sql.connect(config.db, err => {
        if (err) {
            console.log("Error while connecting database :- " + err);
            return callback(true, 'Error in fetching records from database...');
        }
        else {
            new sql.Request()
                .input('ClientId', sql.Int, 469)
                .execute('ForesightDailyDataPull', (err, result) => {
                    if (!err) {
                        //Create Text here and assign to "outputText"
                        CreateFileOnS3(outputText, event, context, callback);
                    }
                    else {
                        console.log('Error in fetching records from database...');
                        return callback(true, 'Error in fetching records from database...');
                    }
                })
        }
    });
    sql.on('error', err => {
        console.log('Error in fetching records from database...');
        return callback(true, 'Error in fetching records from database...');
    })
}

function CreateFileOnS3(fileData, event, context, callback) {
    const fileName = generateFileName();
    console.log('Sending file to S3...');
    const s3 = new AWS.S3(config.awsCredentials);
    const params = {
        Bucket: config.app.s3Bucket,
        Key: fileName,
        Body: fileData
    };
    s3.upload(params, function (s3Err, data) {
        if (s3Err) {
            console.log('There was an error creating file on S3');
            return callback(true, 'There was an error creating file on S3');
        }
        else {
            console.log(`File uploaded successfully at ${data.Location}`);
            CreatefileOnFTP(fileData, fileName, event, context, callback);
        }
    });
}

function CreatefileOnFTP(fileData, fileName, event, context, callback) {
    console.log('Sending file to FTP...');
    var ftpObject = {
        "fileData": fileData,
        "fileName": fileName,
        "ftpURL": config.ftpDetails.ftpProtocol + "://" + config.ftpDetails.host,
        "ftpUserName": config.ftpDetails.user,
        "ftpPassword": config.ftpDetails.password
    };
    request({
        url: config.ftpUploadURL,
        method: "POST",
        json: true,
        body: ftpObject
    }, function (error, response, body) {
        if (!error) {
            console.log('File sent successfully to FTP server.');
            return callback(null, 'File sent successfully to FTP...');
        }
        else {
            console.log('An error occurred while sending file to FTP.');
            return callback(true, 'Error in sending file to FTP...');
        }
    });
}

function generateFileName() {
    var _d = new Date(),
        y = _d.getFullYear(),
        m = _d.getMonth() + 1,
        d = _d.getDate();
    return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + '.txt';
}

1 个答案:

答案 0 :(得分:1)

该函数完成后,应在操作解决后调用sql.close(),以关闭打开的sql连接。

function GetDataFromServer(event, context, callback) {
    console.log("Fetching data from database...");
    var keys = [], outputText = '';
    sql.connect(config.db, err => {
        if (err) {
            console.log("Error while connecting database :- " + err);
            return callback(true, 'Error in fetching records from database...');
        }
        else {
            new sql.Request()
                .input('ClientId', sql.Int, 469)
                .execute('ForesightDailyDataPull', (err, result) => {
                    if (!err) {
                        //Create Text here and assign to "outputText"
                        CreateFileOnS3(outputText, event, context, callback);
                        sql.close() //HERE
                    }
                    else {
                        console.log('Error in fetching records from database...');
                        sql.close() //HERE
                        return callback(true, 'Error in fetching records from database...');

                    }
                })
        }
    });
    sql.on('error', err => {
        console.log('Error in fetching records from database...');
        sql.close() //HERE
        return callback(true, 'Error in fetching records from database...');
    })
}