CVE-FactChecker / run_production.py
NLPGenius's picture
Stability: remove gunicorn --preload, increase timeout, disable tokenizers parallelism, cap BLAS threads, lighten /health, configurable embeddings
1dd0906
raw
history blame
2.97 kB
#!/usr/bin/env python3
"""
Production startup script for CVE Fact Checker.
This script handles environment setup and graceful startup.
"""
import os
import sys
import time
import signal
import subprocess
from pathlib import Path
def setup_signal_handlers():
"""Setup graceful shutdown handlers."""
def signal_handler(signum, frame):
print(f"\nπŸ›‘ Received signal {signum}, shutting down gracefully...")
sys.exit(0)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
def run_health_check():
"""Run basic health check before starting."""
print("πŸ₯ Running pre-startup health check...")
try:
result = subprocess.run([
sys.executable, "health_check.py"
], capture_output=True, text=True, timeout=30)
if result.returncode == 0:
print("βœ… Health check passed")
return True
else:
print("⚠️ Health check warnings:")
print(result.stdout)
print(result.stderr)
return True # Continue anyway
except Exception as e:
print(f"⚠️ Health check failed: {e}")
return True # Continue anyway
def start_application():
"""Start the application with gunicorn."""
print("πŸš€ Starting CVE Fact Checker...")
# Environment setup
env = os.environ.copy()
env.update({
'PYTHONUNBUFFERED': '1',
'PYTHONDONTWRITEBYTECODE': '1',
})
# Gunicorn command
cmd = [
"gunicorn",
"-w", "1", # Single worker to avoid race conditions
"-k", "gthread",
"--threads", "4",
"-b", f"0.0.0.0:{env.get('PORT', '7860')}",
"--timeout", "180",
"--access-logfile", "-", # Log to stdout
"--error-logfile", "-", # Log to stderr
"cve_factchecker.wsgi:application"
]
print(f"πŸ“‹ Command: {' '.join(cmd)}")
print(f"🌐 Will bind to port {env.get('PORT', '7860')}")
try:
# Run gunicorn
subprocess.run(cmd, env=env, check=True)
except KeyboardInterrupt:
print("\nπŸ›‘ Interrupted by user")
except subprocess.CalledProcessError as e:
print(f"❌ Application failed with exit code {e.returncode}")
sys.exit(e.returncode)
except Exception as e:
print(f"❌ Unexpected error: {e}")
sys.exit(1)
def main():
"""Main startup routine."""
print("=" * 60)
print("πŸ€– CVE Fact Checker - Production Startup")
print("=" * 60)
print(f"⏰ Starting at: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"🐍 Python: {sys.version}")
print(f"πŸ“ Working directory: {os.getcwd()}")
print()
setup_signal_handlers()
# Optional health check
if os.path.exists("health_check.py"):
run_health_check()
print()
start_application()
if __name__ == "__main__":
main()