#!/usr/bin/env python """ Entry point for the Lin application. This script starts both the Flask application and Celery scheduler components. """ import os import sys import subprocess import platform import time from pathlib import Path def check_redis(): """Check if Redis is running.""" try: import redis client = redis.Redis(host='localhost', port=6379, db=0) client.ping() print("✓ Redis connection successful") return True except Exception as e: print(f"✗ Redis connection failed: {e}") print("Please start Redis server first:") print(" Windows: redis-server") print(" Linux/Mac: sudo systemctl start redis") return False def start_celery_components(): """Start Celery worker and beat scheduler in background processes.""" print("Starting Celery components...") project_root = Path(__file__).parent.resolve() # Get the project root directory backend_dir = project_root / "backend" # Prepare the environment with updated PYTHONPATH env = os.environ.copy() # Prepend the project root to PYTHONPATH so 'backend' package can be found current_pythonpath = env.get('PYTHONPATH', '') env['PYTHONPATH'] = str(project_root) + os.pathsep + current_pythonpath if current_pythonpath else str(project_root) # Start Celery worker worker_cmd = [ sys.executable, "-m", "celery", "-A", "celery_config:celery_app", "worker", "--loglevel=info", "--pool=solo", "--max-tasks-per-child=100", "--events" # Enable task events for monitoring ] # Start Celery beat beat_cmd = [ sys.executable, "-m", "celery", "-A", "celery_config:celery_app", "beat", "--loglevel=info", "--schedule=/tmp/celerybeat-schedule" # Specify writable location for schedule file ] if platform.system() == "Windows": # Windows: Use subprocess to start background processes with visible logs subprocess.Popen(worker_cmd, cwd=backend_dir, env=env, # Pass the modified env stdout=sys.stdout, stderr=sys.stderr, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) subprocess.Popen(beat_cmd, cwd=backend_dir, env=env, # Pass the modified env stdout=sys.stdout, stderr=sys.stderr, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) else: # Linux/Mac: Use subprocess with visible logs subprocess.Popen(worker_cmd, cwd=backend_dir, env=env, # Pass the modified env stdout=sys.stdout, stderr=sys.stderr) subprocess.Popen(beat_cmd, cwd=backend_dir, env=env, # Pass the modified env stdout=sys.stdout, stderr=sys.stderr) print("Celery worker and beat scheduler started in background") time.sleep(2) # Give Celery components time to start if __name__ == "__main__": # Set the port for Hugging Face Spaces port = os.environ.get('PORT', '7860') os.environ.setdefault('PORT', port) print(f"Starting Lin application on port {port}...") print("=" * 60) # Check if Redis is available if not check_redis(): print("Warning: Redis not available. Celery may not function properly.") print("Continuing with Flask app only...") print("=" * 60) try: # Start Celery components first start_celery_components() # Import and run the backend Flask app directly from backend.app import create_app app = create_app() print("=" * 60) print("Flask application starting...") print("Access the application at:") print(f" http://localhost:{port}") print(f" http://127.0.0.1:{port}") print("=" * 60) app.run( host='0.0.0.0', port=int(port), debug=False, threaded=True ) except KeyboardInterrupt: print("\nShutting down application...") sys.exit(0) except Exception as e: print(f"Failed to start Lin application: {e}") import traceback traceback.print_exc() sys.exit(1)