Skip to content

Commit

Permalink
fix db async session in process issue (#323)
Browse files Browse the repository at this point in the history
Signed-off-by: Aisuko <[email protected]>
  • Loading branch information
Aisuko authored Jul 26, 2024
1 parent d34fc0d commit fe75df6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
4 changes: 4 additions & 0 deletions backend/src/api/dependencies/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
async def get_async_session() -> typing.AsyncGenerator[SQLAlchemyAsyncSession, None]:
try:
yield async_db.async_session
loguru.logger.info(f"Async pool:{async_db.pool.status()}")
except Exception as e:
loguru.logger.info(f"Exception --- {e}")
await async_db.async_session.rollback()
raise
else:
# https://github.com/grillazz/fastapi-sqlalchemy-asyncpg/issues/63
await async_db.async_session.commit()
finally:
await async_db.async_session.close()
4 changes: 3 additions & 1 deletion backend/src/api/routes/rag_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from src.models.schemas.dataset import RagDatasetCreate, RagDatasetResponse, LoadRAGDSResponse
from src.repository.rag_datasets_eng import DatasetEng
from src.repository.crud.account import AccountCRUDRepository
from src.repository.crud.dataset_db import DataSetCRUDRepository
from src.securities.authorizations.jwt import jwt_required
from src.repository.crud.chat import SessionCRUDRepository
from src.utilities.formatters.ds_formatter import DatasetFormatter
Expand All @@ -39,6 +40,7 @@
)
async def get_dataset_list(
token: str = fastapi.Depends(oauth2_scheme),
ds_repo: DataSetCRUDRepository = fastapi.Depends(get_repository(repo_type=DataSetCRUDRepository)),
) -> list[RagDatasetResponse]:
"""
Get all the pre-processed dataset list.
Expand All @@ -62,7 +64,7 @@ async def get_dataset_list(
"""
# It is unthread safe
# list_ds = await ds_repo.get_dataset_list()
# list_ds_from_db = await ds_repo.get_dataset_list()

list_ds = [settings.DEFAULT_RAG_DS_NAME]

Expand Down
15 changes: 0 additions & 15 deletions backend/src/repository/crud/vectors_helper.py

This file was deleted.

8 changes: 4 additions & 4 deletions backend/src/repository/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
create_async_engine as create_sqlalchemy_async_engine,
)
from sqlalchemy.pool import Pool as SQLAlchemyPool
from sqlalchemy.pool import NullPool
from sqlalchemy import create_engine
from src.config.manager import settings

Expand All @@ -33,13 +32,14 @@ def __init__(self):
self.async_engine: SQLAlchemyAsyncEngine = create_sqlalchemy_async_engine(
url=self.set_async_db_uri,
echo=settings.IS_DB_ECHO_LOG,
echo_pool=True,
future=True,
# pool_size=settings.DB_POOL_SIZE,
pool_size=settings.DB_POOL_SIZE,
pool_timeout=60,
# max_overflow=settings.DB_POOL_OVERFLOW,
# pool_recycle=3600, # Periodically recycle connections (optional)
# pool_pre_ping=True, # Check the connection status before using it
pool_pre_ping=True, # Check the connection status before using it
# https://github.com/MagicStack/asyncpg/issues/863
poolclass=NullPool,
)
self.sync_engine = create_engine(
f"{settings.POSTGRES_SCHEMA}://{settings.POSTGRES_USERNAME}:{settings.POSTGRES_PASSWORD}@{settings.POSTGRES_HOST}:{settings.POSTGRES_PORT}/{settings.POSTGRES_DB}"
Expand Down

0 comments on commit fe75df6

Please sign in to comment.