|
|
""" |
|
|
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}") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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() |
|
|
|