From f588cdc5df778f6cd425fa3ed88f5c4b056337a4 Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 28 Feb 2025 16:50:54 +0800 Subject: [PATCH] Optimize logging config & worker handling for different server modes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Separate logging config for uvicorn/gunicorn • Force workers=1 in uvicorn mode • Add warning for worker count in uvicorn --- lightrag/api/lightrag_server.py | 15 ++++----------- lightrag/api/utils_api.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index 33a03cbc..f130a0fa 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -414,9 +414,6 @@ def create_app(args): def get_application(): """Factory function for creating the FastAPI application""" - # Configure logging for this worker process - configure_logging() - # Get args from environment variable args_json = os.environ.get("LIGHTRAG_ARGS") if not args_json: @@ -430,11 +427,7 @@ def get_application(): def configure_logging(): - """Configure logging for both uvicorn and lightrag""" - # Check if running under Gunicorn - if "GUNICORN_CMD_ARGS" in os.environ: - # If started with Gunicorn, return directly as Gunicorn will handle logging - return + """Configure logging for uvicorn startup""" # Reset any existing handlers to ensure clean configuration for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]: @@ -517,13 +510,13 @@ def main(): freeze_support() + # Configure logging before parsing args + configure_logging() + args = parse_args() # Save args to environment variable for child processes os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args)) - # Configure logging before starting uvicorn - configure_logging() - display_splash_screen(args) # Create application instance directly instead of using factory function diff --git a/lightrag/api/utils_api.py b/lightrag/api/utils_api.py index c494101c..f63e9c92 100644 --- a/lightrag/api/utils_api.py +++ b/lightrag/api/utils_api.py @@ -6,6 +6,7 @@ import os import argparse from typing import Optional import sys +import logging from ascii_colors import ASCIIColors from lightrag.api import __api_version__ from fastapi import HTTPException, Security @@ -286,6 +287,16 @@ def parse_args() -> argparse.Namespace: args = parser.parse_args() + # Check if running under uvicorn mode (not Gunicorn) + is_uvicorn_mode = "GUNICORN_CMD_ARGS" not in os.environ + + # If in uvicorn mode and workers > 1, force it to 1 and log warning + if is_uvicorn_mode and args.workers > 1: + original_workers = args.workers + args.workers = 1 + # Log warning directly here + logging.warning(f"In uvicorn mode, workers parameter was set to {original_workers}. Forcing workers=1") + # convert relative path to absolute path args.working_dir = os.path.abspath(args.working_dir) args.input_dir = os.path.abspath(args.input_dir)