Server: Remove broken PID-based logging

- is_master() doesn't work with uvicorn workers
- Keeping clean logs from cache + disabled httpx debug
This commit is contained in:
SashegDev
2026-05-07 17:46:42 +00:00
parent f6fbb66cdc
commit 50080d890f
+2 -19
View File
@@ -18,12 +18,6 @@ from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
logging.getLogger("httpx").setLevel(logging.WARNING) logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").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 pack_manager import DATA_DIR, scan_pack, get_cached_manifest, PACKS_DIR
from models import PackMeta from models import PackMeta
from middleware import LoggingMiddleware from middleware import LoggingMiddleware
@@ -71,10 +65,9 @@ async def lifespan(app: FastAPI):
if BLOCKLIST_CACHE_FILE.exists(): if BLOCKLIST_CACHE_FILE.exists():
try: try:
cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines()) cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines())
if cached_ips and is_master(): if cached_ips:
logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache") logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache")
except Exception as e: except Exception as e:
if is_master():
logger.warning(f"Failed to load blocklist cache: {e}") logger.warning(f"Failed to load blocklist cache: {e}")
# If no cache, download (only one worker will do this) # If no cache, download (only one worker will do this)
@@ -92,7 +85,6 @@ async def lifespan(app: FastAPI):
cached_ips = mw.load_public_blocklists() cached_ips = mw.load_public_blocklists()
if cached_ips: if cached_ips:
BLOCKLIST_CACHE_FILE.write_text("\n".join(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") logger.info(f"Downloaded and saved {len(cached_ips)} IPs to blocklist cache")
except BlockingIOError: except BlockingIOError:
# Another process is downloading - wait for cache # Another process is downloading - wait for cache
@@ -100,14 +92,13 @@ async def lifespan(app: FastAPI):
finally: finally:
lock_fd.close() lock_fd.close()
except Exception as e: except Exception as e:
if is_master():
logger.warning(f"Lock error: {e}") logger.warning(f"Lock error: {e}")
# Re-read cache after download # Re-read cache after download
if BLOCKLIST_CACHE_FILE.exists() and not cached_ips: if BLOCKLIST_CACHE_FILE.exists() and not cached_ips:
try: try:
cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines()) cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines())
if cached_ips and is_master(): if cached_ips:
logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache (after wait)") logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache (after wait)")
except Exception: except Exception:
pass pass
@@ -115,7 +106,6 @@ async def lifespan(app: FastAPI):
all_blocked.update(cached_ips) all_blocked.update(cached_ips)
mw.set_ip_config(blocked=all_blocked) mw.set_ip_config(blocked=all_blocked)
if is_master():
logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs") logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs")
# Determine environment # Determine environment
@@ -126,7 +116,6 @@ async def lifespan(app: FastAPI):
else: else:
env = "production" env = "production"
if is_master():
logger.info(f"Starting ZernMC Launcher Server (environment: {env})") logger.info(f"Starting ZernMC Launcher Server (environment: {env})")
# Create directories if they don't exist # Create directories if they don't exist
@@ -141,25 +130,20 @@ async def lifespan(app: FastAPI):
yield yield
return return
if is_master():
logger.info("Scanning packs on startup...") logger.info("Scanning packs on startup...")
pack_dirs = [p for p in PACKS_DIR.iterdir() if p.is_dir()] pack_dirs = [p for p in PACKS_DIR.iterdir() if p.is_dir()]
if not pack_dirs: if not pack_dirs:
if is_master():
logger.warning(f"No packs found in directory: {PACKS_DIR}") logger.warning(f"No packs found in directory: {PACKS_DIR}")
else: else:
for pack_dir in pack_dirs: for pack_dir in pack_dirs:
try: try:
meta = await scan_pack(pack_dir.name) 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)") logger.info(f"Pack scanned successfully: {pack_dir.name} v{meta.version} ({len(meta.files)} files)")
except Exception as e: except Exception as e:
if is_master():
logger.error(f"Failed to scan pack: {pack_dir.name} - {e}", exc_info=True) 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.") logger.info("All packs ready. Server is running.")
# Initialize proxy client # Initialize proxy client
@@ -172,7 +156,6 @@ async def lifespan(app: FastAPI):
if proxy_client: if proxy_client:
await proxy_client.aclose() await proxy_client.aclose()
if is_master():
logger.info("Server shutting down...") logger.info("Server shutting down...")