Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s

This commit is contained in:
2026-11-19 12:42:03 +01:00
parent 8ca612b7a4
commit cb3135bc8c
124 changed files with 1437 additions and 793 deletions
+7 -5
View File
@@ -28,15 +28,17 @@ runs its command at once, so a gate that wraps its own steps cannot wait on itse
As GNU env, nice and timeout do, it exits 125, saying why, when no command is given or the slot
folder or a slot file cannot be made or locked; 126 when the command is found but cannot run; and 127 when it is not found.
"""
import fcntl
import os
import pathlib
import shutil
import sys
import time
import typing
from run_heavy.capped_docker import CANNOT_RUN, NOT_FOUND, PATH_ENTRY, SHARE_VARIABLE, without_path_entry
from failure import Failure
from machine_load.slots import HELD, slot_count, slot_folder, waited_slot
from machine_load.slots import HELD, Lock, slot_count, slot_folder, waited_slot
from machine_load.memory import Memory
OWN_FAILURE = 125
@@ -51,7 +53,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], cpus: int | None, memor
print(f"run-heavy: no command given; {USAGE}", file=sys.stderr)
return OWN_FAILURE
if HELD not in env:
slot = held_slot(slot_folder(env, pathlib.Path.home()), slot_count(cpus))
slot = held_slot(slot_folder(env, pathlib.Path.home()), slot_count(cpus), time.sleep, fcntl.flock)
if isinstance(slot, Failure):
print(f"run-heavy: {slot.reason}", file=sys.stderr)
return OWN_FAILURE
@@ -67,11 +69,11 @@ def capping(memory: Memory | None, slots: int, env: typing.Mapping[str, str]) ->
return {SHARE_VARIABLE: str(memory.slot_share(slots)), "PATH": os.pathsep.join([str(PATH_ENTRY), path])}
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."""
def held_slot(folder: pathlib.Path, slots: int, sleep: typing.Callable[[float], None], lock: Lock) -> 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; it waits for one with sleep, and locks it with lock."""
try:
folder.mkdir(parents=True, exist_ok=True)
slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for a heavy slot, of {slots} on this machine, in {folder}; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True))
slot, fd = waited_slot(folder, slots, lambda: print(f"run-heavy: waiting for a heavy slot, of {slots} on this machine, in {folder}; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True), sleep, lock)
except OSError as failure:
return Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}")
os.set_inheritable(fd, True)