Spaces:
Running
Running
zhangjianfei09 commited on
Commit ·
f762836
1
Parent(s): 4fe02a1
debug server
Browse files- Dockerfile +13 -3
- config.py +50 -0
- healthcheck.py +84 -0
- start.sh +2 -1
Dockerfile
CHANGED
|
@@ -7,13 +7,23 @@ RUN ls -l /home/user/ && \
|
|
| 7 |
git clone https://github.com/project-numina/kimina-lean-server.git
|
| 8 |
|
| 9 |
WORKDIR /home/user/app/kimina-lean-server
|
| 10 |
-
RUN git checkout
|
| 11 |
ENV LEAN_SERVER_REPL_PATH=/home/user/repl/.lake/build/bin/repl
|
| 12 |
ENV LEAN_SERVER_PROJECT_DIR=/home/user/mathlib4
|
| 13 |
ENV PYTHONPATH=$PYTHONPATH:/home/user/app/kimina-lean-server
|
| 14 |
-
RUN python3 -m pip install --
|
| 15 |
-
python3 -m pip install -
|
|
|
|
|
|
|
| 16 |
RUN cp /home/user/app/.env /home/user/app/kimina-lean-server/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
WORKDIR /home/user/app
|
| 19 |
EXPOSE 7860
|
|
|
|
| 7 |
git clone https://github.com/project-numina/kimina-lean-server.git
|
| 8 |
|
| 9 |
WORKDIR /home/user/app/kimina-lean-server
|
| 10 |
+
RUN git checkout e3bb327e9f1a737decf24ecd861662f6a5f3f801 --detach
|
| 11 |
ENV LEAN_SERVER_REPL_PATH=/home/user/repl/.lake/build/bin/repl
|
| 12 |
ENV LEAN_SERVER_PROJECT_DIR=/home/user/mathlib4
|
| 13 |
ENV PYTHONPATH=$PYTHONPATH:/home/user/app/kimina-lean-server
|
| 14 |
+
RUN python3 -m pip install --upgrade pip --extra-index-url https://pypi.org/simple && \
|
| 15 |
+
python3 -m pip install -r requirements.txt --extra-index-url https://pypi.org/simple && \
|
| 16 |
+
python3 -m pip install . --extra-index-url https://pypi.org/simple && \
|
| 17 |
+
prisma generate
|
| 18 |
RUN cp /home/user/app/.env /home/user/app/kimina-lean-server/
|
| 19 |
+
ENV PATH="/home/user/.elan/bin:${PATH}"
|
| 20 |
+
RUN which lake && \
|
| 21 |
+
which elan && \
|
| 22 |
+
lake --version && \
|
| 23 |
+
elan --version && \
|
| 24 |
+
lean --version && \
|
| 25 |
+
elan toolchain list && \
|
| 26 |
+
echo ${PATH}
|
| 27 |
|
| 28 |
WORKDIR /home/user/app
|
| 29 |
EXPOSE 7860
|
config.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class Settings:
|
| 6 |
+
"""Global settings for the application"""
|
| 7 |
+
|
| 8 |
+
# Server configuration
|
| 9 |
+
HOST: str = os.getenv("LEANSERVER_HOST", "0.0.0.0")
|
| 10 |
+
PORT: int = int(os.getenv("LEANSERVER_PORT", 8888))
|
| 11 |
+
API_KEY: Optional[str] = os.getenv("LEANSERVER_API_KEY")
|
| 12 |
+
|
| 13 |
+
# Logging configuration
|
| 14 |
+
LOG_DIR: str = os.getenv("LEANSERVER_LOG_DIR", "./logs")
|
| 15 |
+
LOG_LEVEL: str = os.getenv("LEANSERVER_LOG_LEVEL", "INFO")
|
| 16 |
+
|
| 17 |
+
# Workspace configuration
|
| 18 |
+
WORKSPACE: Optional[str] = os.getenv("LEANSERVER_WORKSPACE")
|
| 19 |
+
|
| 20 |
+
# Concurrency configuration
|
| 21 |
+
MAX_REPLS: int = int(os.getenv("LEANSERVER_MAX_REPLS", 1))
|
| 22 |
+
MAX_CONCURRENT_REQUESTS: int = int(os.getenv("LEANSERVER_MAX_CONCURRENT_REQUESTS", 1))
|
| 23 |
+
|
| 24 |
+
# Health check thresholds
|
| 25 |
+
HEALTHCHECK_CPU_USAGE_THRESHOLD: Optional[float] = (
|
| 26 |
+
float(os.getenv("LEANSERVER_HEALTHCHECK_CPU_USAGE_THRESHOLD"))
|
| 27 |
+
if os.getenv("LEANSERVER_HEALTHCHECK_CPU_USAGE_THRESHOLD")
|
| 28 |
+
else None
|
| 29 |
+
)
|
| 30 |
+
HEALTHCHECK_MEMORY_USAGE_THRESHOLD: Optional[float] = (
|
| 31 |
+
float(os.getenv("LEANSERVER_HEALTHCHECK_MEMORY_USAGE_THRESHOLD"))
|
| 32 |
+
if os.getenv("LEANSERVER_HEALTHCHECK_MEMORY_USAGE_THRESHOLD")
|
| 33 |
+
else None
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# REPL memory management
|
| 37 |
+
REPL_MEMORY_LIMIT_GB: Optional[float] = (
|
| 38 |
+
float(os.getenv("LEANSERVER_REPL_MEMORY_LIMIT_GB"))
|
| 39 |
+
if os.getenv("LEANSERVER_REPL_MEMORY_LIMIT_GB")
|
| 40 |
+
else None
|
| 41 |
+
)
|
| 42 |
+
REPL_MEMORY_CHECK_INTERVAL: Optional[int] = (
|
| 43 |
+
int(os.getenv("LEANSERVER_REPL_MEMORY_CHECK_INTERVAL"))
|
| 44 |
+
if os.getenv("LEANSERVER_REPL_MEMORY_CHECK_INTERVAL")
|
| 45 |
+
else None
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
settings = Settings()
|
| 50 |
+
|
healthcheck.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import psutil
|
| 4 |
+
import subprocess
|
| 5 |
+
from loguru import logger
|
| 6 |
+
|
| 7 |
+
from .config import settings
|
| 8 |
+
from .server import require_access_dep
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_lean_version() -> str:
|
| 14 |
+
"""Get Lean 4 version"""
|
| 15 |
+
try:
|
| 16 |
+
result = subprocess.run(
|
| 17 |
+
["lean", "--version"],
|
| 18 |
+
capture_output=True,
|
| 19 |
+
text=True,
|
| 20 |
+
timeout=10
|
| 21 |
+
)
|
| 22 |
+
if result.returncode == 0:
|
| 23 |
+
return result.stdout.strip()
|
| 24 |
+
else:
|
| 25 |
+
return "unknown"
|
| 26 |
+
except Exception as e:
|
| 27 |
+
logger.warning(f"Failed to get lean version: {e}")
|
| 28 |
+
return "unknown"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_cpu_usage() -> Optional[float]:
|
| 32 |
+
"""Get current CPU usage percentage"""
|
| 33 |
+
try:
|
| 34 |
+
return psutil.cpu_percent(interval=1)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.warning(f"Failed to get CPU usage: {e}")
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_memory_usage() -> Optional[float]:
|
| 41 |
+
"""Get current memory usage percentage"""
|
| 42 |
+
try:
|
| 43 |
+
memory = psutil.virtual_memory()
|
| 44 |
+
return memory.percent
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.warning(f"Failed to get memory usage: {e}")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def check_health_thresholds(cpu_usage: Optional[float], memory_usage: Optional[float]) -> bool:
|
| 51 |
+
"""Check if CPU and memory usage are within thresholds"""
|
| 52 |
+
if settings.HEALTHCHECK_CPU_USAGE_THRESHOLD is not None and cpu_usage is not None:
|
| 53 |
+
if cpu_usage > settings.HEALTHCHECK_CPU_USAGE_THRESHOLD:
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
if settings.HEALTHCHECK_MEMORY_USAGE_THRESHOLD is not None and memory_usage is not None:
|
| 57 |
+
if memory_usage > settings.HEALTHCHECK_MEMORY_USAGE_THRESHOLD:
|
| 58 |
+
return False
|
| 59 |
+
|
| 60 |
+
return True
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@router.get("/health")
|
| 64 |
+
async def health_check(access: None = Depends(require_access_dep)):
|
| 65 |
+
"""Health check endpoint"""
|
| 66 |
+
lean_version = get_lean_version()
|
| 67 |
+
cpu_usage = get_cpu_usage()
|
| 68 |
+
memory_usage = get_memory_usage()
|
| 69 |
+
|
| 70 |
+
is_healthy = check_health_thresholds(cpu_usage, memory_usage)
|
| 71 |
+
|
| 72 |
+
if not is_healthy:
|
| 73 |
+
raise HTTPException(
|
| 74 |
+
status_code=503,
|
| 75 |
+
detail=f"Service unhealthy: lean_version={lean_version}, cpu_usage={cpu_usage}, memory_usage={memory_usage}"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return {
|
| 79 |
+
"status": "healthy",
|
| 80 |
+
"lean_version": lean_version,
|
| 81 |
+
"cpu_usage": cpu_usage,
|
| 82 |
+
"memory_usage": memory_usage
|
| 83 |
+
}
|
| 84 |
+
|
start.sh
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
#!/bin/bash
|
| 2 |
set -e
|
| 3 |
|
| 4 |
-
export PATH="~/.elan/bin:${PATH}"
|
| 5 |
which lake
|
| 6 |
which elan
|
| 7 |
lake --version
|
|
@@ -41,6 +40,8 @@ echo "curl -v http://localhost:8888/docs"
|
|
| 41 |
curl -v http://localhost:8888/docs
|
| 42 |
echo "curl -v http://localhost:8888/verify"
|
| 43 |
curl -v http://localhost:8888/verify
|
|
|
|
|
|
|
| 44 |
|
| 45 |
cd /home/user/app
|
| 46 |
ls -l
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
set -e
|
| 3 |
|
|
|
|
| 4 |
which lake
|
| 5 |
which elan
|
| 6 |
lake --version
|
|
|
|
| 40 |
curl -v http://localhost:8888/docs
|
| 41 |
echo "curl -v http://localhost:8888/verify"
|
| 42 |
curl -v http://localhost:8888/verify
|
| 43 |
+
curl -v http://localhost:8888/openapi.json
|
| 44 |
+
curl -v http://localhost:8888/redoc
|
| 45 |
|
| 46 |
cd /home/user/app
|
| 47 |
ls -l
|