feat(cache): Add thread-safe Redis client and enable activity stats cache

- Add get_thread_safe_redis() function with thread-local storage and PID checking to prevent "Future attached to a different loop" errors in Celery thread and prefork pools
- Implement health_check_interval=30 to prevent stale connection errors after fork
- Uncomment and enable ActivityStatsCache module in cache/memory/__init__.py
- Uncomment ActivityStatsCache implementation in activity_stats_cache.py and update to use get_thread_safe_redis()
- Update interest_memory.py to use thread-safe Redis client
- Update write_tools.py to use thread-safe Redis client
- Remove redundant Chinese comments from aioRedis.py for cleaner code
- Ensures safe Redis operations across different execution contexts and Celery worker configurations
This commit is contained in:
Ke Sun
2026-03-27 15:35:47 +08:00
committed by lanceyq
parent 17ea92357d
commit 4e9b5736b1
5 changed files with 167 additions and 132 deletions

View File

@@ -260,24 +260,24 @@ async def write(
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"=== Pipeline Run Completed: {timestamp} ===\n\n")
# # 将提取统计写入 Redis按 workspace_id 存储
# try:
# from app.cache.memory.activity_stats_cache import ActivityStatsCache
# 将提取统计写入 Redis按 workspace_id 存储
try:
from app.cache.memory.activity_stats_cache import ActivityStatsCache
# stats_to_cache = {
# "chunk_count": len(all_chunk_nodes) if all_chunk_nodes else 0,
# "statements_count": len(all_statement_nodes) if all_statement_nodes else 0,
# "triplet_entities_count": len(all_entity_nodes) if all_entity_nodes else 0,
# "triplet_relations_count": len(all_entity_entity_edges) if all_entity_entity_edges else 0,
# "temporal_count": 0,
# }
# await ActivityStatsCache.set_activity_stats(
# workspace_id=str(memory_config.workspace_id),
# stats=stats_to_cache,
# )
# logger.info(f"[WRITE] 活动统计已写入 Redis: workspace_id={memory_config.workspace_id}")
# except Exception as cache_err:
# logger.warning(f"[WRITE] 写入活动统计缓存失败(不影响主流程): {cache_err}", exc_info=True)
stats_to_cache = {
"chunk_count": len(all_chunk_nodes) if all_chunk_nodes else 0,
"statements_count": len(all_statement_nodes) if all_statement_nodes else 0,
"triplet_entities_count": len(all_entity_nodes) if all_entity_nodes else 0,
"triplet_relations_count": len(all_entity_entity_edges) if all_entity_entity_edges else 0,
"temporal_count": 0,
}
await ActivityStatsCache.set_activity_stats(
workspace_id=str(memory_config.workspace_id),
stats=stats_to_cache,
)
logger.info(f"[WRITE] 活动统计已写入 Redis: workspace_id={memory_config.workspace_id}")
except Exception as cache_err:
logger.warning(f"[WRITE] 写入活动统计缓存失败(不影响主流程): {cache_err}", exc_info=True)
# logger.info("=== Pipeline Complete ===")
# logger.info(f"Total execution time: {total_time:.2f} seconds")
logger.info("=== Pipeline Complete ===")
logger.info(f"Total execution time: {total_time:.2f} seconds")