From c37b1e8aa7c7404075cde2a1261d0c0c903ae50f Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 28 Feb 2025 20:41:11 +0800 Subject: [PATCH] Align Gunicorn configuration with Uvicorn - centralize config in gunicorn_config.py - fix log level handling in Gunicorn --- gunicorn_config.py | 39 ++++++-------- lightrag/api/lightrag_server.py | 20 ++------ lightrag/api/utils_api.py | 8 +-- run_with_gunicorn.py | 90 ++++++++------------------------- 4 files changed, 47 insertions(+), 110 deletions(-) diff --git a/gunicorn_config.py b/gunicorn_config.py index daab1955..0ca6f9d9 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -2,17 +2,17 @@ import os import logging from lightrag.kg.shared_storage import finalize_share_data -from lightrag.api.utils_api import parse_args from lightrag.api.lightrag_server import LightragPathFilter -# Parse command line arguments -args = parse_args() +# 获取日志文件路径 +log_file_path = os.path.abspath(os.path.join(os.getcwd(), "lightrag.log")) -# Determine worker count - from environment variable or command line arguments -workers = int(os.getenv("WORKERS", args.workers)) - -# Binding address -bind = f"{os.getenv('HOST', args.host)}:{os.getenv('PORT', args.port)}" +# These variables will be set by run_with_gunicorn.py +workers = None +bind = None +loglevel = None +certfile = None +keyfile = None # Enable preload_app option preload_app = True @@ -24,18 +24,9 @@ worker_class = "uvicorn.workers.UvicornWorker" timeout = int(os.getenv("TIMEOUT", 120)) keepalive = 5 -# Optional SSL configuration -if args.ssl: - certfile = args.ssl_certfile - keyfile = args.ssl_keyfile - -# 获取日志文件路径 -log_file_path = os.path.abspath(os.path.join(os.getcwd(), "lightrag.log")) - # Logging configuration errorlog = os.getenv("ERROR_LOG", log_file_path) # 默认写入到 lightrag.log accesslog = os.getenv("ACCESS_LOG", log_file_path) # 默认写入到 lightrag.log -loglevel = os.getenv("LOG_LEVEL", "info") # 配置日志系统 logconfig_dict = { @@ -49,13 +40,11 @@ logconfig_dict = { 'handlers': { 'console': { 'class': 'logging.StreamHandler', - 'level': 'INFO', 'formatter': 'standard', 'stream': 'ext://sys.stdout' }, 'file': { 'class': 'logging.handlers.RotatingFileHandler', - 'level': 'INFO', 'formatter': 'standard', 'filename': log_file_path, 'maxBytes': 10485760, # 10MB @@ -71,22 +60,22 @@ logconfig_dict = { 'loggers': { 'lightrag': { 'handlers': ['console', 'file'], - 'level': 'INFO', + 'level': loglevel.upper() if loglevel else 'INFO', 'propagate': False }, 'gunicorn': { 'handlers': ['console', 'file'], - 'level': 'INFO', + 'level': loglevel.upper() if loglevel else 'INFO', 'propagate': False }, 'gunicorn.error': { 'handlers': ['console', 'file'], - 'level': 'INFO', + 'level': loglevel.upper() if loglevel else 'INFO', 'propagate': False }, 'gunicorn.access': { 'handlers': ['console', 'file'], - 'level': 'INFO', + 'level': loglevel.upper() if loglevel else 'INFO', 'propagate': False, 'filters': ['path_filter'] } @@ -143,6 +132,10 @@ def post_fork(server, worker): Executed after a worker has been forked. This is a good place to set up worker-specific configurations. """ + # Set lightrag logger level in worker processes using gunicorn's loglevel + from lightrag.utils import logger + logger.setLevel(loglevel.upper()) + # Disable uvicorn.error logger in worker processes uvicorn_error_logger = logging.getLogger("uvicorn.error") uvicorn_error_logger.setLevel(logging.CRITICAL) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index f130a0fa..8f7a6781 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -86,7 +86,7 @@ class LightragPathFilter(logging.Filter): def create_app(args): # Setup logging - logger.setLevel(getattr(logging, args.log_level)) + logger.setLevel(args.log_level) set_verbose_debug(args.verbose) # Verify that bindings are correctly setup @@ -412,17 +412,10 @@ def create_app(args): return app -def get_application(): +def get_application(args=None): """Factory function for creating the FastAPI application""" - # Get args from environment variable - args_json = os.environ.get("LIGHTRAG_ARGS") - if not args_json: - args = parse_args() # Fallback to parsing args if env var not set - else: - import types - - args = types.SimpleNamespace(**json.loads(args_json)) - + if args is None: + args = parse_args() return create_app(args) @@ -513,10 +506,7 @@ def main(): # 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)) - + args = parse_args(is_uvicorn_mode=True) 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 f63e9c92..4b5e0a28 100644 --- a/lightrag/api/utils_api.py +++ b/lightrag/api/utils_api.py @@ -111,10 +111,13 @@ def get_env_value(env_key: str, default: any, value_type: type = str) -> any: return default -def parse_args() -> argparse.Namespace: +def parse_args(is_uvicorn_mode: bool = False) -> argparse.Namespace: """ Parse command line arguments with environment variable fallback + Args: + is_uvicorn_mode: Whether running under uvicorn mode + Returns: argparse.Namespace: Parsed arguments """ @@ -287,9 +290,6 @@ 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 diff --git a/run_with_gunicorn.py b/run_with_gunicorn.py index 7b98cb1c..de2b21b6 100755 --- a/run_with_gunicorn.py +++ b/run_with_gunicorn.py @@ -30,45 +30,10 @@ def main(): # Register signal handlers for graceful shutdown signal.signal(signal.SIGINT, signal_handler) # Ctrl+C signal.signal(signal.SIGTERM, signal_handler) # kill command - # Create a parser to handle Gunicorn-specific parameters - parser = argparse.ArgumentParser(description="Start LightRAG server with Gunicorn") - parser.add_argument( - "--workers", - type=int, - help="Number of worker processes (overrides the default or config.ini setting)", - ) - parser.add_argument( - "--timeout", type=int, help="Worker timeout in seconds (default: 120)" - ) - parser.add_argument( - "--log-level", - choices=["debug", "info", "warning", "error", "critical"], - help="Gunicorn log level", - ) - # Parse Gunicorn-specific arguments - gunicorn_args, remaining_args = parser.parse_known_args() + # Parse all arguments using parse_args + args = parse_args(is_uvicorn_mode=False) - # Pass remaining arguments to LightRAG's parse_args - sys.argv = [sys.argv[0]] + remaining_args - args = parse_args() - - # If workers specified, override args value - if gunicorn_args.workers: - args.workers = gunicorn_args.workers - os.environ["WORKERS"] = str(gunicorn_args.workers) - - # If timeout specified, set environment variable - if gunicorn_args.timeout: - os.environ["TIMEOUT"] = str(gunicorn_args.timeout) - - # If log-level specified, set environment variable - if gunicorn_args.log_level: - os.environ["LOG_LEVEL"] = gunicorn_args.log_level - - # Save all LightRAG args to environment variable for worker processes - # This is the key step for passing arguments to lightrag_server.py - os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args)) # Display startup information display_splash_screen(args) @@ -83,11 +48,6 @@ def main(): print(f"Workers setting: {args.workers}") print("=" * 80 + "\n") - # Start application with Gunicorn using direct Python API - # Ensure WORKERS environment variable is set before importing gunicorn_config - if args.workers > 1: - os.environ["WORKERS"] = str(args.workers) - # Import Gunicorn's StandaloneApplication from gunicorn.app.base import BaseApplication @@ -136,51 +96,45 @@ def main(): "child_exit", } - # Import the gunicorn_config module directly - import importlib.util + # Import and configure the gunicorn_config module + import gunicorn_config - spec = importlib.util.spec_from_file_location( - "gunicorn_config", "gunicorn_config.py" - ) - self.config_module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(self.config_module) + # Set configuration variables in gunicorn_config + gunicorn_config.workers = int(os.getenv("WORKERS", args.workers)) + gunicorn_config.bind = f"{os.getenv('HOST', args.host)}:{os.getenv('PORT', args.port)}" + gunicorn_config.loglevel = args.log_level.lower() if args.log_level else os.getenv("LOG_LEVEL", "info") + + # Set SSL configuration if enabled + if args.ssl: + gunicorn_config.certfile = args.ssl_certfile + gunicorn_config.keyfile = args.ssl_keyfile - # Set configuration options - for key in dir(self.config_module): + # Set configuration options from the module + for key in dir(gunicorn_config): if key in valid_options: - value = getattr(self.config_module, key) - # Skip functions like on_starting - if not callable(value): + value = getattr(gunicorn_config, key) + # Skip functions like on_starting and None values + if not callable(value) and value is not None: self.cfg.set(key, value) # Set special hooks elif key in special_hooks: - value = getattr(self.config_module, key) + value = getattr(gunicorn_config, key) if callable(value): self.cfg.set(key, value) # 确保正确加载 logconfig_dict - if hasattr(self.config_module, 'logconfig_dict'): - self.cfg.set('logconfig_dict', getattr(self.config_module, 'logconfig_dict')) - - # Override with command line arguments if provided - if gunicorn_args.workers: - self.cfg.set("workers", gunicorn_args.workers) - if gunicorn_args.timeout: - self.cfg.set("timeout", gunicorn_args.timeout) - if gunicorn_args.log_level: - self.cfg.set("loglevel", gunicorn_args.log_level) + if hasattr(gunicorn_config, 'logconfig_dict'): + self.cfg.set('logconfig_dict', getattr(gunicorn_config, 'logconfig_dict')) def load(self): # Import the application from lightrag.api.lightrag_server import get_application - return get_application() + return get_application(args) # Create the application app = GunicornApp("") - # Directly call initialize_share_data with the correct workers value - # Force workers to be an integer and greater than 1 for multi-process mode workers_count = int(args.workers) if workers_count > 1: