Server: PID-based logging - only master logs startup
- Only master PID logs blocklist loading, pack scanning, etc. - Worker processes stay silent during startup - Much cleaner logs
This commit is contained in:
+20
-2
@@ -1,5 +1,6 @@
|
||||
import re
|
||||
import logging
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
@@ -17,6 +18,12 @@ from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
|
||||
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
||||
|
||||
# PID for logging (only master logs startup)
|
||||
MASTER_PID = os.getpid()
|
||||
|
||||
def is_master() -> bool:
|
||||
return os.getpid() == MASTER_PID
|
||||
|
||||
from pack_manager import DATA_DIR, scan_pack, get_cached_manifest, PACKS_DIR
|
||||
from models import PackMeta
|
||||
from middleware import LoggingMiddleware
|
||||
@@ -64,9 +71,10 @@ async def lifespan(app: FastAPI):
|
||||
if BLOCKLIST_CACHE_FILE.exists():
|
||||
try:
|
||||
cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines())
|
||||
if cached_ips:
|
||||
if cached_ips and is_master():
|
||||
logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache")
|
||||
except Exception as e:
|
||||
if is_master():
|
||||
logger.warning(f"Failed to load blocklist cache: {e}")
|
||||
|
||||
# If no cache, download (only one worker will do this)
|
||||
@@ -84,6 +92,7 @@ async def lifespan(app: FastAPI):
|
||||
cached_ips = mw.load_public_blocklists()
|
||||
if cached_ips:
|
||||
BLOCKLIST_CACHE_FILE.write_text("\n".join(cached_ips))
|
||||
if is_master():
|
||||
logger.info(f"Downloaded and saved {len(cached_ips)} IPs to blocklist cache")
|
||||
except BlockingIOError:
|
||||
# Another process is downloading - wait for cache
|
||||
@@ -91,13 +100,14 @@ async def lifespan(app: FastAPI):
|
||||
finally:
|
||||
lock_fd.close()
|
||||
except Exception as e:
|
||||
if is_master():
|
||||
logger.warning(f"Lock error: {e}")
|
||||
|
||||
# Re-read cache after download
|
||||
if BLOCKLIST_CACHE_FILE.exists() and not cached_ips:
|
||||
try:
|
||||
cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines())
|
||||
if cached_ips:
|
||||
if cached_ips and is_master():
|
||||
logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache (after wait)")
|
||||
except Exception:
|
||||
pass
|
||||
@@ -105,6 +115,7 @@ async def lifespan(app: FastAPI):
|
||||
all_blocked.update(cached_ips)
|
||||
|
||||
mw.set_ip_config(blocked=all_blocked)
|
||||
if is_master():
|
||||
logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs")
|
||||
|
||||
# Determine environment
|
||||
@@ -115,6 +126,7 @@ async def lifespan(app: FastAPI):
|
||||
else:
|
||||
env = "production"
|
||||
|
||||
if is_master():
|
||||
logger.info(f"Starting ZernMC Launcher Server (environment: {env})")
|
||||
|
||||
# Create directories if they don't exist
|
||||
@@ -129,20 +141,25 @@ async def lifespan(app: FastAPI):
|
||||
yield
|
||||
return
|
||||
|
||||
if is_master():
|
||||
logger.info("Scanning packs on startup...")
|
||||
|
||||
pack_dirs = [p for p in PACKS_DIR.iterdir() if p.is_dir()]
|
||||
|
||||
if not pack_dirs:
|
||||
if is_master():
|
||||
logger.warning(f"No packs found in directory: {PACKS_DIR}")
|
||||
else:
|
||||
for pack_dir in pack_dirs:
|
||||
try:
|
||||
meta = await scan_pack(pack_dir.name)
|
||||
if is_master():
|
||||
logger.info(f"Pack scanned successfully: {pack_dir.name} v{meta.version} ({len(meta.files)} files)")
|
||||
except Exception as e:
|
||||
if is_master():
|
||||
logger.error(f"Failed to scan pack: {pack_dir.name} - {e}", exc_info=True)
|
||||
|
||||
if is_master():
|
||||
logger.info("All packs ready. Server is running.")
|
||||
|
||||
# Initialize proxy client
|
||||
@@ -155,6 +172,7 @@ async def lifespan(app: FastAPI):
|
||||
if proxy_client:
|
||||
await proxy_client.aclose()
|
||||
|
||||
if is_master():
|
||||
logger.info("Server shutting down...")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user