promise - 节点 AWS Lambda 问题:它在超时时工作。当我回调时,最后处理的项目实际上并没有处理
问题描述
我最好的猜测是我的代码中有一个 promise 问题,但我想这也可能是我的代码中的 ssh2-sftp-client 问题。
我已经写了 10 种不同的方式。将此视为概念验证代码,而不是 12 岁儿童的著作。在最后一次迭代中,我把它简化了。
批处理作业。
*从 S3 抓取文件 *一个接一个地进行(至少在这次迭代中)并将它们 SFTP 发送给供应商 *删除 S3 中的文件
如果我停止回调,我会在 10 秒后超时,但一切正常。文件在不到 4 秒的时间内向上移动。如果我放入回调,则只处理前两个。我的输出说第三个文件已处理,但事实并非如此。
function processSingleFile(obj, fn, context)
{
return new Promise(async function(resolve, reject)
{
//process.nextTick(async function () {
try{
console.log(obj);
let pathVariables = obj.Key.split('/');
let filename = pathVariables[2];
let srcKeyString = pathVariables[1];
console.log("Beginning to process: " + filename);
/*
let buffer = s3.getObject({
Bucket: bucket_name,
Key: obj.Key
}).createReadStream();
*/
let buffer = await getS3File(bucket_name, obj.Key);
console.log("Got file info " + obj.Key );
let Client = require('ssh2-sftp-client');
let sftp = new Client();
let remoteFilePath = '/Import/30Automations/' + filename;
sftp.connect({
host: hostTargetName,
port: '22',
username: userName,
password: password
}).then(() => {
sftp.put(buffer, remoteFilePath);
}).then(() => {
sftp.end;
sftp.close;
//sftp.end(); //nothing works when I use this
}).then(() => {
Client.end; //not needed
}).then(() => {
deleteEncryptedlFile(obj.Key); //this part of code not shown
}).then(() => {
console.log("Successfully processed: " + filename);
resolve();
}).catch(err => {
console.error("ALERT in transfer for " + filename + ": " + err.message);
resolve();
});
}catch(err){
console.log("ALERT: processing single file: " + obj + "err: " + err);
resolve();
}
});
}
exports.handler = function(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
async function moveFiles(){
try{
var params = {
Bucket: bucketname,
Delimiter: '',
Prefix: 'outgoing'
}
let index = 0;
s3.listObjects(params, async function (err, data) {
if(err)throw err;
let rows = data.Contents.length;
while (index < rows){
var reply = await processSingleFile(data.Contents[index]);
index++;
if (index === rows){
console.log("JOB COMPLETED");
callback(null, JSON.stringify("success")); //if I comment out, all files are moved successfully but I time out.
}
}
});
}catch(err){
console.error("ALERT Error processing job. See logs " + err.message);
callback(JSON.stringify("Error"), null);
}
}
moveFiles();
};
这是输出
2020-01-17T01:04:44.790Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Beginning to process: 20200115_Ta11_file3.txt.pgp
2020-01-17T01:04:44.923Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Got file info outgoing/professional/20200115_Ta11_file3.txt.pgp
2020-01-17T01:04:46.015Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO file deleted outgoing/professional/20200115_Ta11_file3.txt.pgp
2020-01-17T01:04:46.018Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Successfully processed: 20200115_Ta11_file3.txt.pgp
2020-01-17T01:04:46.019Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Beginning to process: 20200116_Ta11_file1.txt.pgp
2020-01-17T01:04:46.179Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Got file info outgoing/professional/20200116_Ta11_file1.txt.pgp
2020-01-17T01:04:47.091Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO file deleted outgoing/professional/20200116_Ta11_file1.txt.pgp
2020-01-17T01:04:47.093Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Successfully processed: 20200116_Ta11_file1.txt.pgp
2020-01-17T01:04:47.093Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Beginning to process: 20200116_Ta11_file2.txt.pgp
2020-01-17T01:04:47.252Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Got file info outgoing/professional/20200116_Ta11_file2.txt.pgp
2020-01-17T01:04:48.197Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO file deleted outgoing/professional/20200116_Ta11_file2.txt.pgp
2020-01-17T01:04:48.199Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO Successfully processed: 20200116_Ta11_file2.txt.pgp
2020-01-17T01:04:48.199Z 6630b8b7-764d-4940-87f8-d6ada6b6eb84 INFO JOB COMPLETED
END RequestId: 6630b8b7-764d-4940-87f8-d6ada6b6eb84
REPORT RequestId: 6630b8b7-764d-4940-87f8-d6ada6b6eb84 Duration: 3775.81 ms Billed Duration: 3800 ms Memory Size: 1536 MB Max Memory Used: 105 MB Init Duration: 541.10 ms
解决方案
我在下面的部分中省略了回报。现在回报就在那里。
sftp.connect({
host: hostTargetName,
port: '22',
username: userName,
password: password
}).then(() => {
return sftp.put(buffer, remoteFilePath);
}).then(() => {
return sftp.end();
}).then(() => {
return deleteEncryptedlFile(obj.Key); //this part of code not shown
}).then(() => {
console.log("Successfully processed: " + filename);
resolve();
}).catch(err => {
console.error("ALERT in transfer for " + filename + ": " + err.message);
resolve();
});
推荐阅读
- xamarin - 隐藏 Xamarin MAC 平台的 ScrollView 的 Scollbar
- python - 为每个文件分别制作具有输出和输入文件大小信息的新 txt 文件
- r - 使用 sets 包对 R 中的集合列表进行集合操作
- sql - 我想计算每个年龄的人
- java - 替换事务后如何保持片段状态?
- json - 找不到密钥时使用 JQ“包含”并抑制错误
- elasticsearch - 在可视化之前编辑数据
- sql-server - SQL 电子邮件通知
- angular - Angular 和 Stackblitz
- .net - 是否可以在高级安装程序中从安装程序执行某些命令之前和之后将文件复制到用户计算机?