Files
rivencraft/scripts/server_test_lock.py
T
2026-11-14 12:17:49 -05:00

19 lines
602 B
Python

"""One managed temporary Minecraft server at a time on this host."""
from contextlib import contextmanager
import fcntl
from pathlib import Path
import tempfile
@contextmanager
def server_test_lock():
with (Path(tempfile.gettempdir()) / "rivencraft-server-tests.lock").open("a") as lock:
try:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
raise SystemExit("Another Rivencraft server test is active; finish it before starting this one")
try:
yield
finally:
fcntl.flock(lock, fcntl.LOCK_UN)