二次修改
This commit is contained in:
6
bot.py
6
bot.py
@@ -193,9 +193,11 @@ class MaiBotMain(BaseMain):
|
||||
logger.error(f"数据库连接初始化失败: {e}")
|
||||
raise e
|
||||
|
||||
async def initialize_database_async(self):
|
||||
"""异步初始化数据库表结构"""
|
||||
logger.info("正在初始化数据库表结构...")
|
||||
try:
|
||||
init_db()
|
||||
await init_db()
|
||||
logger.info("数据库表结构初始化完成")
|
||||
except Exception as e:
|
||||
logger.error(f"数据库表结构初始化失败: {e}")
|
||||
@@ -229,6 +231,8 @@ if __name__ == "__main__":
|
||||
try:
|
||||
# 执行初始化和任务调度
|
||||
loop.run_until_complete(main_system.initialize())
|
||||
# 异步初始化数据库表结构
|
||||
loop.run_until_complete(maibot.initialize_database_async())
|
||||
initialize_lpmm_knowledge()
|
||||
# Schedule tasks returns a future that runs forever.
|
||||
# We can run console_input_loop concurrently.
|
||||
|
||||
@@ -72,6 +72,8 @@ dependencies = [
|
||||
"uvicorn>=0.35.0",
|
||||
"watchdog>=6.0.0",
|
||||
"websockets>=15.0.1",
|
||||
"aiomysql>=0.2.0",
|
||||
"aiosqlite>=0.21.0",
|
||||
]
|
||||
|
||||
[[tool.uv.index]]
|
||||
|
||||
@@ -89,7 +89,7 @@ def get_db():
|
||||
return _db
|
||||
|
||||
|
||||
def initialize_sql_database(database_config):
|
||||
async def initialize_sql_database(database_config):
|
||||
"""
|
||||
根据配置初始化SQL数据库连接(SQLAlchemy版本)
|
||||
|
||||
@@ -119,7 +119,7 @@ def initialize_sql_database(database_config):
|
||||
# 使用SQLAlchemy初始化
|
||||
success = initialize_database_compat()
|
||||
if success:
|
||||
_sql_engine = get_engine()
|
||||
_sql_engine = await get_engine()
|
||||
logger.info("SQLAlchemy数据库初始化成功")
|
||||
else:
|
||||
logger.error("SQLAlchemy数据库初始化失败")
|
||||
|
||||
@@ -7,7 +7,6 @@ from sqlalchemy import Column, String, Float, Integer, Boolean, Text, Index, Dat
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.pool import QueuePool
|
||||
import os
|
||||
import datetime
|
||||
import time
|
||||
@@ -621,10 +620,9 @@ async def initialize_database():
|
||||
}
|
||||
|
||||
if config.database_type == "mysql":
|
||||
# MySQL连接池配置
|
||||
# MySQL连接池配置 - 异步引擎使用默认连接池
|
||||
engine_kwargs.update(
|
||||
{
|
||||
"poolclass": QueuePool,
|
||||
"pool_size": config.connection_pool_size,
|
||||
"max_overflow": config.connection_pool_size * 2,
|
||||
"pool_timeout": config.connection_timeout,
|
||||
@@ -640,10 +638,9 @@ async def initialize_database():
|
||||
}
|
||||
)
|
||||
else:
|
||||
# SQLite配置 - 添加连接池设置以避免连接耗尽
|
||||
# SQLite配置 - 异步引擎使用默认连接池
|
||||
engine_kwargs.update(
|
||||
{
|
||||
"poolclass": QueuePool,
|
||||
"pool_size": 20, # 增加池大小
|
||||
"max_overflow": 30, # 增加溢出连接数
|
||||
"pool_timeout": 60, # 增加超时时间
|
||||
@@ -678,6 +675,7 @@ async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
raise RuntimeError("Database session not initialized")
|
||||
session = SessionLocal()
|
||||
yield session
|
||||
# await session.commit()
|
||||
except Exception:
|
||||
if session:
|
||||
await session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user