From 5d1ba7622919bb6531046594b03fc74bf9dd6f0c Mon Sep 17 00:00:00 2001 From: Thomas Girod Date: Wed, 9 Apr 2025 14:10:39 +0200 Subject: [PATCH] adapt celery to honcho setup --- .env.example | 3 ++- .github/workflows/ci.yml | 1 + Procfile.service | 1 + core/views/user.py | 3 ++- docs/tutorial/install-advanced.md | 12 +++++++++--- docs/tutorial/install.md | 10 ---------- manage.py | 11 +++++++---- sith/settings.py | 2 +- 8 files changed, 23 insertions(+), 20 deletions(-) diff --git a/.env.example b/.env.example index 2d47ad1f..25c259e7 100644 --- a/.env.example +++ b/.env.example @@ -9,7 +9,8 @@ DATABASE_URL=sqlite:///db.sqlite3 #DATABASE_URL=postgres://user:password@127.0.0.1:5432/sith REDIS_PORT=7963 -CACHE_URL=redis://127.0.0.1:${REDIS_PORT}/0 +CACHE_URL=redis://127.0.0.1:7963/0 +BROKER_URL=redis://127.0.0.1:7963/1 # Used to select which other services to run alongside # manage.py, pytest and runserver diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8284d3f..912d5d83 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,7 @@ env: SECRET_KEY: notTheRealOne DATABASE_URL: sqlite:///db.sqlite3 CACHE_URL: redis://127.0.0.1:6379/0 + BROKER_URL: redis://127.0.0.1:6379/1 jobs: pre-commit: diff --git a/Procfile.service b/Procfile.service index 4f9c4808..5ff3952b 100644 --- a/Procfile.service +++ b/Procfile.service @@ -1 +1,2 @@ redis: redis-server --port $REDIS_PORT +celery: uv run celery -A sith worker --beat -l INFO diff --git a/core/views/user.py b/core/views/user.py index cd8fd55b..98ba2f02 100644 --- a/core/views/user.py +++ b/core/views/user.py @@ -66,7 +66,6 @@ from core.views.forms import ( ) from core.views.mixins import QuickNotifMixin, TabedViewMixin, UseFragmentsMixin from counter.models import Counter, Refilling, Selling -from counter.views.student_card import StudentCardFormFragment from eboutic.models import Invoice from subscription.models import Subscription from trombi.views import UserTrombiForm @@ -529,6 +528,8 @@ class UserPreferencesView(UserTabsMixin, UseFragmentsMixin, CanEditMixin, Update return kwargs def get_fragment_context_data(self) -> dict[str, SafeString]: + from counter.views.student_card import StudentCardFormFragment + res = super().get_fragment_context_data() if hasattr(self.object, "customer"): res["student_card_fragment"] = StudentCardFormFragment.as_fragment()( diff --git a/docs/tutorial/install-advanced.md b/docs/tutorial/install-advanced.md index 3489120b..6cc525e1 100644 --- a/docs/tutorial/install-advanced.md +++ b/docs/tutorial/install-advanced.md @@ -120,7 +120,7 @@ les conflits avec les instances de redis déjà en fonctionnement. ```dotenv REDIS_PORT=6379 -CACHE_URL=redis://127.0.0.1:${REDIS_PORT}/0 +CACHE_URL=redis://127.0.0.1:6379/0 ``` Si on souhaite configurer redis pour communiquer via un socket : @@ -151,7 +151,7 @@ ALTER ROLE sith SET client_encoding TO 'utf8'; ALTER ROLE sith SET default_transaction_isolation TO 'read committed'; ALTER ROLE sith SET timezone TO 'UTC'; -GRANT ALL PRIVILEGES ON DATABASE sith TO SITH; +GRANT ALL PRIVILEGES ON DATABASE sith TO sith; \q ``` @@ -290,9 +290,15 @@ Pour faire tourner Celery, faites la commande suivante dans un terminal à part : ```bash -poetry run celery -A sith worker --beat -l INFO +uv run celery -A sith worker --beat -l INFO ``` +!!!note + + Nous utilisons Redis comme broker pour Celery, + donc vous devez aussi configurer l'URL du broker, + de la même manière que ce qui est décrit plus haut + pour Redis. ## Mettre à jour la base de données antispam diff --git a/docs/tutorial/install.md b/docs/tutorial/install.md index 87e0c33b..c5c5b9d6 100644 --- a/docs/tutorial/install.md +++ b/docs/tutorial/install.md @@ -100,16 +100,6 @@ cd /mnt//vos/fichiers/comme/dhab Python ne fait pas parti des dépendances puisqu'il est automatiquement installé par uv. - -Parmi les dépendances installées se trouve redis (que nous utilisons comme cache). -Redis est un service qui doit être activé pour être utilisé. -Pour cela, effectuez les commandes : - -```bash -sudo systemctl start redis -sudo systemctl enable redis # si vous voulez que redis démarre automatiquement au boot -``` - ## Finaliser l'installation Clonez le projet (depuis votre console WSL, si vous utilisez WSL) diff --git a/manage.py b/manage.py index 101696e2..3d58317e 100755 --- a/manage.py +++ b/manage.py @@ -18,10 +18,10 @@ import logging import os import sys +from django.conf import settings from django.utils.autoreload import DJANGO_AUTORELOAD_ENV from sith.composer import start_composer, stop_composer -from sith.settings import PROCFILE_SERVICE if __name__ == "__main__": logging.basicConfig(encoding="utf-8", level=logging.INFO) @@ -30,8 +30,11 @@ if __name__ == "__main__": from django.core.management import execute_from_command_line - if os.environ.get(DJANGO_AUTORELOAD_ENV) is None and PROCFILE_SERVICE is not None: - start_composer(PROCFILE_SERVICE) - _ = atexit.register(stop_composer, procfile=PROCFILE_SERVICE) + if ( + os.environ.get(DJANGO_AUTORELOAD_ENV) is None + and settings.PROCFILE_SERVICE is not None + ): + start_composer(settings.PROCFILE_SERVICE) + _ = atexit.register(stop_composer, procfile=settings.PROCFILE_SERVICE) execute_from_command_line(sys.argv) diff --git a/sith/settings.py b/sith/settings.py index 8f5f8e92..e5565fb2 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -343,7 +343,7 @@ EMAIL_PORT = env.int("EMAIL_PORT", default=25) CELERY_TIMEZONE = TIME_ZONE CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = 30 * 60 -CElERY_BROKER_URL = env.str("CELERY_BROKER_URL", default="redis://localhost:6379/1") +CElERY_BROKER_URL = env.str("BROKER_URL") CELERY_RESULT_BACKEND = "django-db" CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"