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

🤖 Refactor Database Initialization for Celery Workers #1623

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion src/celery_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ def setup_celery_entrypoint(app: Celery):
def init_celery_app(*args: Any, sender: Celery, config: CeleryConfig = injected, **kwargs: Any):
for k, v in config.items():
setattr(sender.conf, k, v)
bootup(start_model_loading=False, integrations=[CeleryIntegration(propagate_traces=True)])

# Initialize without database setup for Celery workers
bootup(
start_model_loading=False,
integrations=[CeleryIntegration(propagate_traces=True)],
skip_db_init=True
)
from celery_app.tasks import setup_periodic_tasks

sender.on_after_finalize.connect(setup_periodic_tasks)
Expand Down
12 changes: 9 additions & 3 deletions src/seer/bootup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from flask import has_app_context, current_app
import sentry_sdk
from psycopg import Connection
from sentry_sdk.integrations import Integration
Expand Down Expand Up @@ -31,18 +32,23 @@ class DisablePreparedStatementConnection(Connection):
pass



@inject
def bootup(
*, start_model_loading: bool, integrations: list[Integration], config: AppConfig = injected
*, start_model_loading: bool, integrations: list[Integration], config: AppConfig = injected, skip_db_init: bool = False
):
initialize_sentry_sdk(integrations)


with sentry_sdk.metrics.timing(key="seer_bootup_time"):
initialize_logs(["seer.", "celery_app."])
config.do_validation()
initialize_database()

# Only initialize database if we have an app context
if has_app_context():
initialize_database()
initialize_models(start_model_loading)


@inject
def initialize_sentry_sdk(integrations: list[Integration], config: AppConfig = injected) -> None:
def before_send(event: Event, hint: dict) -> Event | None:
Expand Down
16 changes: 16 additions & 0 deletions src/seer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sqlalchemy
from flask import Flask
from flask_migrate import Migrate
from flask import current_app
from flask_sqlalchemy import SQLAlchemy
from pgvector.sqlalchemy import Vector # type: ignore
from pydantic import BaseModel
Expand Down Expand Up @@ -35,20 +36,35 @@
from seer.configuration import AppConfig
from seer.dependency_injection import inject, injected

# Track initialization state globally
_db_initialized = False

@inject
def initialize_database(
config: AppConfig = injected,
app: Flask = injected,
):
global _db_initialized

# Only initialize if not already done
if _db_initialized:
return

# If there's already an instance registered, we can reuse it
if 'sqlalchemy' in current_app.extensions:
return

app.config["SQLALCHEMY_DATABASE_URI"] = config.DATABASE_URL
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"connect_args": {"prepare_threshold": None}}


db.init_app(app)
migrate.init_app(app, db)


with app.app_context():
Session.configure(bind=db.engine)
_db_initialized = True


class Base(DeclarativeBase):
Expand Down
Loading