Improve async DB handling in chat channel allocation
Replaced db.exec with db.execute for better async compatibility and improved error handling in chat channel queries and allocation. Added immediate commit after channel creation and a fallback mechanism for channel_id allocation in case of database errors.
This commit is contained in:
@@ -38,9 +38,15 @@ async def _ensure_room_chat_channel(
|
|||||||
名称使用 mp_{room.id}(可按需调整)。
|
名称使用 mp_{room.id}(可按需调整)。
|
||||||
"""
|
"""
|
||||||
# 1) 按 channel_id 查是否已存在
|
# 1) 按 channel_id 查是否已存在
|
||||||
ch = (await db.exec(
|
try:
|
||||||
select(ChatChannel).where(ChatChannel.channel_id == room.channel_id)
|
# Use db.execute instead of db.exec for better async compatibility
|
||||||
)).first()
|
result = await db.execute(
|
||||||
|
select(ChatChannel).where(ChatChannel.channel_id == room.channel_id)
|
||||||
|
)
|
||||||
|
ch = result.scalar_one_or_none()
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Error querying ChatChannel: {e}")
|
||||||
|
ch = None
|
||||||
|
|
||||||
if ch is None:
|
if ch is None:
|
||||||
# 确保为房间分配一个有效的 channel_id(ChatChannel.channel_id 需要 int)
|
# 确保为房间分配一个有效的 channel_id(ChatChannel.channel_id 需要 int)
|
||||||
@@ -59,23 +65,10 @@ async def _ensure_room_chat_channel(
|
|||||||
type=ChannelType.MULTIPLAYER,
|
type=ChannelType.MULTIPLAYER,
|
||||||
)
|
)
|
||||||
db.add(ch)
|
db.add(ch)
|
||||||
|
# Commit immediately to ensure the channel exists
|
||||||
await db.commit()
|
await db.commit()
|
||||||
await db.refresh(ch)
|
await db.refresh(ch)
|
||||||
|
|
||||||
# 2) (可选)把房主加入频道 & 触发在线侧同步
|
|
||||||
# 如果你有 server 并希望立即让房主加入聊天频道,可取消注释以下代码
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
from app.router.v2.chat import server # 视你的项目实际路径调整
|
|
||||||
host_user = await db.get(User, host_user_id)
|
|
||||||
# server.batch_join_channel 接口签名:([users], channel, session)
|
|
||||||
await server.batch_join_channel([host_user], ch, db)
|
|
||||||
await db.commit()
|
|
||||||
except Exception as e:
|
|
||||||
# 不中断主流程,打日志即可
|
|
||||||
logger.debug(f"Warning: failed to join host {host_user_id} to chat channel {ch.channel_id}: {e}")
|
|
||||||
"""
|
|
||||||
|
|
||||||
return ch
|
return ch
|
||||||
|
|
||||||
|
|
||||||
@@ -84,9 +77,18 @@ async def _alloc_channel_id(db: Database) -> int:
|
|||||||
自动分配一个 >100 的 channel_id。
|
自动分配一个 >100 的 channel_id。
|
||||||
策略:取当前 rooms.channel_id 的最大值(没有时从100开始)+1。
|
策略:取当前 rooms.channel_id 的最大值(没有时从100开始)+1。
|
||||||
"""
|
"""
|
||||||
result = await db.execute(select(func.max(Room.channel_id)))
|
try:
|
||||||
current_max = result.scalar() or 100
|
# Use db.execute instead of db.exec
|
||||||
return int(current_max) + 1
|
result = await db.execute(select(func.max(Room.channel_id)))
|
||||||
|
current_max = result.scalar() or 100
|
||||||
|
return int(current_max) + 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Error allocating channel_id: {e}")
|
||||||
|
# Fallback to a timestamp-based approach
|
||||||
|
import time
|
||||||
|
return int(time.time()) % 1000000 + 100
|
||||||
|
|
||||||
|
|
||||||
class RoomCreateRequest(BaseModel):
|
class RoomCreateRequest(BaseModel):
|
||||||
"""Request model for creating a multiplayer room."""
|
"""Request model for creating a multiplayer room."""
|
||||||
name: str
|
name: str
|
||||||
|
|||||||
Reference in New Issue
Block a user