Add run status polling endpoint and update rota run detail view for real-time status updates
This commit is contained in:
@@ -738,6 +738,40 @@ def rota_run_detail(request, run_id):
|
||||
return render(request, "rota/rota_run_detail.html", {"run": run, "result_pretty": result_pretty, "builder_html": builder_html})
|
||||
|
||||
|
||||
def rota_run_status(request, run_id):
|
||||
"""Return a small HTML fragment describing the run status.
|
||||
|
||||
This endpoint is intended to be polled by HTMX. While the run is
|
||||
still running we return a fragment that contains the polling HTMX
|
||||
attributes so the client will continue to poll. Once the run is
|
||||
finished we return a final fragment (no polling attributes) and
|
||||
include a small script to reload the page so the UI updates.
|
||||
"""
|
||||
from .models import RotaRun
|
||||
|
||||
run = get_object_or_404(RotaRun, pk=run_id)
|
||||
|
||||
# If the run is still pending/running, return a self-updating fragment
|
||||
if run.status in (RotaRun.STATUS_PENDING, RotaRun.STATUS_RUNNING):
|
||||
html = (
|
||||
f'<div id="run-status" hx-get="{reverse("rota:rota_run_status", args=[run.id])}" '
|
||||
'hx-trigger="every 3s" hx-swap="outerHTML">'
|
||||
f'<span class="tag is-info">Status: {run.get_status_display()}</span>'
|
||||
f' <small>started: {run.started_at}</small>'
|
||||
"</div>"
|
||||
)
|
||||
return HttpResponse(html)
|
||||
|
||||
# Finished/failed: return final fragment and reload the page so the
|
||||
# user sees final logs/export without further polling.
|
||||
final_html = (
|
||||
f'<div id="run-status-final"><span class="tag is-success">Status: {run.get_status_display()}</span>'
|
||||
f' <small>finished: {run.finished_at}</small></div>'
|
||||
"<script>window.location.reload();</script>"
|
||||
)
|
||||
return HttpResponse(final_html)
|
||||
|
||||
|
||||
def rota_run_export_html(request, run_id):
|
||||
from .models import RotaRun
|
||||
|
||||
|
||||
Reference in New Issue
Block a user