Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend): avoid using DatabaseManager when direct query is possible from the API layer (#10403)

Zamil Majdyb6d6b865ded6
diff --git a/autogpt_platform/backend/backend/blocks/agent.py b/autogpt_platform/backend/backend/blocks/agent.py
index 3ed720f87c..b8066bd501 100644
--- a/autogpt_platform/backend/backend/blocks/agent.py
+++ b/autogpt_platform/backend/backend/blocks/agent.py
@@ -74,7 +74,6 @@ class AgentExecutorBlock(Block):
user_id=input_data.user_id,
inputs=input_data.inputs,
nodes_input_masks=input_data.nodes_input_masks,
- use_db_query=False,
)
logger = execution_utils.LogMetadata(
@@ -198,7 +197,6 @@ class AgentExecutorBlock(Block):
await execution_utils.stop_graph_execution(
graph_exec_id=graph_exec_id,
user_id=user_id,
- use_db_query=False,
)
logger.info(f"Execution {log_id} stopped successfully.")
except Exception as e:
diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.py
index 1aff0cd827..e20d37e88b 100644
--- a/autogpt_platform/backend/backend/executor/scheduler.py
+++ b/autogpt_platform/backend/backend/executor/scheduler.py
@@ -88,7 +88,6 @@ async def _execute_graph(**kwargs):
graph_version=args.graph_version,
inputs=args.input_data,
graph_credentials_inputs=args.input_credentials,
- use_db_query=False,
)
logger.info(
f"Graph execution started with ID {graph_exec.id} for graph {args.graph_id}"
diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.py
index e41e25da1b..e27c43d63f 100644
--- a/autogpt_platform/backend/backend/executor/utils.py
+++ b/autogpt_platform/backend/backend/executor/utils.py
@@ -20,6 +20,7 @@ from backend.data.block import (
)
from backend.data.block_cost_config import BLOCK_COSTS
from backend.data.cost import BlockCostType
+from backend.data.db import prisma
from backend.data.execution import (
AsyncRedisExecutionEventBus,
ExecutionStatus,
@@ -684,7 +685,6 @@ def create_execution_queue_config() -> RabbitMQConfig:
async def stop_graph_execution(
user_id: str,
graph_exec_id: str,
- use_db_query: bool = True,
wait_timeout: float = 15.0,
):
"""
@@ -695,7 +695,7 @@ async def stop_graph_execution(
3. Update execution statuses in DB and set `error` outputs to `"TERMINATED"`.
"""
queue_client = await get_async_execution_queue()
- db = execution_db if use_db_query else get_db_async_client()
+ db = execution_db if prisma.is_connected() else get_db_async_client()
await queue_client.publish_message(
routing_key="",
message=CancelExecutionEvent(graph_exec_id=graph_exec_id).model_dump_json(),
@@ -782,7 +782,6 @@ async def add_graph_execution(
graph_version: Optional[int] = None,
graph_credentials_inputs: Optional[dict[str, CredentialsMetaInput]] = None,
nodes_input_masks: Optional[dict[str, dict[str, JsonValue]]] = None,
- use_db_query: bool = True,
) -> GraphExecutionWithNodes:
"""
Adds a graph execution to the queue and returns the execution entry.
@@ -801,8 +800,12 @@ async def add_graph_execution(
Raises:
ValueError: If the graph is not found or if there are validation errors.
"""
- gdb = graph_db if use_db_query else get_db_async_client()
- edb = execution_db if use_db_query else get_db_async_client()
+ if prisma.is_connected():
+ gdb = graph_db
+ edb = execution_db
+ else:
+ gdb = get_db_async_client()
+ edb = get_db_async_client()
graph: GraphModel | None = await gdb.get_graph(
graph_id=graph_id,
diff --git a/autogpt_platform/backend/backend/integrations/credentials_store.py b/autogpt_platform/backend/backend/integrations/credentials_store.py
index 8added620e..f321da82fa 100644
--- a/autogpt_platform/backend/backend/integrations/credentials_store.py
+++ b/autogpt_platform/backend/backend/integrations/credentials_store.py
@@ -2,18 +2,13 @@ import base64
import hashlib
import secrets
from datetime import datetime, timedelta, timezone
-from typing import TYPE_CHECKING, Optional
-
-from pydantic import SecretStr
-
-from backend.data.redis_client import get_redis_async
-
-if TYPE_CHECKING:
- from backend.executor.database import DatabaseManagerAsyncClient
+from typing import Optional
from autogpt_libs.utils.cache import thread_cached
from autogpt_libs.utils.synchronize import AsyncRedisKeyedMutex
+from pydantic import SecretStr
+from backend.data.db import prisma
from backend.data.model import (
APIKeyCredentials,
Credentials,
@@ -21,6 +16,7 @@ from backend.data.model import (
OAuthState,
UserIntegrations,
)
+from backend.data.redis_client import get_redis_async
from backend.util.settings import Settings
settings = Settings()
@@ -233,11 +229,16 @@ class IntegrationCredentialsStore:
@property
@thread_cached
- def db_manager(self) -> "DatabaseManagerAsyncClient":
- from backend.executor.database import DatabaseManagerAsyncClient
- from backend.util.service import get_service_client
+ def db_manager(self):
+ if prisma.is_connected():
+ from backend.data import user
+
+ return user
+ else:
+ from backend.executor.database import DatabaseManagerAsyncClient
+ from backend.util.service import get_service_client
- return get_service_client(DatabaseManagerAsyncClient)
+ return get_service_client(DatabaseManagerAsyncClient)
async def add_creds(self, user_id: str, credentials: Credentials) -> None:
async with await self.locked_user_integrations(user_id):