Skip to content

Add sync db sessionmaker for Celery tasks#120

Open
szyszkapiotr wants to merge 1 commit into
the-momentum:mainfrom
szyszkapiotr:feature/add-sync-db-engine-for-celery
Open

Add sync db sessionmaker for Celery tasks#120
szyszkapiotr wants to merge 1 commit into
the-momentum:mainfrom
szyszkapiotr:feature/add-sync-db-engine-for-celery

Conversation

@szyszkapiotr

@szyszkapiotr szyszkapiotr commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Related issue: #121

In my previous MR (#108) we have migrated from sync to async SQLAlchemy sessions. That fits the API layer very well (FastAPI is async-first), but async sessions may cause problems in a purely synchronous context — namely inside Celery tasks. For that specific case I propose using sync db sessions, since running anything async inside Celery is famously painful.

This MR adds a sync db sessionmaker and lays out how to handle database operations inside Celery tasks. Rule of thumb:

  • use async db sessions in the API layer;
  • use sync db sessions inside Celery tasks. If that's not possible (e.g. you need to reuse a service exposing async methods), wrap the call with asyncio.run.

In this MR:

  • added a sync db sessionmaker in app/database.py;
  • updated the docs (AGENTS.md) to describe the approach above;
  • added example tasks showing how to perform db operations (app/integrations/celery/tasks/dummy_task.py).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant