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

🤖 Implement Singleton Initialization for Celery App and Database #1613

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
11 changes: 9 additions & 2 deletions src/celery_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ def setup_celery_entrypoint(app: Celery):
app.on_configure.connect(init_celery_app)


# Track if bootup has been performed
_bootup_complete = False

@inject
def init_celery_app(*args: Any, sender: Celery, config: CeleryConfig = injected, **kwargs: Any):
global _bootup_complete
for k, v in config.items():
setattr(sender.conf, k, v)
bootup(start_model_loading=False, integrations=[CeleryIntegration(propagate_traces=True)])

if not _bootup_complete:
bootup(start_model_loading=False, integrations=[CeleryIntegration(propagate_traces=True)])
_bootup_complete = True

from celery_app.tasks import setup_periodic_tasks

sender.on_after_finalize.connect(setup_periodic_tasks)


Expand Down
15 changes: 13 additions & 2 deletions src/seer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,31 @@
from seer.configuration import AppConfig
from seer.dependency_injection import inject, injected

# Module-level flag to track initialization state
_db_initialized = False

@inject
def initialize_database(
config: AppConfig = injected,
app: Flask = injected,
):
app.config["SQLALCHEMY_DATABASE_URI"] = config.DATABASE_URL
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"connect_args": {"prepare_threshold": None}}
global _db_initialized
if _db_initialized:
return

try:
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