diff --git a/DjangoFiles/AuthBillet/tasks.py b/DjangoFiles/AuthBillet/tasks.py new file mode 100644 index 0000000..8f73dc5 --- /dev/null +++ b/DjangoFiles/AuthBillet/tasks.py @@ -0,0 +1,8 @@ +# Create your tasks here + +from celery import shared_task + + +@shared_task +def add(x, y): + return x + y \ No newline at end of file diff --git a/DjangoFiles/BaseBillet/signals.py b/DjangoFiles/BaseBillet/signals.py index e139ce2..92ecf6b 100644 --- a/DjangoFiles/BaseBillet/signals.py +++ b/DjangoFiles/BaseBillet/signals.py @@ -7,19 +7,11 @@ from django.utils import timezone from ApiBillet.thread_mailer import ThreadMaileur from BaseBillet.models import Reservation, LigneArticle, Ticket, Product, Configuration, Paiement_stripe -import logging from TiBillet import settings +import logging logger = logging.getLogger(__name__) -logger.info(f'import basebillet.signals') - - -# @receiver(post_save, sender=Reservation) -# def trigger_reservation(sender, instance: Reservation, created, **kwargs): -# if instance.status == Reservation.PAID: - - ######################################################################## diff --git a/DjangoFiles/TiBillet/__init__.py b/DjangoFiles/TiBillet/__init__.py index e69de29..1e3599b 100644 --- a/DjangoFiles/TiBillet/__init__.py +++ b/DjangoFiles/TiBillet/__init__.py @@ -0,0 +1,5 @@ +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ('celery_app',) \ No newline at end of file diff --git a/DjangoFiles/TiBillet/celery.py b/DjangoFiles/TiBillet/celery.py new file mode 100644 index 0000000..e87bcd7 --- /dev/null +++ b/DjangoFiles/TiBillet/celery.py @@ -0,0 +1,49 @@ +''' + +import os +from celery import Celery + +# setting the Django settings module. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TiBillet.settings') +app = Celery('TiBillet') + +app.config_from_object('django.conf:settings', namespace='CELERY') +# Looks up for task modules in Django applications and loads them +app.autodiscover_tasks() + + +''' + +''' +celery -A TiBillet worker -l INFO +from TiBillet.celery import my_task +my_task.delay() +''' + +import os +from django.db import connection + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TiBillet.settings') + +from django.conf import settings + +from tenant_schemas_celery.app import CeleryApp as TenantAwareCeleryApp + +app = TenantAwareCeleryApp() +app.config_from_object('django.conf:settings') +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + + +@app.task(bind=True) +def debug_task(self): + print(f'Request: {self.request}') + + +@app.task +def add(x, y): + return x + y + + +@app.task +def my_task(): + print(connection.schema_name) diff --git a/DjangoFiles/TiBillet/settings.py b/DjangoFiles/TiBillet/settings.py index bc16b11..71871e4 100644 --- a/DjangoFiles/TiBillet/settings.py +++ b/DjangoFiles/TiBillet/settings.py @@ -212,6 +212,15 @@ EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD') EMAIL_USE_TLS = os.environ.get('EMAIL_USE_TLS', False) EMAIL_USE_SSL = os.environ.get('EMAIL_USE_SSL', True) +# Celery Configuration Options +CELERY_TIMEZONE=os.environ.get('TIME_ZONE', 'UTC') +CELERY_TASK_TRACK_STARTED=True +CELERY_TASK_TIME_LIMIT=30 * 60 +BROKER_URL=os.environ.get('CELERY_BROKER') +# CELERY_BROKER=os.environ.get('CELERY_BROKER') +# CELERY_BROKER_URL=os.environ.get('CELERY_BROKER') +# DJANGO_CELERY_BEAT_TZ_AWARE=False +# celery -A TiBillet worker -l INFO # Jet Menu # -------------------------------------/ diff --git a/DjangoFiles/TiBillet/urls_tenants.py b/DjangoFiles/TiBillet/urls_tenants.py index 82883f7..1291914 100644 --- a/DjangoFiles/TiBillet/urls_tenants.py +++ b/DjangoFiles/TiBillet/urls_tenants.py @@ -39,7 +39,6 @@ urlpatterns = [ re_path(r'api/', include('ApiBillet.urls')), re_path(r'qr/', include('QrcodeCashless.urls')), - # pour carte GEN1 Bisik re_path(r'(?P^\w{5}$)', include('QrcodeCashless.urls')), diff --git a/DjangoFiles/launch.sh b/DjangoFiles/launch.sh index 3b691eb..8366696 100755 --- a/DjangoFiles/launch.sh +++ b/DjangoFiles/launch.sh @@ -1 +1,3 @@ -sleep 30d \ No newline at end of file +sleep 30d + +# celery -A TiBillet worker -l INFO \ No newline at end of file diff --git a/DjangoFiles/launch_celery.sh b/DjangoFiles/launch_celery.sh new file mode 100755 index 0000000..c2286ff --- /dev/null +++ b/DjangoFiles/launch_celery.sh @@ -0,0 +1 @@ +celery -A TiBillet worker -l INFO \ No newline at end of file diff --git a/Docker/Development/docker-compose.yml b/Docker/Development/docker-compose.yml index 85a9dd0..7ee2cd6 100644 --- a/Docker/Development/docker-compose.yml +++ b/Docker/Development/docker-compose.yml @@ -14,6 +14,11 @@ services: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_DB: ${POSTGRES_DB} + redis: + image: redis:6-alpine + restart: always + container_name: redis + hostname: redis tibillet_django: container_name: tibillet_django @@ -30,3 +35,17 @@ services: command: "bash /DjangoFiles/launch.sh" depends_on: - tibillet_postgres + - redis + + tibillet_celery: + container_name: tibillet_celery + build: ../Dockerfile/ + restart: always + env_file: .env + volumes: + - "../../DjangoFiles:/DjangoFiles" + - "../../Docker/bashrc:/root/.bashrc" + working_dir: /DjangoFiles + command: "bash /DjangoFiles/launch.sh" + depends_on: + - tibillet_django diff --git a/Docker/Development/env_example b/Docker/Development/env_example index 2598710..b43c8d4 100644 --- a/Docker/Development/env_example +++ b/Docker/Development/env_example @@ -6,6 +6,9 @@ POSTGRES_USER='tibillet_postgres_user' POSTGRES_PASSWORD='tibillet_postgres_password' POSTGRES_DB='tibillet' +CELERY_BROKER='redis://redis:6379/0' +CELERY_BACKEND='redis://redis:6379/0' + DEBUG_DJANGO=True TIME_ZONE='Indian/Reunion' diff --git a/Docker/Dockerfile/dockerfile b/Docker/Dockerfile/dockerfile index 6ab38d8..d11d328 100644 --- a/Docker/Dockerfile/dockerfile +++ b/Docker/Dockerfile/dockerfile @@ -63,27 +63,20 @@ RUN pip install django-stdimage RUN pip install stripe -# RUN apt-get install -y fonts-font-awesome -# RUN apt-get install -y libffi-dev -# RUN apt-get install -y libgdk-pixbuf2.0-0 -# RUN apt-get install -y libpango1.0-0 -# RUN apt-get install -y python-dev -# RUN apt-get install -y python-lxml -# RUN apt-get install -y shared-mime-info -# RUN apt-get install -y libcairo2 - RUN apt install -y python3-cffi RUN apt install -y python3-brotli RUN apt install -y libpango-1.0-0 RUN apt install -y libpangoft2-1.0-0 RUN pip install django-weasyprint + +RUN pip install celery +RUN pip install redis +RUN pip install tenant-schemas-celery + RUN apt-get -y clean RUN python --version RUN django-admin --version RUN pip freeze -RUN weasyprint --version -RUN pip freeze | grep weasyprint -