diff --git a/docker-compose-ollama.yml b/docker-compose-ollama.yml index 27a6cfc..ecc3b28 100644 --- a/docker-compose-ollama.yml +++ b/docker-compose-ollama.yml @@ -46,7 +46,7 @@ services: - ${LOCAL_FILES_PATH}:/usr/src/app/local_files/ - ./indexer:/usr/src/app ports: - - 8001:8000 + - 8001:8001 environment: - PYTHONPATH=/usr/src - PYTHONUNBUFFERED=TRUE @@ -54,6 +54,11 @@ services: - EMBEDDING_MODEL_ID=${EMBEDDING_MODEL_ID} - EMBEDDING_SIZE=${EMBEDDING_SIZE} - START_INDEXING=${START_INDEXING} + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8001/health"] + interval: 10s + timeout: 5s + retries: 3 depends_on: - qdrant @@ -66,7 +71,7 @@ services: volumes: - ./llm:/usr/src/app ports: - - 8003:8000 + - 8003:8003 environment: - PYTHONPATH=/usr/src - PYTHONUNBUFFERED=TRUE @@ -76,6 +81,7 @@ services: - ollama - qdrant - indexer + entrypoint: ["./wait-for-it.sh", "http://indexer:8001/health", "60", "--", "python", "app.py"] chat: build: ./chat diff --git a/indexer/Dockerfile b/indexer/Dockerfile index 3ef788c..3419191 100644 --- a/indexer/Dockerfile +++ b/indexer/Dockerfile @@ -16,7 +16,7 @@ COPY . . ENV START_INDEXING=${START_INDEXING} RUN echo "START_INDEXING is $START_INDEXING" -ENV PORT 8000 +ENV PORT 8001 ENV CURRENT_HOST 0.0.0.0 ENV WORKERS 1 diff --git a/indexer/app.py b/indexer/app.py index d3b46a5..754690b 100644 --- a/indexer/app.py +++ b/indexer/app.py @@ -5,6 +5,7 @@ from pydantic import BaseModel from async_queue import AsyncQueue from fastapi import FastAPI, APIRouter +from fastapi.responses import JSONResponse from contextlib import asynccontextmanager from async_loop import index_loop, crawl_loop @@ -20,6 +21,34 @@ class Query(BaseModel): query: str +@router.get( + "/health", + response_description="Health check", + status_code=200, +) +async def health(): + try: + result = indexer.find("Indexer healthy") + if not result: + return JSONResponse( + status_code=500, + content={"status": "error", "detail": "Indexer is not healthy"}, + ) + + return JSONResponse( + status_code=200, + content={"status": "ok", "detail": "Indexer is healthy"}, + ) + + except Exception as e: + logger.error(f"Error during health check: {e}") + + return JSONResponse( + status_code=500, + content={"status": "error", "detail": str(e)}, + ) + + @router.post( "/query", response_description='Query local data storage', diff --git a/llm/Dockerfile b/llm/Dockerfile index 19c9b51..b3d2537 100644 --- a/llm/Dockerfile +++ b/llm/Dockerfile @@ -10,6 +10,9 @@ RUN pip install huggingface_hub RUN huggingface-cli download $RERANKER_MODEL --repo-type model RUN pip install --no-cache-dir -r requirements.txt COPY . . +RUN apt-get update && apt-get install -y curl +COPY ./wait-for-it.sh /usr/local/bin/wait-for-it.sh +RUN chmod +x /usr/local/bin/wait-for-it.sh ENV PORT 8000 ENV CURRENT_HOST 0.0.0.0 diff --git a/llm/wait-for-it.sh b/llm/wait-for-it.sh new file mode 100755 index 0000000..6527c22 --- /dev/null +++ b/llm/wait-for-it.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e + +HOST=$1 +TIMEOUT=$2 + +if [ -z "$HOST" ] || [ -z "$TIMEOUT" ]; then + echo "Usage: $0 " + exit 1 +fi + +echo "Waiting for $HOST to return HTTP 200..." + +for ((i=1;i<=TIMEOUT;i++)); do + STATUS=$(curl -o /dev/null -s -w "%{http_code}" "$HOST" || echo "000") + if [ "$STATUS" -eq 200 ]; then + echo "Service $HOST is ready." + exit 0 + fi + echo "Service $HOST not ready, retrying ($i/$TIMEOUT)..." + sleep 1 +done + +echo "Timeout reached while waiting for $HOST" +exit 1