Upload files to "/"
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Manage existing Kronic Krusaders FastAPI as a child of Kronic Media.
|
||||
|
||||
No edits to the existing LiveKit Docker configuration.
|
||||
"""
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
CHAT_DIR = ROOT / "kronic-chat"
|
||||
|
||||
class ChatManager:
|
||||
def __init__(self):
|
||||
self._stop = threading.Event()
|
||||
self._thread = None
|
||||
self._proc = None
|
||||
|
||||
def start(self):
|
||||
if self._thread and self._thread.is_alive():
|
||||
return
|
||||
self._stop.clear()
|
||||
self._thread = threading.Thread(target=self._supervise, name="kronic-chat-supervisor", daemon=True)
|
||||
self._thread.start()
|
||||
|
||||
def _supervise(self):
|
||||
if not (CHAT_DIR / '.env').exists():
|
||||
print('[CHAT] No kronic-chat/.env yet. Chat not started.', flush=True)
|
||||
return
|
||||
py = CHAT_DIR / '.venv' / 'bin' / 'python'
|
||||
python = str(py) if py.is_file() else sys.executable
|
||||
port = int(os.getenv('KRONIC_CHAT_PORT', '8001'))
|
||||
env = os.environ.copy()
|
||||
# Do not let Chat fall back to its historical independent accounts DB.
|
||||
# Media's resolved path is the single account/room permission source.
|
||||
from media_accounts import DATABASE_PATH as shared_accounts_path
|
||||
env['KRONICK_ACCOUNT_DB'] = str(shared_accounts_path)
|
||||
# Existing server.py calls load_dotenv(), using the chat cwd.
|
||||
env['PYTHONUNBUFFERED'] = '1'
|
||||
while not self._stop.is_set():
|
||||
command = [python, '-m', 'uvicorn', 'server:app', '--host', '127.0.0.1', '--port', str(port)]
|
||||
try:
|
||||
self._proc = subprocess.Popen(command, cwd=str(CHAT_DIR), env=env,
|
||||
start_new_session=True)
|
||||
print(f'[CHAT] Started PID {self._proc.pid} on 127.0.0.1:{port}', flush=True)
|
||||
code = self._proc.wait()
|
||||
self._proc = None
|
||||
if not self._stop.is_set():
|
||||
print(f'[CHAT] Exited with {code}; retrying in 5 seconds', flush=True)
|
||||
self._stop.wait(5)
|
||||
except Exception as exc:
|
||||
print(f'[CHAT] Start error: {exc!r}', flush=True)
|
||||
self._stop.wait(5)
|
||||
|
||||
def stop(self):
|
||||
self._stop.set()
|
||||
proc = self._proc
|
||||
if proc is not None and proc.poll() is None:
|
||||
try:
|
||||
os.killpg(proc.pid, signal.SIGTERM)
|
||||
proc.wait(timeout=8)
|
||||
except subprocess.TimeoutExpired:
|
||||
os.killpg(proc.pid, signal.SIGKILL)
|
||||
proc.wait(timeout=5)
|
||||
except ProcessLookupError:
|
||||
pass
|
||||
if self._thread and self._thread.is_alive():
|
||||
self._thread.join(timeout=10)
|
||||
|
||||
chat_manager = ChatManager()
|
||||
Reference in New Issue
Block a user