File size: 3,736 Bytes
25f22bf 1d6d1e6 25f22bf 1d6d1e6 2f9f089 1d6d1e6 2f9f089 1d6d1e6 3d8beb9 1d6d1e6 3d8beb9 1d6d1e6 3d8beb9 1d6d1e6 3d8beb9 1d6d1e6 3d8beb9 1d6d1e6 3d8beb9 1d6d1e6 25f22bf 1d6d1e6 25f22bf 1d6d1e6 25f22bf 1d6d1e6 25f22bf 1d6d1e6 25f22bf 1d6d1e6 25f22bf 1d6d1e6 25f22bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
#!/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...")
backend_dir = Path(__file__).parent / "backend"
# Start Celery worker
worker_cmd = [
sys.executable, "-m", "celery",
"-A", "celery_config:celery_app", # Changed from 'celery_config'
"worker",
"--loglevel=info",
"--pool=solo",
"--max-tasks-per-child=100"
]
# Start Celery beat
beat_cmd = [
sys.executable, "-m", "celery",
"-A", "celery_config:celery_app", # Changed from 'celery_config'
"beat",
"--loglevel=info",
"--scheduler=django_celery_beat.schedulers:DatabaseScheduler"
]
if platform.system() == "Windows":
# Windows: Use subprocess to start background processes with visible logs
subprocess.Popen(worker_cmd, cwd=backend_dir,
stdout=sys.stdout, stderr=sys.stderr,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
subprocess.Popen(beat_cmd, cwd=backend_dir,
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,
stdout=sys.stdout, stderr=sys.stderr)
subprocess.Popen(beat_cmd, cwd=backend_dir,
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) |