Answer stability round 1: 0 🔴, 5 🟡; test the lock-error and slot-folder exits and the default folder, and return a Failure from held_slot

This commit is contained in:
2026-10-23 11:06:55 +02:00
parent dde9caa172
commit 792fa2b8b0
3 changed files with 30 additions and 4 deletions
+5 -4
View File
@@ -20,6 +20,7 @@ import pathlib
import sys
import typing
from failure import Failure
from machine_load.slots import slot_count, slot_folder, waited_slot
# Exit codes for a command run-heavy never ran, apart from any the command exits with, as GNU env, nice and timeout use them.
@@ -40,20 +41,20 @@ def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None) -> in
return OWN_FAILURE
if HELD not in env:
slot = held_slot(slot_folder(env), slot_count(cpus))
if isinstance(slot, str):
print(f"run-heavy: {slot}", file=sys.stderr)
if isinstance(slot, Failure):
print(f"run-heavy: {slot.reason}", file=sys.stderr)
return OWN_FAILURE
env = {**env, HELD: str(slot)}
return run(argv, env)
def held_slot(folder: pathlib.Path, slots: int) -> int | str:
def held_slot(folder: pathlib.Path, slots: int) -> int | Failure:
"""The number of the slot this call holds, its lock left for the command to inherit, or why no slot could be taken."""
try:
folder.mkdir(parents=True, exist_ok=True)
slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for one of the machine's {slots} heavy slots", file=sys.stderr, flush=True))
except OSError as failure:
return f"cannot take a heavy slot in {folder}: {failure.strerror}"
return Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}")
# The command inherits the locked file, so the slot stays held until it exits.
os.set_inheritable(fd, True)
return slot