contentdb/app/public/static/js/polltask.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-05-17 16:18:20 +02:00
// @author rubenwardy
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
"use strict";
2023-08-26 14:21:08 +02:00
async function getJSON(url, method) {
const response = await fetch(new Request(url, {
method: method || "get",
credentials: "same-origin",
headers: {
"Accept": "application/json",
},
2023-08-26 14:21:08 +02:00
}));
return await response.json();
}
function sleep(interval) {
return new Promise(resolve => setTimeout(resolve, interval));
}
2020-12-04 03:34:08 +01:00
2023-08-26 14:21:08 +02:00
async function pollTask(poll_url, disableTimeout) {
let tries = 0;
while (true) {
tries++;
if (!disableTimeout && tries > 30) {
throw "timeout";
} else {
const interval = Math.min(tries * 100, 1000);
console.log("Polling task in " + interval + "ms");
await sleep(interval);
}
2023-08-26 14:21:08 +02:00
let res = undefined;
try {
res = await getJSON(poll_url);
} catch (e) {
console.error(e);
}
2023-08-26 14:21:08 +02:00
if (res && res.status === "SUCCESS") {
console.log("Got result")
return res.result;
} else if (res && (res.status === "FAILURE" || res.status === "REVOKED")) {
throw res.error ?? "Unknown server error";
}
}
}
2023-08-26 14:21:08 +02:00
async function performTask(url) {
const startResult = await getJSON(url, "post");
console.log(startResult);
if (typeof startResult.poll_url == "string") {
return await pollTask(startResult.poll_url);
} else {
throw "Start task didn't return string!";
}
}