From 408d716e8b4029f3db86fbd3e07be1729ab9214e Mon Sep 17 00:00:00 2001 From: William David van Collenburg Date: Sun, 22 Nov 2026 14:09:27 +0100 Subject: [PATCH 1/2] fix(backup): the Backup tab stays while a cluster answers slowly (status from the cached pre-flight, refreshed in the background; a timeout is not 'off') --- backup/api.py | 10 ++++------ backup/jobs.py | 32 ++++++++++++++++++++++++++++++++ dashboard/app.py | 14 ++++++++++++-- dashboard/static/index.html | 7 ++++--- docs/CHANGELOG.md | 10 ++++++++++ tests/test_backup_jobs.py | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 11 deletions(-) diff --git a/backup/api.py b/backup/api.py index 451b05f..bee3e73 100644 --- a/backup/api.py +++ b/backup/api.py @@ -100,12 +100,10 @@ def status(): loc = jobs.location_for_cluster(cid) if cid else None pf = None if cid: - try: - pf = jobs.preflight(jobs.client_for(cid), cid, max_age=60) - if not pf['ok']: - pf = dict(pf, hint=jobs.DEFERRED_HINT.get(pf['kind'], '')) - except HyperCoreError as e: - pf = {'ok': True, 'kind': None, 'detail': '', 'nodes': [], 'unavailable': str(e)} + # never wait for the cluster here: the last answer, refreshed in the background when stale + pf = jobs.preflight_cached(cid, max_age=60) + if pf and not pf['ok']: + pf = dict(pf, hint=jobs.DEFERRED_HINT.get(pf['kind'], '')) return jsonify({ 'service': 'backup', # update in progress / node missing: the dashboard shows a banner, runs are deferred diff --git a/backup/jobs.py b/backup/jobs.py index 6f2bf07..63c2be7 100644 --- a/backup/jobs.py +++ b/backup/jobs.py @@ -8,6 +8,7 @@ import logging import re import shutil +import threading import time from datetime import datetime, timezone from pathlib import Path @@ -211,6 +212,37 @@ def preflight(hc: HyperCoreClient, cluster_id: str | None = None, max_age: float return result +_PREFLIGHT_REFRESHING: set[str] = set() +_PREFLIGHT_LOCK = threading.Lock() + + +def preflight_cached(cluster_id: str, max_age: float = 60, client_factory=None) -> dict | None: + """The last known pre-flight answer for the dashboard, without waiting for the cluster. A missing or + stale answer is refreshed in a background thread (one per cluster at a time); until the first answer + arrives this returns None. Asking the cluster live made /status take as long as the HyperCore login, + which with a node down (timeouts per node) outlasted the dashboard's 3 s and hid the Backup tab.""" + hit = _PREFLIGHT_CACHE.get(cluster_id) + if hit and time.monotonic() - hit[0] < max_age: + return hit[1] + with _PREFLIGHT_LOCK: + if cluster_id in _PREFLIGHT_REFRESHING: + return hit[1] if hit else None + _PREFLIGHT_REFRESHING.add(cluster_id) + + def refresh(): + try: + preflight((client_factory or client_for)(cluster_id), cluster_id) + except Exception as e: # noqa: BLE001 -- unreachable cluster: the next call tries again + log.info('[%s] pre-flight for the dashboard failed: %s', cluster_id, e) + _PREFLIGHT_CACHE[cluster_id] = (time.monotonic(), {'ok': True, 'kind': None, 'detail': '', 'nodes': [], + 'unavailable': str(e)}) + finally: + with _PREFLIGHT_LOCK: + _PREFLIGHT_REFRESHING.discard(cluster_id) + threading.Thread(target=refresh, name=f'preflight-{cluster_id[:8]}', daemon=True).start() + return hit[1] if hit else None + + def wait_until_clear(hc: HyperCoreClient, on_wait, max_hours: float | None = None, poll_seconds: int | None = None) -> dict: """Pre-flight, repeated until the cluster is idle or the wait is used up. on_wait(detail) is called whenever the reason changes. Returns the last pre-flight result (ok or not).""" diff --git a/dashboard/app.py b/dashboard/app.py index 67fbba5..910fb6e 100644 --- a/dashboard/app.py +++ b/dashboard/app.py @@ -2023,14 +2023,24 @@ def backup_status(): try: rq = urllib.request.Request(f'{url}/api/v1/status?cluster={_cluster_id()}', headers={'Authorization': f'Bearer {token}'} if token else {}) - with urllib.request.urlopen(rq, timeout=3) as resp: + with urllib.request.urlopen(rq, timeout=5) as resp: data = json.loads(resp.read().decode()) data['available'] = True return jsonify(data if _access().can('backup.targets.manage') else _strip_sensitive(data)) - except Exception: + except Exception as e: # noqa: BLE001 + # a slow answer is not a missing service: keep the tab (it hid while a node was down); only a + # refused or unresolvable connection means the backup container is not running + if _is_timeout(e): + return jsonify({'available': True, 'status_slow': True}) return jsonify({'available': False}) +def _is_timeout(e: BaseException) -> bool: + import socket + reason = getattr(e, 'reason', e) + return isinstance(reason, (socket.timeout, TimeoutError)) or 'timed out' in str(reason).lower() + + @app.route('/api/backup/', methods=['GET', 'POST', 'PUT', 'DELETE']) @requires(None) def backup_proxy(sub): diff --git a/dashboard/static/index.html b/dashboard/static/index.html index ae4b3be..215807a 100644 --- a/dashboard/static/index.html +++ b/dashboard/static/index.html @@ -6177,11 +6177,12 @@

HyperCore Manager

return false; } const d = await apiFetch('/api/backup/status'); - _exAvailable = !!(d && d.available); - if (d && d.available) { _exDefaults = d; _exLocation = d.location || null; _exClusterZone = d.cluster_timezone || ''; } const tab = document.getElementById('navTabExports'); + if (!d) return _exAvailable; // the request itself failed (network, dashboard restart): leave the tab as it is + _exAvailable = !!d.available; + if (d.available && !d.status_slow) { _exDefaults = d; _exLocation = d.location || null; _exClusterZone = d.cluster_timezone || ''; } if (tab) tab.hidden = !_exAvailable; - _exPreflightBanner(d && d.available ? d.preflight : null); + if (!d.status_slow) _exPreflightBanner(d.available ? d.preflight : null); return _exAvailable; } // cluster self-update or a node missing: HyperCore cannot snapshot, so every backup, export and diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5d01bc3..ebc0df9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/). +## [Unreleased] + +### Fixed +- **The Backup tab no longer disappears while a cluster answers slowly.** The backup service's status asked the + cluster live (update state and nodes) and so took as long as the HyperCore login: with a node down, or a newly + registered cluster whose nodes did not answer, that outlasted the dashboard's 3 s and the tab was hidden, for + everyone, just when a node was missing. The status now answers from the last known pre-flight and refreshes it in + the background (one refresh per cluster at a time); a status that times out keeps the tab (only a refused + connection, a stopped backup service, hides it); and a failed status request leaves the tab as it was. + ## [1.7.3] ### Fixed diff --git a/tests/test_backup_jobs.py b/tests/test_backup_jobs.py index 8960d69..a8bc97c 100644 --- a/tests/test_backup_jobs.py +++ b/tests/test_backup_jobs.py @@ -7,6 +7,7 @@ import importlib import os import sys +import time import pytest @@ -217,3 +218,35 @@ def test_nfs_is_refused_for_hypercore_import_and_export(monkeypatch): monkeypatch.setattr(jobs.db, 'update_import', lambda iid, **f: imports.update(f)) jobs.run_import(3) assert imports['status'] == 'ERROR' and 'SMB only' in imports['message'] and not asked + + +def test_preflight_for_the_dashboard_never_waits_for_the_cluster(monkeypatch): + """/status answers from the last pre-flight; a stale or missing one is refreshed in the background, once per + cluster at a time, so a slow login (a node down) cannot make the dashboard hide the Backup tab.""" + import threading + jobs._PREFLIGHT_CACHE.clear(); jobs._PREFLIGHT_REFRESHING.clear() + gate, calls = threading.Event(), [] + + class SlowHC: + def update_status(self): return {} + def list_nodes(self): + calls.append(1); gate.wait(5) + return [{'lanIP': '10.0.0.1', 'networkStatus': 'ONLINE'}] + assert jobs.preflight_cached('c1', client_factory=lambda cid: SlowHC()) is None, 'first call: nothing known yet, no wait' + assert jobs.preflight_cached('c1', client_factory=lambda cid: SlowHC()) is None + gate.set() + for _ in range(50): + if 'c1' in jobs._PREFLIGHT_CACHE and not jobs._PREFLIGHT_REFRESHING: break + time.sleep(0.02) + assert len(calls) == 1, 'one refresh at a time per cluster' + assert jobs.preflight_cached('c1', client_factory=lambda cid: SlowHC())['ok'] is True + # stale: the old answer comes back at once while a refresh runs; a failing cluster leaves a non-blocking answer + jobs._PREFLIGHT_CACHE['c1'] = (time.monotonic() - 120, {'ok': False, 'kind': 'node', 'detail': 'old', 'nodes': []}) + + def boom(cid): raise jobs.HyperCoreError('login timed out') + assert jobs.preflight_cached('c1', client_factory=boom)['detail'] == 'old' + for _ in range(50): + if not jobs._PREFLIGHT_REFRESHING: break + time.sleep(0.02) + pf = jobs.preflight_cached('c1', client_factory=boom) + assert pf['ok'] is True and 'timed out' in pf['unavailable'] From e669ff20464bd1044554e0b66c0eac5be518a228 Mon Sep 17 00:00:00 2001 From: William David van Collenburg Date: Sun, 22 Nov 2026 14:10:30 +0100 Subject: [PATCH 2/2] chore: release 1.7.4 --- VERSION | 2 +- docs/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 661e7ae..10c0880 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.7.3 +1.7.4 diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ebc0df9..b1e063d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/). -## [Unreleased] +## [1.7.4] ### Fixed - **The Backup tab no longer disappears while a cluster answers slowly.** The backup service's status asked the