| |
| """ |
| 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() |
| backend_dir = project_root / "backend" |
| |
| |
| env = os.environ.copy() |
| |
| current_pythonpath = env.get('PYTHONPATH', '') |
| env['PYTHONPATH'] = str(project_root) + os.pathsep + current_pythonpath if current_pythonpath else str(project_root) |
|
|
| |
| worker_cmd = [ |
| sys.executable, "-m", "celery", |
| "-A", "celery_config:celery_app", |
| "worker", |
| "--loglevel=debug", |
| "--pool=solo", |
| "--max-tasks-per-child=100", |
| "--events", |
| "-Q", "content,publish,scheduler" |
| ] |
| |
| |
| beat_cmd = [ |
| sys.executable, "-m", "celery", |
| "-A", "celery_config:celery_app", |
| "beat", |
| "--loglevel=debug", |
| "--schedule=/tmp/celerybeat-schedule" |
| ] |
| |
| if platform.system() == "Windows": |
| |
| subprocess.Popen(worker_cmd, cwd=backend_dir, env=env, |
| stdout=sys.stdout, stderr=sys.stderr, |
| creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| subprocess.Popen(beat_cmd, cwd=backend_dir, env=env, |
| stdout=sys.stdout, stderr=sys.stderr, |
| creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) |
| else: |
| |
| subprocess.Popen(worker_cmd, cwd=backend_dir, env=env, |
| stdout=sys.stdout, stderr=sys.stderr) |
| subprocess.Popen(beat_cmd, cwd=backend_dir, env=env, |
| stdout=sys.stdout, stderr=sys.stderr) |
| |
| print("Celery worker and beat scheduler started in background") |
| time.sleep(2) |
|
|
| if __name__ == "__main__": |
| |
| port = os.environ.get('PORT', '7860') |
| os.environ.setdefault('PORT', port) |
| |
| print(f"Starting Lin application on port {port}...") |
| print("=" * 60) |
| |
| |
| 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() |
| |
| |
| 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) |