44 lines
1.9 KiB
Python
44 lines
1.9 KiB
Python
"""The folder a bench keeps its runs' streams and its digest in, under the chunk's review folder, and the lock that says a bench
|
|
or a gather is using it. The streams are large and nothing reads them once gather has, so each bench removes the folders left
|
|
by benches that crashed or were never gathered."""
|
|
import contextlib
|
|
import fcntl
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from chunk_driver.claims import locked
|
|
|
|
FOLDER = "task-bench"
|
|
# Beside FOLDER, not in it, since a bench empties its folder. It marks FOLDER as a bench's, and stays once the folder is gone: compress-archives leaves a lock alone.
|
|
LOCK = "task-bench.lock"
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def held(review: pathlib.Path) -> typing.Iterator[pathlib.Path | Failure]:
|
|
"""Holds the bench folder of the review folder review, waiting for a bench or a gather that holds it, and yields its path;
|
|
a Failure, with no lock held, where its lock cannot be taken."""
|
|
with locked(review / LOCK) as unlocked:
|
|
yield unlocked or review / FOLDER
|
|
|
|
|
|
def clear_left(reports: pathlib.Path) -> None:
|
|
"""Removes each bench folder under reports that no process holds. One that cannot be locked or removed stays.
|
|
|
|
A bench that ended without a gather loses its folder to the next bench in any checkout. A folder doesn't say whether
|
|
someone still means to gather it, so every unheld one goes."""
|
|
for at, folders, files in os.walk(reports):
|
|
if LOCK not in files or FOLDER not in folders:
|
|
continue
|
|
# Don't walk into a bench's folder: its copies of whole trees are large, and a tree may hold such a pair of its own.
|
|
folders.remove(FOLDER)
|
|
try:
|
|
with open(os.path.join(at, LOCK), "a") as lock:
|
|
# A lock this process holds through `held` refuses too, so a bench leaves its own folder to its caller.
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
shutil.rmtree(os.path.join(at, FOLDER), ignore_errors=True)
|
|
except OSError:
|
|
continue
|