refactor: one low disk threshold in hostguard

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 03:38:59 +01:00
1 parent 54bed5c235
commit 06e16510d5
4 files changed
+17 -10

No files matched your search

+3 -2
View File
@@ -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")
+9 -1
View File
@@ -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:
+2 -3
View File
@@ -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:
+3 -4
View File
@@ -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):