File size: 1,371 Bytes
c7e434a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
RQ Worker
Run this to start the background worker
"""

import time
import sys
from rq import Worker
from app.core.redis_client import get_redis_connection, get_queue, check_redis_connection
from app.config import settings


def run_worker():
    """Run RQ worker with retry logic"""
    
    print(f"πŸ”„ Starting RQ Worker...")
    print(f"πŸ“Š Queue: {settings.QUEUE_NAME}")
    print(f"πŸ”— Redis: {settings.REDIS_HOST}:{settings.REDIS_PORT}")
    
    # Wait for Redis to be ready
    max_retries = 30
    retry_interval = 2
    
    for attempt in range(1, max_retries + 1):
        is_connected, error_msg = check_redis_connection()
        
        if is_connected:
            print(f"βœ… Redis connected!")
            break
        else:
            if attempt < max_retries:
                print(f"⏳ Waiting for Redis... (attempt {attempt}/{max_retries})")
                time.sleep(retry_interval)
            else:
                print(f"❌ Failed to connect to Redis after {max_retries} attempts")
                print(f"   Error: {error_msg}")
                sys.exit(1)
    
    # Start worker
    redis_conn = get_redis_connection()
    queue = get_queue()
    
    print(f"πŸš€ Worker ready and listening for tasks!\n")
    
    worker = Worker([queue], connection=redis_conn)
    worker.work()


if __name__ == "__main__":
    run_worker()