Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move blocking io ops to async method on dbservice init #5291

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/backend/base/langflow/services/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from typing import TYPE_CHECKING

import anyio
import sqlalchemy as sa
from alembic import command, util
from alembic.config import Config
Expand Down Expand Up @@ -43,31 +44,33 @@ def __init__(self, settings_service: SettingsService):
raise ValueError(msg)
self.database_url: str = settings_service.settings.database_url
self._sanitize_database_url()

# This file is in langflow.services.database.manager.py
# the ini is in langflow
langflow_dir = Path(__file__).parent.parent.parent
self.script_location = langflow_dir / "alembic"
self.alembic_cfg_path = langflow_dir / "alembic.ini"

# register the event listener for sqlite as part of this class.
# Using decorator will make the method not able to use self
event.listen(Engine, "connect", self.on_connection)
self.engine = self._create_engine()
self.async_engine = self._create_async_engine()
alembic_log_file = self.settings_service.settings.alembic_log_file

alembic_log_file = self.settings_service.settings.alembic_log_file
# Check if the provided path is absolute, cross-platform.
if Path(alembic_log_file).is_absolute():
# Use the absolute path directly.
self.alembic_log_path = Path(alembic_log_file)
else:
# Construct the path using the langflow directory.
self.alembic_log_path = Path(langflow_dir) / alembic_log_file

# Ensure the directory and file for the alembic log file exists
self.alembic_log_path.parent.mkdir(parents=True, exist_ok=True)
self.alembic_log_path.touch(exist_ok=True)
self._logged_pragma = False

async def initialize_alembic_log_file(self):
# Ensure the directory and file for the alembic log file exists
anyio.Path(self.alembic_log_path.parent).mkdir(parents=True, exist_ok=True)
anyio.Path(self.alembic_log_path).touch(exist_ok=True)

def reload_engine(self) -> None:
self._sanitize_database_url()
self.engine = self._create_engine()
Expand Down
4 changes: 3 additions & 1 deletion src/backend/base/langflow/services/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ async def initialize_services(*, fix_migration: bool = False) -> None:
get_service(ServiceType.CACHE_SERVICE, default=CacheServiceFactory())
# Setup the superuser
await initialize_database(fix_migration=fix_migration)
async with get_db_service().with_async_session() as session:
db_service = get_db_service()
await db_service.initialize_alembic_log_file()
async with db_service.with_async_session() as session:
settings_service = get_service(ServiceType.SETTINGS_SERVICE)
await setup_superuser(settings_service, session)
try:
Expand Down
Loading