backend-async-tasks
Use when creating or updating Celery tasks, async job logic, or Beat schedules in the backend.
Installation
Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.
Voir la source ↗GuidesInstructions
Backend async tasks (Celery)
When to use
- You are adding or changing a Celery task in
backend/src/backend/apps/**/tasks.py. - You need to update the Celery Beat schedule in
backend/src/backend/celery_app.py. - You are adding DB access inside a Celery worker process.
Steps
- Define tasks only in a
tasks.pyfile under the relevant app module. - Decorate tasks with
@app.taskimported frombackend.celery_app. - Return a typed result (
int,None, etc.). Keep task-specific parameters local unless they must be environment-configured. - For DB access:
- Use
asyncio.run()to bridge async work. - Always acquire a session via
get_task_session()frombackend.celery_app.
- Use
- For periodic jobs, update
app.conf.beat_scheduleinbackend/src/backend/celery_app.pyand usecelery.schedules.crontab.
Constraints and guardrails
- Never use the global
alchemy_configengine inside a task; it is not fork-safe for Celery workers. - Always use
get_task_session()to manage DB connections safely across worker processes.
References
-
backend/src/backend/apps/**/tasks.py -
backend/src/backend/celery_app.py -
backend/tests/apps/users/test_tasks.py
Exemples
Consultez les exemples du dépôt source.
Voir la source ↗Historique source
- Date d’indexation
- 2026-01-20
- Mise à jour de l’index
- 2026-01-26
- Branch
- main
Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!