Skip to content

Commit

Permalink
Use AsyncSession in crud log
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Nov 18, 2024
1 parent 3188517 commit 5283f6e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/backend/base/langflow/graph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from langflow.services.database.models.transactions.model import TransactionBase
from langflow.services.database.models.vertex_builds.crud import log_vertex_build as crud_log_vertex_build
from langflow.services.database.models.vertex_builds.model import VertexBuildBase
from langflow.services.database.utils import session_getter
from langflow.services.database.utils import session_getter, async_session_getter

Check failure on line 21 in src/backend/base/langflow/graph/utils.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (F401)

src/backend/base/langflow/graph/utils.py:21:46: F401 `langflow.services.database.utils.session_getter` imported but unused
from langflow.services.deps import get_db_service, get_settings_service

if TYPE_CHECKING:

Check failure on line 24 in src/backend/base/langflow/graph/utils.py

View workflow job for this annotation

GitHub Actions / Ruff Style Check (3.12)

Ruff (I001)

src/backend/base/langflow/graph/utils.py:1:1: I001 Import block is un-sorted or un-formatted
Expand Down Expand Up @@ -157,14 +157,14 @@ async def log_transaction(
error=error,
flow_id=flow_id if isinstance(flow_id, UUID) else UUID(flow_id),
)
with session_getter(get_db_service()) as session:
inserted = crud_log_transaction(session, transaction)
async with async_session_getter(get_db_service()) as session:
inserted = await crud_log_transaction(session, transaction)
logger.debug(f"Logged transaction: {inserted.id}")
except Exception: # noqa: BLE001
logger.exception("Error logging transaction")


def log_vertex_build(
async def log_vertex_build(
*,
flow_id: str,
vertex_id: str,
Expand All @@ -186,8 +186,8 @@ def log_vertex_build(
# ugly hack to get the model dump with weird datatypes
artifacts=json.loads(json.dumps(artifacts, default=str)),
)
with session_getter(get_db_service()) as session:
inserted = crud_log_vertex_build(session, vertex_build)
with async_session_getter(get_db_service()) as session:
inserted = await crud_log_vertex_build(session, vertex_build)
logger.debug(f"Logged vertex build: {inserted.build_id}")
except Exception: # noqa: BLE001
logger.exception("Error logging vertex build")
Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/graph/vertex/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ async def stream(self):
and hasattr(self.custom_component, "store_message")
):
self.custom_component.store_message(message)
log_vertex_build(
await log_vertex_build(
flow_id=self.graph.flow_id,
vertex_id=self.id,
valid=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ async def get_transactions_by_flow_id(
return list(transactions)


def log_transaction(db: Session, transaction: TransactionBase) -> TransactionTable:
async def log_transaction(db: AsyncSession, transaction: TransactionBase) -> TransactionTable:
table = TransactionTable(**transaction.model_dump())
db.add(table)
try:
db.commit()
await db.commit()
except IntegrityError:
db.rollback()
await db.rollback()
raise
return table
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ async def get_vertex_builds_by_flow_id(
return list(builds)


def log_vertex_build(db: Session, vertex_build: VertexBuildBase) -> VertexBuildTable:
async def log_vertex_build(db: AsyncSession, vertex_build: VertexBuildBase) -> VertexBuildTable:
table = VertexBuildTable(**vertex_build.model_dump())
db.add(table)
try:
db.commit()
await db.commit()
except IntegrityError:
db.rollback()
await db.rollback()
raise
return table

Expand Down
2 changes: 1 addition & 1 deletion src/backend/base/langflow/services/socket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def build_vertex(
result_dict = ResultDataResponse(results={})
artifacts = {}
await set_cache(flow_id, graph)
log_vertex_build(
await log_vertex_build(
flow_id=flow_id,
vertex_id=vertex_id,
valid=valid,
Expand Down
4 changes: 2 additions & 2 deletions src/backend/tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class VertexTuple(NamedTuple):
"vertex_id": "vid",
"flow_id": flow_id,
}
log_vertex_build(
await log_vertex_build(
flow_id=build["flow_id"],
vertex_id=build["vertex_id"],
valid=build["valid"],
Expand Down Expand Up @@ -376,7 +376,7 @@ class VertexTuple(NamedTuple):
"vertex_id": "vid",
"flow_id": flow_id,
}
log_vertex_build(
await log_vertex_build(
flow_id=build["flow_id"],
vertex_id=build["vertex_id"],
valid=build["valid"],
Expand Down

0 comments on commit 5283f6e

Please sign in to comment.