yangdx commited on
Commit
fdb6c2e
·
1 Parent(s): f9dfc78

Optimize logging config & worker handling for different server modes

Browse files

• Separate logging config for uvicorn/gunicorn
• Force workers=1 in uvicorn mode
• Add warning for worker count in uvicorn

lightrag/api/lightrag_server.py CHANGED
@@ -414,9 +414,6 @@ def create_app(args):
414
 
415
  def get_application():
416
  """Factory function for creating the FastAPI application"""
417
- # Configure logging for this worker process
418
- configure_logging()
419
-
420
  # Get args from environment variable
421
  args_json = os.environ.get("LIGHTRAG_ARGS")
422
  if not args_json:
@@ -430,11 +427,7 @@ def get_application():
430
 
431
 
432
  def configure_logging():
433
- """Configure logging for both uvicorn and lightrag"""
434
- # Check if running under Gunicorn
435
- if "GUNICORN_CMD_ARGS" in os.environ:
436
- # If started with Gunicorn, return directly as Gunicorn will handle logging
437
- return
438
 
439
  # Reset any existing handlers to ensure clean configuration
440
  for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]:
@@ -517,13 +510,13 @@ def main():
517
 
518
  freeze_support()
519
 
 
 
 
520
  args = parse_args()
521
  # Save args to environment variable for child processes
522
  os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args))
523
 
524
- # Configure logging before starting uvicorn
525
- configure_logging()
526
-
527
  display_splash_screen(args)
528
 
529
  # Create application instance directly instead of using factory function
 
414
 
415
  def get_application():
416
  """Factory function for creating the FastAPI application"""
 
 
 
417
  # Get args from environment variable
418
  args_json = os.environ.get("LIGHTRAG_ARGS")
419
  if not args_json:
 
427
 
428
 
429
  def configure_logging():
430
+ """Configure logging for uvicorn startup"""
 
 
 
 
431
 
432
  # Reset any existing handlers to ensure clean configuration
433
  for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]:
 
510
 
511
  freeze_support()
512
 
513
+ # Configure logging before parsing args
514
+ configure_logging()
515
+
516
  args = parse_args()
517
  # Save args to environment variable for child processes
518
  os.environ["LIGHTRAG_ARGS"] = json.dumps(vars(args))
519
 
 
 
 
520
  display_splash_screen(args)
521
 
522
  # Create application instance directly instead of using factory function
lightrag/api/utils_api.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  import argparse
7
  from typing import Optional
8
  import sys
 
9
  from ascii_colors import ASCIIColors
10
  from lightrag.api import __api_version__
11
  from fastapi import HTTPException, Security
@@ -286,6 +287,16 @@ def parse_args() -> argparse.Namespace:
286
 
287
  args = parser.parse_args()
288
 
 
 
 
 
 
 
 
 
 
 
289
  # convert relative path to absolute path
290
  args.working_dir = os.path.abspath(args.working_dir)
291
  args.input_dir = os.path.abspath(args.input_dir)
 
6
  import argparse
7
  from typing import Optional
8
  import sys
9
+ import logging
10
  from ascii_colors import ASCIIColors
11
  from lightrag.api import __api_version__
12
  from fastapi import HTTPException, Security
 
287
 
288
  args = parser.parse_args()
289
 
290
+ # Check if running under uvicorn mode (not Gunicorn)
291
+ is_uvicorn_mode = "GUNICORN_CMD_ARGS" not in os.environ
292
+
293
+ # If in uvicorn mode and workers > 1, force it to 1 and log warning
294
+ if is_uvicorn_mode and args.workers > 1:
295
+ original_workers = args.workers
296
+ args.workers = 1
297
+ # Log warning directly here
298
+ logging.warning(f"In uvicorn mode, workers parameter was set to {original_workers}. Forcing workers=1")
299
+
300
  # convert relative path to absolute path
301
  args.working_dir = os.path.abspath(args.working_dir)
302
  args.input_dir = os.path.abspath(args.input_dir)