Refactor error handling in main function and improve response handling
This commit is contained in:
41
index.js
41
index.js
@@ -28,6 +28,13 @@ async function main() {
|
|||||||
data: response.data
|
data: response.data
|
||||||
})
|
})
|
||||||
console.log(`Response registered for instance ${instanceId}`);
|
console.log(`Response registered for instance ${instanceId}`);
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!responses[instanceId]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
responses[instanceId] = responses[instanceId].filter(resp => resp.id !== response.id);
|
||||||
|
//console.log(`Response ${response.id} removed from instance ${instanceId}`);
|
||||||
|
}, 60_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function DoRequest(instanceId, requestObj) {
|
async function DoRequest(instanceId, requestObj) {
|
||||||
@@ -38,13 +45,13 @@ async function main() {
|
|||||||
|
|
||||||
let headers = requestParams.Headers || null;
|
let headers = requestParams.Headers || null;
|
||||||
// if headers is a an array or is an empty object, convert it to null
|
// if headers is a an array or is an empty object, convert it to null
|
||||||
if (Array.isArray(headers) || Object.keys(headers).length === 0) {
|
if (headers && (Array.isArray(headers) || Object.keys(headers).length === 0)) {
|
||||||
headers = null;
|
headers = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const shouldHaveBody = ['post', 'put', 'patch'].includes(method);
|
const shouldHaveBody = ['post', 'put', 'patch'].includes(method);
|
||||||
|
|
||||||
console.log(instanceId,requestObj)
|
console.log("DoRequest",instanceId,requestObj)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
@@ -59,7 +66,7 @@ async function main() {
|
|||||||
data: response.data
|
data: response.data
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (RETRY_ERROR_CODES.includes(error.response.status)) {
|
if (RETRY_ERROR_CODES.includes(error.response?.status)) {
|
||||||
console.log(`Retrying request ${requestObj.id} for instance ${instanceId}`);
|
console.log(`Retrying request ${requestObj.id} for instance ${instanceId}`);
|
||||||
SetTimeout(() => {
|
SetTimeout(() => {
|
||||||
DoRequest(instanceId, requestObj);
|
DoRequest(instanceId, requestObj);
|
||||||
@@ -67,13 +74,23 @@ async function main() {
|
|||||||
} else {
|
} else {
|
||||||
RegisterResponse(instanceId, {
|
RegisterResponse(instanceId, {
|
||||||
id: requestObj.id,
|
id: requestObj.id,
|
||||||
data: error.response.data,
|
data: error.response?.data,
|
||||||
error: "yep there was an error"
|
error: "yep there was an error"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getToSend(instanceId, waiting) {
|
||||||
|
const toSend = [];
|
||||||
|
const instanceResponses = responses[instanceId] || [];
|
||||||
|
instanceResponses.forEach(respObj => {
|
||||||
|
if (waiting.includes(respObj.id)) {
|
||||||
|
toSend.push(respObj);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return toSend;
|
||||||
|
}
|
||||||
|
|
||||||
app.post('/:instanceId', async (req, res) => {
|
app.post('/:instanceId', async (req, res) => {
|
||||||
const instanceId = req.params.instanceId;
|
const instanceId = req.params.instanceId;
|
||||||
@@ -87,15 +104,15 @@ async function main() {
|
|||||||
DoRequest(instanceId,requestObj);
|
DoRequest(instanceId,requestObj);
|
||||||
});
|
});
|
||||||
|
|
||||||
const toSend = [];
|
const startTime = Date.now();
|
||||||
const instanceResponses = responses[instanceId] || [];
|
|
||||||
//console.log(instanceResponses,waiting);
|
let toSend = []
|
||||||
instanceResponses.forEach(respObj => {
|
while (toSend.length === 0 && Date.now() - startTime < 1_000) {
|
||||||
if (waiting.includes(respObj.id)) {
|
toSend = getToSend(instanceId, waiting);
|
||||||
toSend.push(respObj);
|
await new Promise(resolve => setTimeout(resolve, 10));
|
||||||
console.log(`Response sent for instance ${instanceId}`);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
console.log(`${toSend.length} responses ready for instance ${instanceId}`);
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
responses: toSend
|
responses: toSend
|
||||||
|
|||||||
Reference in New Issue
Block a user