Skip to content

Commit

Permalink
added auto retrying to the aPI calls after timeout in an attempt to d…
Browse files Browse the repository at this point in the history
…eal with waking up the lambda functions
  • Loading branch information
ronnyTodgers committed Jul 5, 2023
1 parent 641f185 commit deec1e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function Upload({ fileInfos, setFileInfos, setApiData, existingIn
}))
});
Promise.all(frp).then((allFiles) => {
postData("https://api.harmonydata.org/text/parse", allFiles, 30000).then((data) => {
postData("https://api.harmonydata.org/text/parse", allFiles,15000).then((data) => {
const newFileInfos = [...fileInfos]
// Load each returned file / instrument in the data
data.forEach(instrument => {
Expand Down
42 changes: 26 additions & 16 deletions src/utilities/postData.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@


async function postData(url = "", data = {}, timeout = 8000) {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify(data),
signal: controller.signal
});
clearTimeout(id);
var retries = 3
var response;
while(retries > 0 && !(response && response.ok)) {
try{
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
response = await fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify(data),
signal: controller.signal

});
clearTimeout(id);
} catch (e) {
console.log(e)
}
retries --;
}
return response.json();
}
export default postData;

0 comments on commit deec1e4

Please sign in to comment.