Files
infra-docker-configs/servers/tools-pve/asr-pipeline/asrpipe/locking.py
T
Codex 3c70861463
Build image / build (push) Canceled after 0s
Build ASR Pipeline / image (push) Canceled after 0s
feat(asr-pipeline): merge PR #1 webauth/CSRF/locking hardening and web_password fallback
2026-11-02 06:37:33 -08:00

26 lines
739 B
Python

"""One machine-wide lock for processes that consume the recording inbox."""
from __future__ import annotations
import fcntl
import os
from pathlib import Path
def acquire_recording_lock(path: Path):
"""Return an open file holding an exclusive lock, or exit if another consumer has it.
The handle must stay referenced for the life of the process.
"""
path.parent.mkdir(parents=True, exist_ok=True)
handle = path.open("a+")
try:
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
handle.close()
raise SystemExit(f"another recording consumer holds {path}")
handle.seek(0)
handle.truncate()
handle.write(str(os.getpid()))
handle.flush()
return handle