| 1 |
<!doctype html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset=UTF-8> |
| 5 |
<title>Celery Example</title> |
| 6 |
</head> |
| 7 |
<body> |
| 8 |
<h2>Celery Example</h2> |
| 9 |
Execute background tasks with Celery. Submits tasks and shows results using JavaScript. |
| 10 |
|
| 11 |
<hr> |
| 12 |
<h4>Add</h4> |
| 13 |
<p>Start a task to add two numbers, then poll for the result. |
| 14 |
<form id=add method=post action="{{ url_for("tasks.add") }}"> |
| 15 |
<label>A <input type=number name=a value=4></label><br> |
| 16 |
<label>B <input type=number name=b value=2></label><br> |
| 17 |
<input type=submit> |
| 18 |
</form> |
| 19 |
<p>Result: <span id=add-result></span></p> |
| 20 |
|
| 21 |
<hr> |
| 22 |
<h4>Block</h4> |
| 23 |
<p>Start a task that takes 5 seconds. However, the response will return immediately. |
| 24 |
<form id=block method=post action="{{ url_for("tasks.block") }}"> |
| 25 |
<input type=submit> |
| 26 |
</form> |
| 27 |
<p id=block-result></p> |
| 28 |
|
| 29 |
<hr> |
| 30 |
<h4>Process</h4> |
| 31 |
<p>Start a task that counts, waiting one second each time, showing progress. |
| 32 |
<form id=process method=post action="{{ url_for("tasks.process") }}"> |
| 33 |
<label>Total <input type=number name=total value="10"></label><br> |
| 34 |
<input type=submit> |
| 35 |
</form> |
| 36 |
<p id=process-result></p> |
| 37 |
|
| 38 |
<script> |
| 39 |
const taskForm = (formName, doPoll, report) => { |
| 40 |
document.forms[formName].addEventListener("submit", (event) => { |
| 41 |
event.preventDefault() |
| 42 |
fetch(event.target.action, { |
| 43 |
method: "POST", |
| 44 |
body: new FormData(event.target) |
| 45 |
}) |
| 46 |
.then(response => response.json()) |
| 47 |
.then(data => { |
| 48 |
report(null) |
| 49 |
|
| 50 |
const poll = () => { |
| 51 |
fetch(`/tasks/result/${data["result_id"]}`) |
| 52 |
.then(response => response.json()) |
| 53 |
.then(data => { |
| 54 |
report(data) |
| 55 |
|
| 56 |
if (!data["ready"]) { |
| 57 |
setTimeout(poll, 500) |
| 58 |
} else if (!data["successful"]) { |
| 59 |
console.error(formName, data) |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
if (doPoll) { |
| 65 |
poll() |
| 66 |
} |
| 67 |
}) |
| 68 |
}) |
| 69 |
} |
| 70 |
|
| 71 |
taskForm("add", true, data => { |
| 72 |
const el = document.getElementById("add-result") |
| 73 |
|
| 74 |
if (data === null) { |
| 75 |
el.innerText = "submitted" |
| 76 |
} else if (!data["ready"]) { |
| 77 |
el.innerText = "waiting" |
| 78 |
} else if (!data["successful"]) { |
| 79 |
el.innerText = "error, check console" |
| 80 |
} else { |
| 81 |
el.innerText = data["value"] |
| 82 |
} |
| 83 |
}) |
| 84 |
|
| 85 |
taskForm("block", false, data => { |
| 86 |
document.getElementById("block-result").innerText = ( |
| 87 |
"request finished, check celery log to see task finish in 5 seconds" |
| 88 |
) |
| 89 |
}) |
| 90 |
|
| 91 |
taskForm("process", true, data => { |
| 92 |
const el = document.getElementById("process-result") |
| 93 |
|
| 94 |
if (data === null) { |
| 95 |
el.innerText = "submitted" |
| 96 |
} else if (!data["ready"]) { |
| 97 |
el.innerText = `${data["value"]["current"]} / ${data["value"]["total"]}` |
| 98 |
} else if (!data["successful"]) { |
| 99 |
el.innerText = "error, check console" |
| 100 |
} else { |
| 101 |
el.innerText = "✅ done" |
| 102 |
} |
| 103 |
console.log(data) |
| 104 |
}) |
| 105 |
|
| 106 |
</script> |
| 107 |
</body> |
| 108 |
</html> |