Answer architecture round 1: 0 🔴, 3 🟡; keep the slots and the memory share in machine_load, exit 125/126/127 as GNU wrappers do, point --help at the module

This commit is contained in:
2026-10-23 10:47:01 +02:00
parent 7c05bf414c
commit 66af309e71
12 changed files with 113 additions and 67 deletions
+14 -30
View File
@@ -12,17 +12,20 @@ it and every process that inherited the lock exit. Its exit code is the command'
A command it runs carries the slot's number in RUN_HEAVY_SLOT, and run-heavy called with that set
runs its command at once, so a gate that wraps its own steps cannot wait on itself.
Exit 1, saying why, when no command is given or the slot folder cannot be made; exit 127 when the
command cannot start.
As GNU env, nice and timeout do, it exits 125, saying why, when no command is given or the slot
folder cannot be made; 126 when the command is found but cannot run; and 127 when it is not found.
"""
import fcntl
import os
import pathlib
import sys
import time
import typing
CPUS_PER_SLOT = 4
from machine_load.slots import slot_count, slot_folder, taken_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.
OWN_FAILURE = 125
CANNOT_RUN = 126
NOT_FOUND = 127
# Names the slot a command runs in, so a heavy command it runs does not wait for a second slot.
HELD = "RUN_HEAVY_SLOT"
# Seconds between tries while every slot is held; a slot frees as its command exits.
@@ -30,42 +33,20 @@ POLL_SECONDS = 1
USAGE = "usage: run-heavy <command> [<arg>...]"
def slot_count(cpus: int | None) -> int:
return max(1, (cpus or 0) // CPUS_PER_SLOT)
def slot_folder(env: typing.Mapping[str, str]) -> pathlib.Path:
state = env.get("XDG_STATE_HOME") or str(pathlib.Path.home() / ".local" / "state")
return pathlib.Path(state) / "run-heavy"
def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
"""The number and open file of a slot this call locked; None while every slot is held."""
for slot in range(slots):
fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644)
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
os.close(fd)
continue
return slot, fd
return None
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
if not argv:
print(f"run-heavy: no command given; {USAGE}", file=sys.stderr)
return 1
return OWN_FAILURE
if HELD not in env:
folder = slot_folder(env)
try:
folder.mkdir(parents=True, exist_ok=True)
except OSError as failure:
print(f"run-heavy: cannot make the slot folder {folder}: {failure.strerror}", file=sys.stderr)
return 1
return OWN_FAILURE
slots = slot_count(os.cpu_count())
said = False
while (taken := taken_slot(folder, slots)) is None:
@@ -79,6 +60,9 @@ def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
env = {**env, HELD: str(slot)}
try:
os.execvpe(argv[0], argv, env)
except FileNotFoundError:
print(f"run-heavy: {argv[0]}: command not found", file=sys.stderr)
return NOT_FOUND
except OSError as failure:
print(f"run-heavy: cannot run {argv[0]}: {failure.strerror}", file=sys.stderr)
return 127
return CANNOT_RUN