I'm running a blog web app inside a docker container, I'm trying to use celery and redis for asynchronous sending of welcome emails to newly registered user.
When I run my docker compose up, everything is setup successfully, the celery, redis broker is running as expected, I can share the docker-compose and Dockerfile below if needed.
The issue is, I'm getting a KeyError when trying to execute the tasks in my tasks.py.
This is the error I'm getting below:
celery_1 | [2024-01-22 09:37:50,439: INFO/MainProcess] Events of group {task} e
nabled by remote.
app_1 | [22/Jan/2024 09:39:02] "GET /api/user/create/ HTTP/1.1" 405 8444
celery_1 | [2024-01-22 09:39:16,512: ERROR/MainProcess] Received unregistered t
ask of type 'core.tasks.send_verification_email'.
celery_1 | The message has been ignored and discarded.
celery_1 |
celery_1 | Did you remember to import the module containing this task?
celery_1 | Or maybe you're using relative imports?
celery_1 |
celery_1 | Please see
celery_1 | https://docs.celeryq.dev/en/latest/internals/protocol.html
celery_1 | for more information.
celery_1 |
celery_1 | The full contents of the message body was:
celery_1 | b'[["[email protected]"], {}, {"callbacks": null, "errbacks": nul
l, "chain": null, "chord": null}]' (99b)
celery_1 |
celery_1 | The full contents of the message headers:
celery_1 | {'lang': 'py', 'task': 'core.tasks.send_verification_email', 'id': '
a9e2b679-a94d-4007-851a-30f0f14764f1', 'shadow': None, 'eta': None, 'expires': N
one, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None]
, 'root_id': 'a9e2b679-a94d-4007-851a-30f0f14764f1', 'parent_id': None, 'argsrep
r': "('[email protected]',)", 'kwargsrepr': '{}', 'origin': 'gen10@app', 'ign
ore_result': False, 'replaced_task_nesting': 0, 'stamped_headers': None, 'stamps
': {}}
celery_1 |
celery_1 | The delivery info for this task is:
celery_1 | {'exchange': '', 'routing_key': 'celery'}
celery_1 | Traceback (most recent call last):
celery_1 | File "/py/lib/python3.9/site-packages/celery/worker/consumer/consu
mer.py", line 658, in on_task_received
celery_1 | strategy = strategies[type_]
celery_1 | KeyError: 'core.tasks.send_verification_email'
app_1 | [22/Jan/2024 09:39:16] "POST /api/user/create/ HTTP/1.1" 201 8643
Even when I check for registered task, it shows no task found.
Here's how my celery is configured:
My celery.py:
from __future__ import absolute_import, unicode_literals
import os
import logging
from celery import Celery
logger = logging.getLogger("Celery")
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
# Create a Celery instance and configure it using the settings from Django.
app = Celery('app', include=['core.tasks'])
# Load task modules from all registered Django app configs.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Auto-discover tasks in all installed apps
app.autodiscover_tasks()
init.py:
from .celery import app as celery_app
__all__ = ['celery_app']
My settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'user',
'rest_framework',
'drf_spectacular',
]
CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
CELERY_TASK_TRACK_STARTED = True
CELERY_TIMEZONE = 'UTC'
My tasks.py:
from celery import shared_task
from django.core.mail import send_mail
import logging
logger = logging.getLogger(__name__)
@shared_task
def send_verification_email(user_email):
try:
subject = 'Verify your email'
message = f'Welcome to the test blog application'
from_email = '[email protected]'
recipient_list = [user_email]
send_mail(subject, message, from_email, recipient_list, fail_silently=False)
logger.info(f"Verification email sent to {user_email}")
except Exception as e:
# Log the exception
logger.error(f"Failed to send verification email to {user_email}: {e}")
The tasks is to be executed in the views.py:
"""
Views for the user API.
"""
from core.tasks import send_verification_email
from rest_framework import generics, views
from user.serializers import (
UserSerializer,
)
class CreateUserView(generics.CreateAPIView):
"""Create a new user in the system."""
serializer_class = UserSerializer
def perform_create(self, serializer):
user = serializer.save()
send_verification_email.delay(user.email)
This is how my project struture looks like:
BLOG-APP-API/
│
├── app/
│ ├── __init__.py
│ ├── settings.py
│ ├── celery.py
│ └── # other py files related to the main app
│
├── core/
│ ├── __init__.py
│ ├── tasks.py
│ └── # other py files related to the core functionality
│
├── user/
│ └── views.py
│ └── # other py files related to the user functionality
│
├── Dockerfile
├── docker-compose.yml
└── # other project-level files or directories
I need assistance please, is there anything I'm not doing right.
I've used this one pure django project I built and it worked,I don't know why it has refused to work with this one. Anyone who had had similar experience should please give his/her ideas, Thanks.