Skip to content

Commit

Permalink
Merge pull request #2228 from aswanthabam/perf-register
Browse files Browse the repository at this point in the history
feat :added celery for running mail
  • Loading branch information
shaheenhyderk authored Sep 25, 2024
2 parents 8894324 + 775a812 commit 5d50e64
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ RUN apt-get update -y && apt-get install -y default-libmysqlclient-dev python-d
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
ENTRYPOINT sh entrypoint.sh
# EXPOSE 8000
# ENTRYPOINT sh entrypoint.sh
9 changes: 5 additions & 4 deletions api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .register_helper import get_auth_token
from django.views.decorators.cache import cache_page
from django.core.cache import cache
from mu_celery.task import send_email


class UserRegisterValidateAPI(APIView):
Expand Down Expand Up @@ -119,10 +120,10 @@ def post(self, request):

response_data = serializers.UserDetailSerializer(user, many=False).data

send_template_mail(
context=response_data,
subject="YOUR TICKET TO µFAM IS HERE!",
address=["user_registration.html"],
send_email.delay(
response_data,
"YOUR TICKET TO µFAM IS HERE!",
["user_registration.html"],
)

res_data["data"] = response_data
Expand Down
17 changes: 16 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: "3"
services:
mulearnbackend:
build:
Expand All @@ -15,3 +15,18 @@ services:
- /var/www/mulearnbackend/media:/app/media
env_file:
- .env
entrypoint: sh entrypoint.sh
celery:
build:
context: .
dockerfile: Dockerfile
container_name: celery
image: mulearnbackend-celery
restart: always
volumes:
- /var/log/mulearnbackend:/var/log/mulearnbackend
- /var/www/mulearnbackend/assets:/app/assets
- /var/www/mulearnbackend/media:/app/media
env_file:
- .env
command: celery -A mulearnbackend.celery worker -l info
Empty file added mu_celery/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions mu_celery/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from celery import shared_task
from utils.utils import send_template_mail


@shared_task
def send_email(context: dict, subject: str, address: list[str], attachment: str = None):
return send_template_mail(context, subject, address, attachment)
5 changes: 5 additions & 0 deletions mulearnbackend/__init__.py
Original file line number Diff line number Diff line change
@@ -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",)
17 changes: 17 additions & 0 deletions mulearnbackend/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

from celery import Celery

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mulearnbackend.settings")

app = Celery("mulearnbackend")

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")

# Load task modules from all registered Django apps.
app.autodiscover_tasks()
3 changes: 3 additions & 0 deletions mulearnbackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
},
}

CELERY_BROKER_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/2"
CELERY_RESULT_BACKEND = f"redis://{REDIS_HOST}:{REDIS_PORT}/2"

# Use the Redis cache as the default cache
CACHES["default"] = CACHES["redis"]

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ qrcode==7.4.2
pymysql==1.0.2
razorpay==1.4.2
reportlab==4.2.0
django_redis==5.4.0
django_redis==5.4.0
celery==5.4.0

0 comments on commit 5d50e64

Please sign in to comment.