我的 node.js 代码在 aws-lambda 中工作不稳定,有时它工作正常,有时跳过数据持久性那段代码
my node.js code work unstable in aws-lambda, Sometimes it works fine, sometimes skipping the data persistence that piece of code
数据中心每五分钟向我们的 S3 存储桶上传一个 csv 文件,这会触发我的 lambda 函数读取该文件并将数据保存到 DynamoDB。但是执行数据持久化的代码并不稳定,有时会执行,有时会完全跳过。这让我很困惑。这是我的代码。
var AWS = require('aws-sdk');
var csvtojson = require('csvtojson');
var encoding = require('text-encoding');
AWS.config.update({
accessKeyId: '********',
secretAccessKey: '**********',
region: 'us-west-2',
sslEnabled:false
});
var s3 = new AWS.S3();
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
await res.map(async(item,key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
let putRes = await ddb.putItem(putParams).promise();
console.dir(putRes);
});
}
catch(error){
console.error(error);
return error;
}
};
Array.map()
returns 数组不是 Promise,所以你不能 await
它(例如你的代码中的 await res.map()
)。
首先,您应该收集一份承诺列表,并使用 Promise.all()
等待所有承诺。
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
// Construct the list of promises.
const promises = res.map((item, key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
return ddb.putItem(putParams).promise();
});
// Wait for all promises to finish.
return Promise.all(promises)
}
catch(error){
console.error(error);
return error;
}
};
数据中心每五分钟向我们的 S3 存储桶上传一个 csv 文件,这会触发我的 lambda 函数读取该文件并将数据保存到 DynamoDB。但是执行数据持久化的代码并不稳定,有时会执行,有时会完全跳过。这让我很困惑。这是我的代码。
var AWS = require('aws-sdk');
var csvtojson = require('csvtojson');
var encoding = require('text-encoding');
AWS.config.update({
accessKeyId: '********',
secretAccessKey: '**********',
region: 'us-west-2',
sslEnabled:false
});
var s3 = new AWS.S3();
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
await res.map(async(item,key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
let putRes = await ddb.putItem(putParams).promise();
console.dir(putRes);
});
}
catch(error){
console.error(error);
return error;
}
};
Array.map()
returns 数组不是 Promise,所以你不能 await
它(例如你的代码中的 await res.map()
)。
首先,您应该收集一份承诺列表,并使用 Promise.all()
等待所有承诺。
exports.handler = async (event) => {
try {
console.log(event);
console.log(event['Records'][0]['s3']['object']['key']);
//get the file name
let key = event['Records'][0]['s3']['object']['key'];
let date = `${key}`.slice(24,36);
console.log(date);
let getObject = {Bucket: 'saas-status-mockup-data', Key: `${key}`};
//get the object
let response = await s3.getObject(getObject).promise();
//transfer to csv
let csvFile= new encoding.TextDecoder("utf-8").decode(response.Body);
//transfer to json
let res = await csvtojson().fromString(csvFile);
console.log(res);
// Construct the list of promises.
const promises = res.map((item, key) => {
console.log(item);
let putParams = {};
if(item.FARM=="SMAX Internal production Functionalities"){
putParams.TableName = 'InternalProductionDb';
} else if(item.FARM=="SMAX Trial Major Functionalities"){
putParams.TableName = 'TrialMajorDb';
} else {
console.error(item);
}
putParams.Item = {
'Date' : {
S:`${date}${item.BUSINESS_PROCESS}`
},
'StatusId':{
S:`${date}${item.BUSINESS_PROCESS}`
},
'BusinessProcess':{
S:`${item.BUSINESS_PROCESS}`
},
'Status':{
S:`${item.STATUS}`
}
};
console.log(putParams);
//put data to dynamoDB, But sometimes this code sometimes does not execute.
return ddb.putItem(putParams).promise();
});
// Wait for all promises to finish.
return Promise.all(promises)
}
catch(error){
console.error(error);
return error;
}
};