optimize py control

This commit is contained in:
team 1
2026-02-25 09:23:02 +01:00
parent 2a055b2f72
commit 4b5ad9b338
3 changed files with 57 additions and 12 deletions

1
.gitignore vendored
View File

@@ -7,6 +7,7 @@
/var/locks /var/locks
/var/agent-history /var/agent-history
/var/cache /var/cache
/var/run/*
.env.local .env.local
test.* test.*
# ---> Symfony # ---> Symfony

View File

@@ -18,7 +18,6 @@ from typing import Dict, List, Optional, Tuple
BASE_PATH = Path(__file__).resolve().parents[2] BASE_PATH = Path(__file__).resolve().parents[2]
VENV_DIR = BASE_PATH / ".venv" VENV_DIR = BASE_PATH / ".venv"
VENV_PY = VENV_DIR / "bin" / "python" VENV_PY = VENV_DIR / "bin" / "python"
VENV_PIP = VENV_DIR / "bin" / "pip"
UVICORN_BIN = VENV_DIR / "bin" / "uvicorn" UVICORN_BIN = VENV_DIR / "bin" / "uvicorn"
PID_DIR = BASE_PATH / "var" / "run" PID_DIR = BASE_PATH / "var" / "run"
@@ -112,9 +111,29 @@ def _curl(url: str, method: str = "GET", timeout_seconds: int = 3) -> Tuple[int,
# ============================================================ # ============================================================
# Dependency Handling # Dependency Handling (robust & self-healing)
# ============================================================ # ============================================================
def _venv_python_ok() -> bool:
return VENV_PY.exists() and os.access(VENV_PY, os.X_OK)
def _ensure_pip() -> subprocess.CompletedProcess:
return subprocess.run(
[str(VENV_PY), "-m", "ensurepip", "--upgrade"],
capture_output=True,
text=True,
)
def _pip_install(pkgs: List[str]) -> subprocess.CompletedProcess:
return subprocess.run(
[str(VENV_PY), "-m", "pip", "install", *pkgs],
capture_output=True,
text=True,
)
def check_modules() -> List[str]: def check_modules() -> List[str]:
missing = [] missing = []
for module in REQUIRED_MODULES: for module in REQUIRED_MODULES:
@@ -126,6 +145,12 @@ def check_modules() -> List[str]:
def install_missing_modules(missing: List[str]) -> Dict[str, str]: def install_missing_modules(missing: List[str]) -> Dict[str, str]:
if not _venv_python_ok():
return {
"status": "error",
"detail": "venv python not found or not executable"
}
mod_to_pkg = { mod_to_pkg = {
"fastapi": "fastapi", "fastapi": "fastapi",
"uvicorn": "uvicorn", "uvicorn": "uvicorn",
@@ -136,17 +161,29 @@ def install_missing_modules(missing: List[str]) -> Dict[str, str]:
pkgs = [mod_to_pkg.get(m, m) for m in missing] pkgs = [mod_to_pkg.get(m, m) for m in missing]
if not VENV_PIP.exists(): # Try normal pip install
return {"status": "error", "detail": "pip not found in .venv"} p = _pip_install(pkgs)
if p.returncode == 0:
cmd = [str(VENV_PIP), "install", *pkgs]
p = subprocess.run(cmd, capture_output=True, text=True)
if p.returncode != 0:
return {"status": "error", "detail": (p.stderr or p.stdout).strip()}
return {"status": "ok", "detail": "installed: " + " ".join(pkgs)} return {"status": "ok", "detail": "installed: " + " ".join(pkgs)}
# Attempt pip repair via ensurepip
ensure = _ensure_pip()
if ensure.returncode != 0:
return {
"status": "error",
"detail": "ensurepip failed: " + (ensure.stderr or ensure.stdout).strip()
}
# Retry install after repair
retry = _pip_install(pkgs)
if retry.returncode != 0:
return {
"status": "error",
"detail": (retry.stderr or retry.stdout).strip()
}
return {"status": "ok", "detail": "installed after pip repair: " + " ".join(pkgs)}
# ============================================================ # ============================================================
# Service Control # Service Control
@@ -173,14 +210,16 @@ def service_status(port: int) -> Dict:
def start_service(host: str, port: int) -> Dict: def start_service(host: str, port: int) -> Dict:
if not UVICORN_BIN.exists(): if not _venv_python_ok():
return {"status": "error", "detail": "uvicorn not found in .venv"} return {"status": "error", "detail": "venv python not executable"}
if _is_port_open("127.0.0.1", port): if _is_port_open("127.0.0.1", port):
return {"status": "error", "detail": f"port {port} already in use"} return {"status": "error", "detail": f"port {port} already in use"}
cmd = [ cmd = [
str(UVICORN_BIN), str(VENV_PY),
"-m",
"uvicorn",
"python.vector.vector_service:app", "python.vector.vector_service:app",
"--host", host, "--host", host,
"--port", str(port), "--port", str(port),

5
requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
fastapi
uvicorn
faiss-cpu
sentence-transformers
numpy