File size: 3,166 Bytes
1d6d1e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
#!/usr/bin/env python3
"""
Script to start Celery components for the Lin application.
This script provides a unified way to start Celery worker and beat scheduler.
"""

import os
import sys
import subprocess
import platform
from pathlib import Path

# Add the backend directory to Python path
backend_dir = Path(__file__).parent
sys.path.insert(0, str(backend_dir))

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_worker():
    """Start Celery worker."""
    print("Starting Celery worker...")
    cmd = [
        sys.executable, "-m", "celery",
        "-A", "celery_config",
        "worker",
        "--loglevel=info",
        "--pool=solo",
        "--max-tasks-per-child=100"
    ]
    
    if platform.system() == "Windows":
        subprocess.Popen(cmd, cwd=backend_dir)
    else:
        subprocess.Popen(cmd, cwd=backend_dir)
    
    print("Celery worker started")

def start_beat():
    """Start Celery Beat scheduler."""
    print("Starting Celery Beat scheduler...")
    cmd = [
        sys.executable, "-m", "celery",
        "-A", "celery_config",
        "beat",
        "--loglevel=info",
        "--scheduler=django_celery_beat.schedulers:DatabaseScheduler"
    ]
    
    if platform.system() == "Windows":
        subprocess.Popen(cmd, cwd=backend_dir)
    else:
        subprocess.Popen(cmd, cwd=backend_dir)
    
    print("Celery Beat scheduler started")

def start_all():
    """Start both worker and beat."""
    if not check_redis():
        return False
    
    print("Starting all Celery components...")
    start_worker()
    start_beat()
    print("All Celery components started")
    return True

def main():
    """Main function."""
    if len(sys.argv) < 2:
        print("Usage: python start_celery.py <command>")
        print("Commands:")
        print("  worker    - Start Celery worker only")
        print("  beat      - Start Celery Beat scheduler only")
        print("  all       - Start both worker and beat")
        print("  check     - Check system requirements")
        sys.exit(1)
    
    command = sys.argv[1].lower()
    
    if command == "worker":
        if not check_redis():
            sys.exit(1)
        start_worker()
    elif command == "beat":
        if not check_redis():
            sys.exit(1)
        start_beat()
    elif command == "all":
        if not start_all():
            sys.exit(1)
    elif command == "check":
        print("Checking system requirements...")
        if check_redis():
            print("βœ“ All requirements met")
        else:
            print("βœ— Some requirements not met")
            sys.exit(1)
    else:
        print(f"Unknown command: {command}")
        sys.exit(1)

if __name__ == "__main__":
    main()