Я создал функцию на Amazon Lambda. Мой сценарий заключается в том, что я должен отправлять два HTTP-запроса одновременно на двух разных URL-адресах. Поэтому я пишу два HTTP-запроса в моем коде с разными URL-адресами. Проблема в том, что большую часть времени первый HTTP-запрос не вызывается, а второй HTTP-запрос почти все время работает. Поэтому, пожалуйста, помогите мне разобраться с проблемой. Вот мой кодДва запроса HTTP-запросов в nodejs
console.log('Loading function');
var aws = require('aws-sdk');
var http = require('http');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var params = {
Bucket: bucket,
Key: decodeURIComponent(key)
};
s3.getObject(params, function(err, data) {
if (err) {
console.log("Error getting object " + key + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.");
context.fail("Error getting file: " + err);
} else {
var userString = JSON.stringify(params);
console.log(userString);
var options = {
hostname: 'example1.com',
path: '/api/test1.cfm',
method: 'POST',
Port:'80',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': userString.length
}
};
console.log("Start");
var x = http.request(options,function(res){
console.log("Connected");
console.log('CONTENT TYPE:', data.ContentType);
console.log("Start");
console.log("x");
context.succeed(data.ContentType);
});
x.write(userString);
x.end();
var optionsdev = {
hostname: 'example2.com',
path: '/api/test2.cfm',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': userString.length
}
};
console.log("Start");
var y = http.request(optionsdev,function(res){
console.log("Connected");
console.log('CONTENT TYPE:', data.ContentType);
console.log("Start");
console.log("y");
context.succeed(data.ContentType);
});
y.write(userString);
y.end();
}
});
};
Почему вы говорите, ваш первый запрос не вызывается? –
Ваш код будет иметь два HTTP-запросов, работающих параллельно, и оба будут бороться с 'context.succeed()' first. Сначала вы закончите, но может быть не предсказуемо, какой из них заканчивается первым. Второй будет закончен, а также вызовет 'context.succeed()'. – jfriend00
его не вызывали когда-то, но иногда правильно запускали –