Files
VoiceStudio/backend/core/runtime_owner.py
mostafa-ismail-2004 4708ae9a51 feat(cli): add the voicestudio terminal command
Add a terminal command that drives the desktop app's local backend.

- The desktop app owns the backend. When no backend answers, the CLI
  starts the installed app in the background (no window); it publishes
  its address in backend.json and quits after 10 idle minutes. Opening
  the app later shows a window in the same process, so a terminal
  session and the GUI never run two backends.
- The backend reports activity (requests, jobs, downloads, installs,
  calls) at /system/activity so idle shutdown never interrupts work, and
  holds a kernel lock on its data directory so a second backend fails
  fast with an actionable message.
- `voicestudio stop` stops a background app; commands that only report
  state never start it, and `status` exits 3 when it is not running.
- Output is readable tables and fields chosen from each result's shape;
  `--json` prints the raw data and piped output is tab-separated.
- Tab completion for bash, zsh, fish and PowerShell
  (`voicestudio completion <shell>`), without building the command tree.
- `voicestudio update` opens the app's updater; the CLI itself makes no
  network requests.
- Settings → General shows the terminal command and can add it to PATH
  on request; shell profiles are only edited with that consent.
2026-11-05 18:18:58 +02:00

68 lines
2.4 KiB
Python

"""Allow one backend process per data directory.
The lock file is never removed: replacing it would let two processes lock
different inodes. Kernel locks, rather than saved PIDs, establish ownership, so
a crashed backend releases the directory with its process.
"""
from __future__ import annotations
import errno
import os
from pathlib import Path
import time
_CONTENDED = {errno.EAGAIN, errno.EACCES, getattr(errno, 'EWOULDBLOCK', errno.EAGAIN),
getattr(errno, 'EDEADLK', 36), 36}
class DataDirectoryBusy(RuntimeError):
pass
class DataDirectoryLease:
def __init__(self, directory: str | os.PathLike[str], timeout: float = 0) -> None:
self.directory = Path(directory)
self.timeout = timeout
self.stream = None
def __enter__(self) -> DataDirectoryLease:
self.directory.mkdir(parents=True, exist_ok=True)
fd = os.open(self.directory / '.runtime.lock', os.O_RDWR | os.O_CREAT, 0o600)
self.stream = os.fdopen(fd, 'r+b', buffering=0)
if os.fstat(fd).st_size == 0:
self.stream.write(b'\0')
deadline = time.monotonic() + self.timeout
while True:
try:
if os.name == 'nt':
import msvcrt
self.stream.seek(0)
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
else:
import fcntl
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return self
except OSError as exc:
if exc.errno not in _CONTENDED:
self.stream.close()
raise
if time.monotonic() >= deadline:
self.stream.close()
raise DataDirectoryBusy(
f'Another VoiceStudio backend is already using {self.directory}. '
'Quit VoiceStudio (or run "voicestudio stop"), or set OMNIVOICE_DATA_DIR '
'to a different folder, then start this backend again.') from None
time.sleep(.05)
def __exit__(self, *args: object) -> None:
try:
if os.name == 'nt':
import msvcrt
self.stream.seek(0)
msvcrt.locking(self.stream.fileno(), msvcrt.LK_UNLCK, 1)
else:
import fcntl
fcntl.flock(self.stream.fileno(), fcntl.LOCK_UN)
finally:
self.stream.close()