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
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -8,16 +8,18 @@ adf-codec has two pairs of calls, and each function here names the one it runs:
|
||||
"""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import fcntl
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from machine_load.slots import HELD, slot_count, slot_folder, usable_cpus, waited_slot
|
||||
from machine_load.slots import HELD, Lock, slot_count, slot_folder, usable_cpus, waited_slot
|
||||
|
||||
SHIM = pathlib.Path(__file__).resolve().parent / "node_shim"
|
||||
SHIM_FILES = ("Dockerfile", "package.json", "package-lock.json", "shim.mjs")
|
||||
@@ -62,7 +64,13 @@ def docker_args(image: str, mode: str) -> list[str]:
|
||||
return ["docker", "run", "--rm", "-i", "--memory", MEMORY, "--memory-swap", MEMORY, "--network", "none", "--read-only", image, mode]
|
||||
|
||||
|
||||
def docker(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any] = subprocess.run) -> Ran | Failure:
|
||||
def docker(args: list[str], text: str, seconds: int) -> Ran | Failure:
|
||||
"""Runs a docker command line as a process of this one: the shim's shell, as git_repo.git is git's."""
|
||||
return docker_ran(args, text, seconds, subprocess.run)
|
||||
|
||||
|
||||
def docker_ran(args: list[str], text: str, seconds: int, call: typing.Callable[..., typing.Any]) -> Ran | Failure:
|
||||
"""What call, which runs a command line as subprocess.run does, made of a docker command line on text."""
|
||||
try:
|
||||
done = call(args, input=text, capture_output=True, text=True, encoding="utf-8", timeout=seconds)
|
||||
except FileNotFoundError:
|
||||
@@ -73,16 +81,22 @@ def docker(args: list[str], text: str, seconds: int, call: typing.Callable[...,
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = None) -> typing.Iterator[Failure | None]:
|
||||
"""Holds one of the machine's heavy slots, which run-heavy shares, for the with block. Under run-heavy the process already holds one, so it doesn't wait."""
|
||||
def machine_slot() -> typing.Iterator[Failure | None]:
|
||||
"""heavy_slot on this process's environment, home, processors, stderr and clock: the shim's shell, as docker is."""
|
||||
with heavy_slot(os.environ, pathlib.Path.home(), slot_count(usable_cpus()), lambda text: print(text, file=sys.stderr, flush=True), time.sleep, fcntl.flock) as refused:
|
||||
yield refused
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def heavy_slot(env: typing.Mapping[str, str], home: pathlib.Path, slots: int, say: typing.Callable[[str], None], sleep: typing.Callable[[float], None], lock: Lock) -> typing.Iterator[Failure | None]:
|
||||
"""Holds one of the machine's heavy slots, which run-heavy shares, for the with block, telling say where it waits for one. Under run-heavy the process already holds one, so it doesn't wait."""
|
||||
if HELD in env:
|
||||
yield None
|
||||
return
|
||||
folder = slot_folder(env, pathlib.Path.home())
|
||||
count = slot_count(usable_cpus()) if slots is None else slots
|
||||
folder = slot_folder(env, home)
|
||||
try:
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
_, fd = waited_slot(folder, count, lambda: print(f"adf-codec: waiting for a heavy slot, of {count} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them", file=sys.stderr, flush=True))
|
||||
_, fd = waited_slot(folder, slots, lambda: say(f"adf-codec: waiting for a heavy slot, of {slots} on this machine, in {folder}, to build the shim's image; `fuser {folder}/slot-*` names the processes that hold them"), sleep, lock)
|
||||
except OSError as failure:
|
||||
yield Failure(f"cannot take a heavy slot in {folder}: {failure.strerror}")
|
||||
return
|
||||
@@ -92,7 +106,7 @@ def heavy_slot(env: typing.Mapping[str, str] = os.environ, slots: int | None = N
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = heavy_slot) -> str | Failure:
|
||||
def ensure_image(shim: pathlib.Path, run_docker: Docker = docker, slot: Slot = machine_slot) -> str | Failure:
|
||||
"""The tag of the image for the shim folder, built from that folder first where this machine has no image under that tag.
|
||||
|
||||
The build waits for a heavy slot. Once this process holds the slot, it inspects again, as another process may have built the image meanwhile."""
|
||||
|
||||
Reference in New Issue
Block a user