optimize py control service

This commit is contained in:
team2
2026-02-22 09:00:06 +01:00
parent 4dfed5a797
commit 06376e0fb4
3 changed files with 569 additions and 251 deletions

View File

@@ -4,13 +4,27 @@ import argparse
import importlib
import json
import os
import signal
import socket
import subprocess
import sys
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple
BASE_PATH = Path(__file__).resolve().parents[2]
KNOWLEDGE_DIR = BASE_PATH / "var" / "knowledge"
VENV_DIR = BASE_PATH / ".venv"
VENV_PY = VENV_DIR / "bin" / "python"
VENV_PIP = VENV_DIR / "bin" / "pip"
UVICORN_BIN = VENV_DIR / "bin" / "uvicorn"
PID_DIR = BASE_PATH / "var" / "run"
PID_FILE = PID_DIR / "vector_service.pid"
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = 8090
DEFAULT_HEALTH_URL = "http://127.0.0.1:{port}/health"
DEFAULT_RELOAD_URL = "http://127.0.0.1:{port}/reload"
REQUIRED_MODULES = [
"fastapi",
@@ -20,11 +34,78 @@ REQUIRED_MODULES = [
"numpy",
]
VENV_PIP = BASE_PATH / ".venv" / "bin" / "pip"
UVICORN_BIN = BASE_PATH / ".venv" / "bin" / "uvicorn"
# If you want pinning later, do it here. For now keep simple.
INSTALL_PACKAGES = [
"fastapi",
"uvicorn",
"numpy",
"sentence-transformers",
# faiss: depending on your env, it might be "faiss-cpu" (pip) or system package.
# Don't force install unless missing import "faiss" and you opt-in via --install.
"faiss-cpu",
]
def check_modules():
def _now_ms() -> int:
return int(time.time() * 1000)
def _read_pid() -> Optional[int]:
try:
if PID_FILE.exists():
content = PID_FILE.read_text(encoding="utf-8").strip()
if content.isdigit():
return int(content)
except Exception:
return None
return None
def _write_pid(pid: int) -> None:
PID_DIR.mkdir(parents=True, exist_ok=True)
PID_FILE.write_text(str(pid), encoding="utf-8")
def _remove_pid() -> None:
try:
if PID_FILE.exists():
PID_FILE.unlink()
except Exception:
pass
def _pid_is_running(pid: int) -> bool:
try:
os.kill(pid, 0)
return True
except Exception:
return False
def _is_port_open(host: str, port: int, timeout: float = 0.3) -> bool:
try:
with socket.create_connection((host, port), timeout=timeout):
return True
except Exception:
return False
def _curl(url: str, timeout_seconds: int = 2) -> Tuple[int, str]:
# We use curl because it's usually available in your container;
# if you prefer, we can switch to urllib.
cmd = ["curl", "-s", "-m", str(timeout_seconds), "-w", "\n%{http_code}", url]
p = subprocess.run(cmd, capture_output=True, text=True)
out = (p.stdout or "").rstrip("\n")
if "\n" in out:
body, code = out.rsplit("\n", 1)
try:
return int(code), body
except Exception:
return 0, body
return 0, out
def check_modules() -> List[str]:
missing = []
for module in REQUIRED_MODULES:
try:
@@ -34,96 +115,228 @@ def check_modules():
return missing
def install_modules(modules):
if not modules:
return
subprocess.call([str(VENV_PIP), "install", *modules])
def install_missing_modules(missing: List[str]) -> Dict[str, str]:
# map missing module names to pip packages (faiss -> faiss-cpu, sentence_transformers -> sentence-transformers)
mod_to_pkg = {
"fastapi": "fastapi",
"uvicorn": "uvicorn",
"numpy": "numpy",
"sentence_transformers": "sentence-transformers",
"faiss": "faiss-cpu",
}
pkgs = []
for m in missing:
pkgs.append(mod_to_pkg.get(m, m))
if not VENV_PIP.exists():
return {"status": "error", "detail": "pip not found in .venv"}
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 or "pip install failed").strip()}
return {"status": "ok", "detail": "installed: " + " ".join(pkgs)}
def service_running():
result = subprocess.run(
["ps", "aux"],
capture_output=True,
text=True
)
return "uvicorn src.Vector.vector_service:app" in result.stdout
def service_status(port: int) -> Dict:
pid = _read_pid()
pid_running = bool(pid and _pid_is_running(pid))
# if pid file is stale, remove it
if pid and not pid_running:
_remove_pid()
pid = None
health_code, health_body = _curl(DEFAULT_HEALTH_URL.format(port=port), timeout_seconds=2)
health_ok = health_code == 200 and health_body.strip() != ""
def start_service():
subprocess.Popen([
str(UVICORN_BIN),
"src.Vector.vector_service:app",
"--host", "0.0.0.0",
"--port", "8090"
])
time.sleep(2)
def reload_service():
subprocess.call([
"curl",
"-s",
"-X",
"POST",
"http://127.0.0.1:8090/reload"
])
def health_check():
try:
result = subprocess.run(
["curl", "-s", "http://127.0.0.1:8090/health"],
capture_output=True,
text=True
)
return result.stdout.strip()
except Exception:
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--install", action="store_true")
parser.add_argument("--start", action="store_true")
parser.add_argument("--reload", action="store_true")
args = parser.parse_args()
result = {
"modules_missing": [],
"service_running": False,
"health": None,
"actions": []
return {
"pid_file": str(PID_FILE),
"pid": pid,
"pid_running": pid_running,
"health_code": health_code,
"health_body": health_body if len(health_body) <= 600 else health_body[:600] + "...",
"healthy": health_ok,
"port": port,
}
# 1⃣ Check modules
def start_service(host: str, port: int, background: bool, health_retries: int, health_wait_ms: int) -> Dict:
# already running?
st = service_status(port)
if st["pid_running"] and st["healthy"]:
return {"status": "ok", "detail": "already running", "status_info": st}
if not UVICORN_BIN.exists():
return {"status": "error", "detail": "uvicorn not found in .venv/bin/uvicorn"}
# If port already open but pidfile missing, we still consider it running; user can fix by stop with --force later
if _is_port_open("127.0.0.1", port):
# Try health anyway
st2 = service_status(port)
if st2["healthy"]:
return {"status": "ok", "detail": "port already in use but service healthy", "status_info": st2}
return {"status": "error", "detail": f"port {port} already in use, and /health not healthy"}
cmd = [
str(UVICORN_BIN),
"src.Vector.vector_service:app",
"--host", host,
"--port", str(port),
]
# production: no --reload
# run in background by default
if background:
p = subprocess.Popen(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=str(BASE_PATH),
start_new_session=True, # detach from terminal
)
_write_pid(p.pid)
else:
# foreground start (rare in production)
p = subprocess.Popen(cmd, cwd=str(BASE_PATH))
_write_pid(p.pid)
# wait for health
last = None
for _ in range(max(1, health_retries)):
time.sleep(health_wait_ms / 1000.0)
stx = service_status(port)
last = stx
if stx["healthy"]:
return {"status": "ok", "detail": "started", "status_info": stx}
return {"status": "error", "detail": "started but health not OK", "status_info": last}
def stop_service(port: int, force: bool = False, wait_seconds: int = 5) -> Dict:
pid = _read_pid()
if not pid:
# nothing to stop via pid; still check health
st = service_status(port)
if st["healthy"]:
return {"status": "error", "detail": "service healthy but no PID file (cannot stop safely)", "status_info": st}
return {"status": "ok", "detail": "not running"}
if not _pid_is_running(pid):
_remove_pid()
return {"status": "ok", "detail": "not running (stale pid removed)"}
# SIGTERM
try:
os.kill(pid, signal.SIGTERM)
except Exception as e:
if force:
try:
os.kill(pid, signal.SIGKILL)
_remove_pid()
return {"status": "ok", "detail": "killed (SIGKILL)"}
except Exception as e2:
return {"status": "error", "detail": f"failed to kill: {e2}"}
return {"status": "error", "detail": f"failed to stop: {e}"}
# wait for exit
end = time.time() + max(1, wait_seconds)
while time.time() < end:
if not _pid_is_running(pid):
_remove_pid()
return {"status": "ok", "detail": "stopped"}
time.sleep(0.2)
if force:
try:
os.kill(pid, signal.SIGKILL)
_remove_pid()
return {"status": "ok", "detail": "forced stop (SIGKILL)"}
except Exception as e:
return {"status": "error", "detail": f"failed to SIGKILL: {e}"}
return {"status": "error", "detail": "timeout stopping (use --force)"}
def reload_service(port: int) -> Dict:
code, body = _curl(DEFAULT_RELOAD_URL.format(port=port), timeout_seconds=5)
if code == 200 and "reloaded" in body:
return {"status": "ok", "detail": body}
if code == 404:
return {"status": "error", "detail": "reload endpoint not found (wrong service version?)"}
return {"status": "error", "detail": f"reload failed (http {code}): {body}"}
def main() -> int:
parser = argparse.ArgumentParser(description="Production-safe vector service control")
parser.add_argument("--install", action="store_true", help="Install missing python deps into .venv")
parser.add_argument("--start", action="store_true", help="Start service if not running")
parser.add_argument("--stop", action="store_true", help="Stop service using PID file")
parser.add_argument("--force", action="store_true", help="Force stop (SIGKILL) if needed")
parser.add_argument("--reload", action="store_true", help="Trigger /reload")
parser.add_argument("--status", action="store_true", help="Print status (default if no action)")
parser.add_argument("--port", type=int, default=DEFAULT_PORT)
parser.add_argument("--host", type=str, default=DEFAULT_HOST)
parser.add_argument("--foreground", action="store_true", help="Start in foreground (default background)")
parser.add_argument("--health-retries", type=int, default=20)
parser.add_argument("--health-wait-ms", type=int, default=250)
args = parser.parse_args()
started_ms = _now_ms()
out: Dict = {
"ts_ms": started_ms,
"base_path": str(BASE_PATH),
"venv_python": str(VENV_PY),
"pid_file": str(PID_FILE),
"actions": [],
"results": {},
}
# sanity: venv exists
if not VENV_PY.exists():
out["results"]["venv"] = {"status": "error", "detail": ".venv/bin/python not found"}
print(json.dumps(out, indent=2))
return 2
# 1) deps check
missing = check_modules()
result["modules_missing"] = missing
out["results"]["modules_missing"] = missing
if missing and args.install:
install_modules(missing)
result["actions"].append("modules_installed")
out["actions"].append("install")
out["results"]["install"] = install_missing_modules(missing)
# re-check after install
missing2 = check_modules()
out["results"]["modules_missing_after"] = missing2
# 2️⃣ Service check
running = service_running()
result["service_running"] = running
# 2) service actions
if args.stop:
out["actions"].append("stop")
out["results"]["stop"] = stop_service(args.port, force=args.force)
if not running and args.start:
start_service()
result["actions"].append("service_started")
running = True
if args.start:
out["actions"].append("start")
out["results"]["start"] = start_service(
host=args.host,
port=args.port,
background=not args.foreground,
health_retries=args.health_retries,
health_wait_ms=args.health_wait_ms,
)
# 3⃣ Reload
if args.reload:
reload_service()
result["actions"].append("service_reloaded")
out["actions"].append("reload")
out["results"]["reload"] = reload_service(args.port)
# 4⃣ Health
if running:
result["health"] = health_check()
# default: status (or if requested)
if args.status or (not args.install and not args.start and not args.stop and not args.reload):
out["actions"].append("status")
out["results"]["status"] = service_status(args.port)
print(json.dumps(result, indent=2))
out["duration_ms"] = _now_ms() - started_ms
print(json.dumps(out, indent=2))
return 0
if __name__ == "__main__":
main()
raise SystemExit(main())