Remove meta refresh on task page, use JS to poll

This commit is contained in:
rubenwardy 2018-05-11 15:29:26 +01:00
parent 6d7b810270
commit 510bf50ff2
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
5 changed files with 69 additions and 55 deletions

@ -6,55 +6,6 @@ $(function() {
$(".pkg_meta").show()
}
function getJSON(url, method) {
return new Promise(function(resolve, reject) {
fetch(new Request(url, {
method: method || "get",
credentials: "same-origin",
headers: {
"Accept": "application/json",
},
})).then(function(response) {
response.text().then(function(txt) {
resolve(JSON.parse(txt))
}).catch(reject)
}).catch(reject)
})
}
function performTask(url) {
return new Promise(function(resolve, reject) {
getJSON(url, "post").then(function(startResult) {
console.log(startResult)
if (typeof startResult.poll_url == "string") {
var tries = 0;
function retry() {
tries++;
if (tries > 10) {
reject("timeout")
} else {
console.log("Polling task in " + (tries*100) + "ms")
setTimeout(step, tries*100)
}
}
function step() {
getJSON(startResult.poll_url).then(function(res) {
if (res.status == "SUCCESS") {
console.log("Got result")
resolve(res.result)
} else {
retry()
}
}).catch(retry)
}
retry()
} else {
reject("Start task didn't return string!")
}
}).catch(reject)
})
}
function repoIsSupported(url) {
try {
return URI(url).hostname() == "github.com"

56
app/static/polltask.js Normal file

@ -0,0 +1,56 @@
function getJSON(url, method) {
return new Promise(function(resolve, reject) {
fetch(new Request(url, {
method: method || "get",
credentials: "same-origin",
headers: {
"Accept": "application/json",
},
})).then(function(response) {
response.text().then(function(txt) {
resolve(JSON.parse(txt))
}).catch(reject)
}).catch(reject)
})
}
function pollTask(poll_url, disableTimeout) {
return new Promise(function(resolve, reject) {
var tries = 0;
function retry() {
tries++;
if (!disableTimeout && tries > 10) {
reject("timeout")
} else {
const interval = Math.min(tries*100, 1000)
console.log("Polling task in " + interval + "ms")
setTimeout(step, interval)
}
}
function step() {
getJSON(poll_url).then(function(res) {
if (res.status == "SUCCESS") {
console.log("Got result")
resolve(res.result)
} else {
retry()
}
}).catch(retry)
}
retry()
})
}
function performTask(url) {
return new Promise(function(resolve, reject) {
getJSON(url, "post").then(function(startResult) {
console.log(startResult)
if (typeof startResult.poll_url == "string") {
pollTask(startResult.poll_url).then(resolve).catch(reject)
} else {
reject("Start task didn't return string!")
}
}).catch(reject)
})
}

@ -55,6 +55,7 @@
{% if not package.title %}
<script src="/static/jquery.min.js"></script>
<script src="/static/url.min.js"></script>
<script src="/static/polltask.js"></script>
<script src="/static/package_create.js"></script>
<noscript>
<div class="box box_grey alert alert-warning">

@ -4,12 +4,6 @@
Working
{% endblock %}
{% block headextra %}
{% if not "error" in info %}
<meta http-equiv="refresh" content="1;URL=">
{% endif %}
{% endblock %}
{% block content %}
{% if "error" in info %}
<h1>Task Failed</h1>
@ -17,5 +11,15 @@ Working
<p>{{ info. error }}</p>
{% else %}
<h1>Working…</h1>
<script src="/static/polltask.js"></script>
<script>
pollTask("{{ url_for('check_task', id=info.id) }}", true)
.then(function() { location.reload() })
.catch(function() { location.reload() })
</script>
<noscript>
Reload the page to check for updates.
</noscript>
{% endif %}
{% endblock %}

@ -29,11 +29,13 @@ def check_task(id):
info = None
if isinstance(result, Exception):
info = {
'id': id,
'status': status,
'error': str(result),
}
else:
info = {
'id': id,
'status': status,
'result': result,
}