List every running loop in loop-inbox, with the ID and tagline of each item its chunk takes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""The lock a loop-claude holds on its worker's session while loop-inbox lists the session's question. loop-inbox reads a held lock as a loop that still runs.
|
||||
"""The locks by which loop-inbox tells that a loop-claude still runs.
|
||||
|
||||
The lock is <session>.lock, held with flock, which the kernel frees however the loop ends, killed included. The lock
|
||||
file stays once the loop ends: docs/decisions.md → `loop-inbox: …` says why.
|
||||
A loop holds <session>.lock while loop-inbox lists the session's question, and <session>.running.lock for its whole run
|
||||
(running_loops.py). Each is held with flock, which the kernel frees however the loop ends, killed included. A lock file
|
||||
stays once the loop ends: docs/decisions.md → `loop-inbox: …` says why.
|
||||
"""
|
||||
import contextlib
|
||||
import fcntl
|
||||
@@ -19,12 +20,18 @@ def lock_file(folder: pathlib.Path, session: str) -> pathlib.Path:
|
||||
@contextlib.contextmanager
|
||||
def locked(folder: pathlib.Path, session: str) -> typing.Iterator[Failure | None]:
|
||||
"""Holds the session's lock while the with block runs, or yields a Failure where it cannot. loop-inbox's drop_ended takes the lock for a moment on each ask still on disk, so a loop may find it held."""
|
||||
with held(folder, lock_file(folder, session)) as unlocked:
|
||||
yield unlocked
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def held(folder: pathlib.Path, path: pathlib.Path) -> typing.Iterator[Failure | None]:
|
||||
"""Holds the lock at path, a file in folder, while the with block runs, or yields a Failure where it cannot."""
|
||||
try:
|
||||
make_private(folder)
|
||||
except OSError as failure:
|
||||
yield Failure(f"cannot make {folder}: {failure.strerror}")
|
||||
return
|
||||
path = lock_file(folder, session)
|
||||
try:
|
||||
handle = open(path, "a")
|
||||
except OSError as failure:
|
||||
@@ -44,8 +51,13 @@ def locked(folder: pathlib.Path, session: str) -> typing.Iterator[Failure | None
|
||||
|
||||
def running(folder: pathlib.Path, session: str) -> bool:
|
||||
"""Whether a loop holds the session's lock; False also where the lock file cannot be opened or locked."""
|
||||
return taken(lock_file(folder, session))
|
||||
|
||||
|
||||
def taken(path: pathlib.Path) -> bool:
|
||||
"""Whether a process holds the lock at path; False also where it cannot be opened or locked."""
|
||||
try:
|
||||
with open(lock_file(folder, session)) as handle:
|
||||
with open(path) as handle:
|
||||
fcntl.flock(handle, fcntl.LOCK_SH | fcntl.LOCK_NB)
|
||||
except BlockingIOError:
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user