Align Gunicorn configuration with Uvicorn

- centralize config in gunicorn_config.py
- fix log level handling in Gunicorn
This commit is contained in:
yangdx
2025-02-28 20:41:11 +08:00
parent ff549a3a9c
commit c37b1e8aa7
4 changed files with 47 additions and 110 deletions

View File

@@ -2,17 +2,17 @@
import os import os
import logging import logging
from lightrag.kg.shared_storage import finalize_share_data from lightrag.kg.shared_storage import finalize_share_data
from lightrag.api.utils_api import parse_args
from lightrag.api.lightrag_server import LightragPathFilter 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 # These variables will be set by run_with_gunicorn.py
workers = int(os.getenv("WORKERS", args.workers)) workers = None
bind = None
# Binding address loglevel = None
bind = f"{os.getenv('HOST', args.host)}:{os.getenv('PORT', args.port)}" certfile = None
keyfile = None
# Enable preload_app option # Enable preload_app option
preload_app = True preload_app = True
@@ -24,18 +24,9 @@ worker_class = "uvicorn.workers.UvicornWorker"
timeout = int(os.getenv("TIMEOUT", 120)) timeout = int(os.getenv("TIMEOUT", 120))
keepalive = 5 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 # Logging configuration
errorlog = os.getenv("ERROR_LOG", log_file_path) # 默认写入到 lightrag.log errorlog = os.getenv("ERROR_LOG", log_file_path) # 默认写入到 lightrag.log
accesslog = os.getenv("ACCESS_LOG", log_file_path) # 默认写入到 lightrag.log accesslog = os.getenv("ACCESS_LOG", log_file_path) # 默认写入到 lightrag.log
loglevel = os.getenv("LOG_LEVEL", "info")
# 配置日志系统 # 配置日志系统
logconfig_dict = { logconfig_dict = {
@@ -49,13 +40,11 @@ logconfig_dict = {
'handlers': { 'handlers': {
'console': { 'console': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'standard', 'formatter': 'standard',
'stream': 'ext://sys.stdout' 'stream': 'ext://sys.stdout'
}, },
'file': { 'file': {
'class': 'logging.handlers.RotatingFileHandler', 'class': 'logging.handlers.RotatingFileHandler',
'level': 'INFO',
'formatter': 'standard', 'formatter': 'standard',
'filename': log_file_path, 'filename': log_file_path,
'maxBytes': 10485760, # 10MB 'maxBytes': 10485760, # 10MB
@@ -71,22 +60,22 @@ logconfig_dict = {
'loggers': { 'loggers': {
'lightrag': { 'lightrag': {
'handlers': ['console', 'file'], 'handlers': ['console', 'file'],
'level': 'INFO', 'level': loglevel.upper() if loglevel else 'INFO',
'propagate': False 'propagate': False
}, },
'gunicorn': { 'gunicorn': {
'handlers': ['console', 'file'], 'handlers': ['console', 'file'],
'level': 'INFO', 'level': loglevel.upper() if loglevel else 'INFO',
'propagate': False 'propagate': False
}, },
'gunicorn.error': { 'gunicorn.error': {
'handlers': ['console', 'file'], 'handlers': ['console', 'file'],
'level': 'INFO', 'level': loglevel.upper() if loglevel else 'INFO',
'propagate': False 'propagate': False
}, },
'gunicorn.access': { 'gunicorn.access': {
'handlers': ['console', 'file'], 'handlers': ['console', 'file'],
'level': 'INFO', 'level': loglevel.upper() if loglevel else 'INFO',
'propagate': False, 'propagate': False,
'filters': ['path_filter'] 'filters': ['path_filter']
} }
@@ -143,6 +132,10 @@ def post_fork(server, worker):
Executed after a worker has been forked. Executed after a worker has been forked.
This is a good place to set up worker-specific configurations. 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 # Disable uvicorn.error logger in worker processes
uvicorn_error_logger = logging.getLogger("uvicorn.error") uvicorn_error_logger = logging.getLogger("uvicorn.error")
uvicorn_error_logger.setLevel(logging.CRITICAL) uvicorn_error_logger.setLevel(logging.CRITICAL)

View File

@@ -86,7 +86,7 @@ class LightragPathFilter(logging.Filter):
def create_app(args): def create_app(args):
# Setup logging # Setup logging
logger.setLevel(getattr(logging, args.log_level)) logger.setLevel(args.log_level)
set_verbose_debug(args.verbose) set_verbose_debug(args.verbose)
# Verify that bindings are correctly setup # Verify that bindings are correctly setup
@@ -412,17 +412,10 @@ def create_app(args):
return app return app
def get_application(): def get_application(args=None):
"""Factory function for creating the FastAPI application""" """Factory function for creating the FastAPI application"""
# Get args from environment variable if args is None:
args_json = os.environ.get("LIGHTRAG_ARGS") args = parse_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))
return create_app(args) return create_app(args)
@@ -513,10 +506,7 @@ def main():
# Configure logging before parsing args # Configure logging before parsing args
configure_logging() configure_logging()
args = parse_args() args = parse_args(is_uvicorn_mode=True)
# Save args to environment variable for child processes
os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args))
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

@@ -111,10 +111,13 @@ def get_env_value(env_key: str, default: any, value_type: type = str) -> any:
return default 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 Parse command line arguments with environment variable fallback
Args:
is_uvicorn_mode: Whether running under uvicorn mode
Returns: Returns:
argparse.Namespace: Parsed arguments argparse.Namespace: Parsed arguments
""" """
@@ -287,9 +290,6 @@ 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 in uvicorn mode and workers > 1, force it to 1 and log warning
if is_uvicorn_mode and args.workers > 1: if is_uvicorn_mode and args.workers > 1:
original_workers = args.workers original_workers = args.workers

View File

@@ -30,45 +30,10 @@ def main():
# Register signal handlers for graceful shutdown # Register signal handlers for graceful shutdown
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # kill command 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 # Parse all arguments using parse_args
gunicorn_args, remaining_args = parser.parse_known_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 startup information
display_splash_screen(args) display_splash_screen(args)
@@ -83,11 +48,6 @@ def main():
print(f"Workers setting: {args.workers}") print(f"Workers setting: {args.workers}")
print("=" * 80 + "\n") 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 # Import Gunicorn's StandaloneApplication
from gunicorn.app.base import BaseApplication from gunicorn.app.base import BaseApplication
@@ -136,51 +96,45 @@ def main():
"child_exit", "child_exit",
} }
# Import the gunicorn_config module directly # Import and configure the gunicorn_config module
import importlib.util import gunicorn_config
spec = importlib.util.spec_from_file_location( # Set configuration variables in gunicorn_config
"gunicorn_config", "gunicorn_config.py" gunicorn_config.workers = int(os.getenv("WORKERS", args.workers))
) gunicorn_config.bind = f"{os.getenv('HOST', args.host)}:{os.getenv('PORT', args.port)}"
self.config_module = importlib.util.module_from_spec(spec) gunicorn_config.loglevel = args.log_level.lower() if args.log_level else os.getenv("LOG_LEVEL", "info")
spec.loader.exec_module(self.config_module)
# Set SSL configuration if enabled
if args.ssl:
gunicorn_config.certfile = args.ssl_certfile
gunicorn_config.keyfile = args.ssl_keyfile
# Set configuration options # Set configuration options from the module
for key in dir(self.config_module): for key in dir(gunicorn_config):
if key in valid_options: if key in valid_options:
value = getattr(self.config_module, key) value = getattr(gunicorn_config, key)
# Skip functions like on_starting # Skip functions like on_starting and None values
if not callable(value): if not callable(value) and value is not None:
self.cfg.set(key, value) self.cfg.set(key, value)
# Set special hooks # Set special hooks
elif key in special_hooks: elif key in special_hooks:
value = getattr(self.config_module, key) value = getattr(gunicorn_config, key)
if callable(value): if callable(value):
self.cfg.set(key, value) self.cfg.set(key, value)
# 确保正确加载 logconfig_dict # 确保正确加载 logconfig_dict
if hasattr(self.config_module, 'logconfig_dict'): if hasattr(gunicorn_config, 'logconfig_dict'):
self.cfg.set('logconfig_dict', getattr(self.config_module, 'logconfig_dict')) self.cfg.set('logconfig_dict', getattr(gunicorn_config, '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)
def load(self): def load(self):
# Import the application # Import the application
from lightrag.api.lightrag_server import get_application from lightrag.api.lightrag_server import get_application
return get_application() return get_application(args)
# Create the application # Create the application
app = GunicornApp("") 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 # Force workers to be an integer and greater than 1 for multi-process mode
workers_count = int(args.workers) workers_count = int(args.workers)
if workers_count > 1: if workers_count > 1: