From 06e16510d5f32c11a43b69386fd0b38dadbe3d33 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:00:10 +0100 Subject: [PATCH] refactor: one low disk threshold in hostguard --- scripts/cleanup_disk.py | 5 +++-- scripts/core/hostguard.py | 10 +++++++++- scripts/devtools/diskcleanup.py | 5 ++--- scripts/unattended/runner.py | 7 +++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/scripts/cleanup_disk.py b/scripts/cleanup_disk.py index ce390f73f..7a0159984 100644 --- a/scripts/cleanup_disk.py +++ b/scripts/cleanup_disk.py @@ -38,7 +38,8 @@ from pathlib import Path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from core.gitclone import main_checkout -from devtools.diskcleanup import ALERT_GB, apply, busy_paths, disk_line, inside, plan +from core.hostguard import LOW_DISK_GB +from devtools.diskcleanup import apply, busy_paths, disk_line, inside, plan def main() -> int: @@ -46,7 +47,7 @@ def main() -> int: parser.add_argument("--apply", action="store_true", help="delete (default: dry run)") parser.add_argument("--hours", type=float, default=72, help="keep anything younger than this (default: %(default)s)") - parser.add_argument("--alert-gb", type=float, default=ALERT_GB, + parser.add_argument("--alert-gb", type=float, default=LOW_DISK_GB, help="alert under this much free space (default: %(default)s)") parser.add_argument("--check", action="store_true", help="report free space only, delete nothing") diff --git a/scripts/core/hostguard.py b/scripts/core/hostguard.py index 2665330a5..a7969bde4 100644 --- a/scripts/core/hostguard.py +++ b/scripts/core/hostguard.py @@ -15,6 +15,8 @@ LOCK_HELD_ENV = "RETROBIOS_AUTO_LOCK_HELD" # A segfault as a shell reports it. Python reports the signal as -11. SHELL_SEGFAULT = 139 HOST_MISREAD_MARKERS = ("SystemError", "unknown opcode") +# Under this much free space the autorun state and the disk check both alert. +LOW_DISK_GB = 100 class LockBusy(RuntimeError): @@ -50,8 +52,14 @@ def lock_busy(path: Path) -> bool: return True +def disk_gb(path: Path) -> tuple[float, float]: + """Free and total space of *path*'s filesystem, in GB.""" + usage = shutil.disk_usage(path) + return usage.free / 1024 ** 3, usage.total / 1024 ** 3 + + def free_gb(path: Path) -> float: - return shutil.disk_usage(path).free / 1024 ** 3 + return disk_gb(path)[0] def host_misread(returncode: int, text: str) -> bool: diff --git a/scripts/devtools/diskcleanup.py b/scripts/devtools/diskcleanup.py index 7ed493067..795c7ac5b 100644 --- a/scripts/devtools/diskcleanup.py +++ b/scripts/devtools/diskcleanup.py @@ -12,6 +12,7 @@ from dataclasses import dataclass from pathlib import Path from core.generated import is_generated +from core.hostguard import disk_gb from core.paths import AUTORUN_WORKTREE # What tmp/ keeps whatever its age, by name pattern, with the reason printed. @@ -28,7 +29,6 @@ KEPT_TMP: dict[str, str] = { # The CI guardian's log, scripts and the runs it has already seen. "ci_guardian*": "CI guardian state", } -ALERT_GB = 100 @dataclass(frozen=True) @@ -189,8 +189,7 @@ def apply(repo: Path, decisions: Iterable[Decision]) -> list[str]: def disk_line(path: Path, alert_gb: float) -> tuple[str, bool]: - usage = shutil.disk_usage(path) - free, total = usage.free / 1024 ** 3, usage.total / 1024 ** 3 + free, total = disk_gb(path) low = free < alert_gb line = f"disk: {free:.0f} GB free of {total:.0f} GB" if low: diff --git a/scripts/unattended/runner.py b/scripts/unattended/runner.py index ee4645a9b..4194841b4 100644 --- a/scripts/unattended/runner.py +++ b/scripts/unattended/runner.py @@ -18,7 +18,7 @@ from dataclasses import dataclass, field from pathlib import Path from core.artifacts import TIMESTAMP_KEYS -from core.hostguard import LockBusy, free_gb, global_lock, host_misread, lock_busy +from core.hostguard import LOW_DISK_GB, LockBusy, free_gb, global_lock, host_misread, lock_busy from core.paths import AUTORUN_DIR, AUTORUN_WORKTREE from core.statefile import now_iso, save_state from unattended.breaker import may_push, refresh_breaker @@ -35,7 +35,6 @@ from unattended.worktree import ( ) -ALERT_FREE_GB = 100 LOG_DAYS = 30 @@ -382,8 +381,8 @@ class Runner: def execute(self) -> tuple[str, str]: ctx, job = self.ctx, self.job free = free_gb(ctx.repo) - set_alert(self.state, "disk", free < ALERT_FREE_GB, - f"{free:.0f} GB free, under {ALERT_FREE_GB} GB", now_iso()) + set_alert(self.state, "disk", free < LOW_DISK_GB, + f"{free:.0f} GB free, under {LOW_DISK_GB} GB", now_iso()) if free < job.min_free_gb: return REFUSED, f"{free:.0f} GB free, {job.name} needs {job.min_free_gb:.0f} GB" if not wait_for_load(ctx.host, job.max_load_wait):