Optimize logging config & worker handling for different server modes

• Separate logging config for uvicorn/gunicorn
• Force workers=1 in uvicorn mode
• Add warning for worker count in uvicorn
This commit is contained in:
yangdx
2025-02-28 16:50:54 +08:00
parent 81f6f6e343
commit f588cdc5df
2 changed files with 15 additions and 11 deletions

View File

@@ -414,9 +414,6 @@ def create_app(args):
def get_application(): def get_application():
"""Factory function for creating the FastAPI application""" """Factory function for creating the FastAPI application"""
# Configure logging for this worker process
configure_logging()
# Get args from environment variable # Get args from environment variable
args_json = os.environ.get("LIGHTRAG_ARGS") args_json = os.environ.get("LIGHTRAG_ARGS")
if not args_json: if not args_json:
@@ -430,11 +427,7 @@ def get_application():
def configure_logging(): def configure_logging():
"""Configure logging for both uvicorn and lightrag""" """Configure logging for uvicorn startup"""
# Check if running under Gunicorn
if "GUNICORN_CMD_ARGS" in os.environ:
# If started with Gunicorn, return directly as Gunicorn will handle logging
return
# Reset any existing handlers to ensure clean configuration # Reset any existing handlers to ensure clean configuration
for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]: for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]:
@@ -517,13 +510,13 @@ def main():
freeze_support() freeze_support()
# Configure logging before parsing args
configure_logging()
args = parse_args() args = parse_args()
# Save args to environment variable for child processes # Save args to environment variable for child processes
os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args)) os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args))
# Configure logging before starting uvicorn
configure_logging()
display_splash_screen(args) display_splash_screen(args)
# Create application instance directly instead of using factory function # Create application instance directly instead of using factory function

View File

@@ -6,6 +6,7 @@ import os
import argparse import argparse
from typing import Optional from typing import Optional
import sys import sys
import logging
from ascii_colors import ASCIIColors from ascii_colors import ASCIIColors
from lightrag.api import __api_version__ from lightrag.api import __api_version__
from fastapi import HTTPException, Security from fastapi import HTTPException, Security
@@ -286,6 +287,16 @@ def parse_args() -> argparse.Namespace:
args = parser.parse_args() 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 # convert relative path to absolute path
args.working_dir = os.path.abspath(args.working_dir) args.working_dir = os.path.abspath(args.working_dir)
args.input_dir = os.path.abspath(args.input_dir) args.input_dir = os.path.abspath(args.input_dir)