diff --git a/archivebox/opencode/templates/opencode/agent.html b/archivebox/opencode/templates/opencode/agent.html index d8cc17c2..e6f41a67 100644 --- a/archivebox/opencode/templates/opencode/agent.html +++ b/archivebox/opencode/templates/opencode/agent.html @@ -219,9 +219,17 @@ redirect: "manual", }; let checking = false; + let waking = false; let wasUnavailable = false; let healthyChecks = 0; let knownVersion = "{{ opencode_version|escapejs }}"; + const wake = () => { + if (waking) return; + waking = true; + fetch(healthUrl, fetchOptions) + .catch(() => {}) + .finally(() => { waking = false; }); + }; const check = async () => { if (checking || (document.hidden && wasUnavailable)) return; checking = true; @@ -231,9 +239,7 @@ wasUnavailable = true; healthyChecks = 0; if (response.status === 503) { - try { - await fetch(healthUrl, fetchOptions); - } catch {} + wake(); } return; } diff --git a/archivebox/opencode/views.py b/archivebox/opencode/views.py index 5f82dcab..c689abee 100644 --- a/archivebox/opencode/views.py +++ b/archivebox/opencode/views.py @@ -557,10 +557,12 @@ def agent_health_view(request: HttpRequest): version = _opencode_version(_settings(config)) healthy = bool(version) - return JsonResponse( + response = JsonResponse( {"healthy": healthy, "version": version}, status=200 if healthy else 503, ) + response.headers["Cache-Control"] = "no-store" + return response def _proxy_url(settings: dict, path: str | None) -> str: diff --git a/archivebox/tests/test_opencode_agent.py b/archivebox/tests/test_opencode_agent.py index 9463cfe6..4bd89e2f 100644 --- a/archivebox/tests/test_opencode_agent.py +++ b/archivebox/tests/test_opencode_agent.py @@ -192,6 +192,8 @@ def test_opencode_agent_superuser_gets_admin_wrapper(admin_client, live_opencode assert b"/admin/agent/opencode/global/health" in response.content assert b"/admin/agent/opencode/_archivebox/health" in response.content assert b'redirect: "manual"' in response.content + assert b"let waking = false" in response.content + assert b"wake();" in response.content assert b"frame.contentWindow.location.reload()" in response.content assert response.headers["X-Frame-Options"] == "DENY" assert response.headers["Content-Security-Policy"] == "frame-ancestors 'none'" @@ -217,6 +219,7 @@ def test_opencode_health_monitor_does_not_start_server(admin_client, live_openco assert response.status_code == 503 assert response.json() == {"healthy": False, "version": ""} + assert response.headers["Cache-Control"] == "no-store" assert views._PROCESS is None