aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-05-11 15:29:26 +0100
committerrubenwardy <rw@rubenwardy.com>2018-05-11 15:29:26 +0100
commit510bf50ff2fb29817fc754d6793e506e3414bc6b (patch)
treed4dc5c04d9bcf793593d7091607b978c1950832a
parent6d7b810270b21575dca54eb36f371969b976bafd (diff)
downloadcheatdb-510bf50ff2fb29817fc754d6793e506e3414bc6b.tar.xz
Remove meta refresh on task page, use JS to poll
-rw-r--r--app/static/package_create.js49
-rw-r--r--app/static/polltask.js56
-rw-r--r--app/templates/packages/create_edit.html1
-rw-r--r--app/templates/tasks/view.html16
-rw-r--r--app/views/tasks.py2
5 files changed, 69 insertions, 55 deletions
diff --git a/app/static/package_create.js b/app/static/package_create.js
index 927273f..6568550 100644
--- a/app/static/package_create.js
+++ b/app/static/package_create.js
@@ -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"
diff --git a/app/static/polltask.js b/app/static/polltask.js
new file mode 100644
index 0000000..bc9d802
--- /dev/null
+++ b/app/static/polltask.js
@@ -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)
+ })
+}
diff --git a/app/templates/packages/create_edit.html b/app/templates/packages/create_edit.html
index 3378a5d..9049e81 100644
--- a/app/templates/packages/create_edit.html
+++ b/app/templates/packages/create_edit.html
@@ -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">
diff --git a/app/templates/tasks/view.html b/app/templates/tasks/view.html
index a01ca80..d47203f 100644
--- a/app/templates/tasks/view.html
+++ b/app/templates/tasks/view.html
@@ -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 %}
diff --git a/app/views/tasks.py b/app/views/tasks.py
index 64ed702..72d7d1d 100644
--- a/app/views/tasks.py
+++ b/app/views/tasks.py
@@ -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,
}