fix: Bangumi别名解析和Redis事件循环切换问题

- 修复Bangumi API返回别名为字符串列表时的解析错误
- 修复Redis异步客户端在不同事件循环中使用时报Future attached to different loop错误
This commit is contained in:
jxxghp
2026-05-30 13:11:18 +08:00
parent 492e3c333b
commit 5c3796bf73
2 changed files with 28 additions and 6 deletions

View File

@@ -685,7 +685,10 @@ class MediaInfo:
if infobox:
akas = [item.get("value") for item in infobox if item.get("key") == "别名"]
if akas:
self.names = [aka.get("v") for aka in akas[0]]
if isinstance(akas[0], list):
self.names = [aka.get("v") if isinstance(aka, dict) else aka for aka in akas[0]]
elif isinstance(akas[0], str):
self.names = [akas[0]]
# 剧集
if self.type == MediaType.TV and not self.seasons:

View File

@@ -1,3 +1,4 @@
import asyncio
import json
import pickle
from typing import Any, Optional, Generator, Tuple, AsyncGenerator, Union
@@ -320,12 +321,18 @@ class AsyncRedisHelper(ConfigReloadMixin, metaclass=Singleton):
"""
self.redis_url = settings.CACHE_BACKEND_URL
self.client: Optional[Redis] = None
self._loop: Optional[asyncio.AbstractEventLoop] = None
async def _connect(self):
"""
建立异步Redis连接
"""
try:
current_loop = asyncio.get_running_loop()
# 检测事件循环是否发生变化,如果变化则重新连接
if self.client is not None and self._loop is not current_loop:
logger.debug("Event loop changed, reconnecting Redis (async)")
await self._close_client()
if self.client is None:
self.client = Redis.from_url(
self.redis_url,
@@ -334,6 +341,7 @@ class AsyncRedisHelper(ConfigReloadMixin, metaclass=Singleton):
socket_connect_timeout=_socket_connect_timeout,
health_check_interval=_health_check_interval,
)
self._loop = current_loop
# 测试连接确保Redis可用
await self.client.ping()
logger.info(f"Successfully connected to Redis (async){self.redis_url}")
@@ -341,10 +349,23 @@ class AsyncRedisHelper(ConfigReloadMixin, metaclass=Singleton):
except Exception as e:
logger.error(f"Failed to connect to Redis (async): {e}")
self.client = None
self._loop = None
raise RuntimeError("Redis async connection failed") from e
async def _close_client(self):
"""
关闭当前Redis客户端连接
"""
if self.client:
try:
await self.client.close()
except Exception:
pass
self.client = None
self._loop = None
async def on_config_changed(self):
await self.close()
await self._close_client()
await self._connect()
def get_reload_name(self):
@@ -526,7 +547,5 @@ class AsyncRedisHelper(ConfigReloadMixin, metaclass=Singleton):
"""
关闭异步Redis客户端的连接池
"""
if self.client:
await self.client.close()
self.client = None
logger.debug("Redis async connection closed")
await self._close_client()
logger.debug("Redis async connection closed")