imetting_backend/test/test_redis_llm.py

58 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
测试Redis连接和LLM任务队列
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import redis
from app.core.config import REDIS_CONFIG
# 连接Redis
redis_client = redis.Redis(**REDIS_CONFIG)
try:
# 测试连接
redis_client.ping()
print("✅ Redis连接成功")
# 检查任务队列
queue_length = redis_client.llen("llm_task_queue")
print(f"📋 当前任务队列长度: {queue_length}")
# 检查所有LLM任务
keys = redis_client.keys("llm_task:*")
print(f"📊 当前存在的LLM任务: {len(keys)}")
for key in keys:
task_data = redis_client.hgetall(key)
# key可能是bytes或str
if isinstance(key, bytes):
task_id = key.decode('utf-8').replace('llm_task:', '')
else:
task_id = key.replace('llm_task:', '')
# 获取状态和进度
status = task_data.get(b'status', task_data.get('status', 'unknown'))
if isinstance(status, bytes):
status = status.decode('utf-8')
progress = task_data.get(b'progress', task_data.get('progress', '0'))
if isinstance(progress, bytes):
progress = progress.decode('utf-8')
print(f" - 任务 {task_id[:8]}... 状态: {status}, 进度: {progress}%")
# 如果任务是pending重新推送到队列
if status == 'pending':
print(f" 🔄 发现pending任务重新推送到队列...")
redis_client.lpush("llm_task_queue", task_id)
print(f" ✅ 任务 {task_id[:8]}... 已重新推送到队列")
except redis.ConnectionError as e:
print(f"❌ Redis连接失败: {e}")
except Exception as e:
print(f"❌ 错误: {e}")
import traceback
traceback.print_exc()