Skip to content

Commit

Permalink
Setup celery.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Jan 28, 2024
1 parent e3897e2 commit f732bb6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
7 changes: 5 additions & 2 deletions AlumniConnect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from __future__ import absolute_import
from .celery import app as celery_app
# This will make sure the Celery app is always imported when
# Django starts so that @shared_task decorator will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)
33 changes: 25 additions & 8 deletions AlumniConnect/celery.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
# Refer Here : https://realpython.com/asynchronous-tasks-with-django-and-celery/
# Refer Here : https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html

from __future__ import absolute_import
import os

from celery import Celery
from django.conf import settings
from celery.schedules import crontab

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

app = Celery('AlumniConnect')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes (no need to convert
# configuration object to byte stream since it can just pass the
# `django.conf:settings` string 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')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AlumniConnect.settings.production')
app = Celery("AlumniConnect")
# Load task modules from all registered Django apps.
app.autodiscover_tasks()

app.config_from_object("django.conf:settings")
app.autodiscover_tasks(lambda : settings.INSTALLED_APPS)
app.conf.beat_schedule = {
'send-birthday-wishes': {
'task': 'applications.alumniprofile.tasks.send_birthday_wishes',
'schedule': crontab(hour=13, minute=5),
},
}

@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
print(f'Request: {self.request!r}')
5 changes: 3 additions & 2 deletions AlumniConnect/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@
TEMPUS_DOMINUS_LOCALIZE = True

# CELERY STUFF
BROKER_URL = 'redis://localhost:6379'
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_TIMEZONE = TIME_ZONE

PASSWORD_RESET_TIMEOUT_DAYS = 1
ANYMAIL = {
# (exact settings here depend on your ESP...)
Expand Down
15 changes: 5 additions & 10 deletions applications/alumniprofile/tasks.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from celery import shared_task
from celery.utils.log import get_task_logger
from datetime import date

from .models import Profile
from .funcs import send_birthday_wish
import datetime

logger = get_task_logger(__name__)


@periodic_task(
run_every=(crontab(hour=13, minute=5)),
name="send_birthday_wish_celery",
ignore_result=True
)
def send_birthday_wish_celery():
@shared_task
def send_birthday_wishes():
today = date.today()
birthday_users = Profile.objects.filter(date_of_birth__day=today.day, date_of_birth__month=today.month)
if birthday_users:
logger.info("{} People have birthdays today".format(len(birthday_users)))
for user in birthday_users:
send_birthday_wish(user.name, user.email)
# send_birthday_wish(user.name, user.email)
logger.info("Mail Sent to {} at {}".format(user.name, user.email))
logger.info("Mail Sent to {} People Today!".format(len(birthday_users)))
else:
Expand Down

0 comments on commit f732bb6

Please sign in to comment.