Matin Najafi
Matin Najafi

Reputation: 111

Rabbitmq not showing messages in django

I'm reading Django 3 by example book and in Chapter 7 of the book we use rabbitmq,celery and flower. I configed rabbitmq,celery and flower but there is a few problems my task is a email that it sends after a order is created and the task is executed in celery terminal and in flower flower panel but i cant see the email. and in rabbitmq panel the messages are unacked and the other problem is that broker tab in flower is empty and it does'nt show rabbitmq. Here is the screenshots and my code.

rabbitmq server is runnig

rabbitmq admin panel

flower panel

celery logs

Here is my tasks.py:

from __future__ import absolute_import, unicode_literals
from celery import shared_task
from django.core.mail import send_mail
from .models import Order


@shared_task
def order_created(order_id):
    """Task to send an e-mail notification when order is successfully created."""
    order = Order.objects.get(id=order_id)
    subject = f'Order nr. {order.id}'
    message = f'Dear {order.first_name},\n\n' \
              f' You have successfully placed an order.' \
              f'Your order ID is {order.id}'

    mail_sent = send_mail(subject, message, '[email protected]', [order.email])
    return mail_sent

celery config file, celery.py:

import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Onlineshop.settings')

broker = "amqp://test:test@localhost:5672/"

app = Celery('Onlineshop', broker=broker)

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

flowr config file:

# RabbitMQ management api
broker_api = 'http://guest:guest@localhost:5672/api/'

# Enable debug logging
logging = 'DEBUG'

views.py:

def order_create(request):
cart = Cart(request)
if request.method == 'POST':
    user = request.user
    form = OrderCreateForm(request.POST)
    if form.is_valid():
        order = form.save(commit=False)
        order.user = user
        order.save()
        for item in cart:
            OrderItem.objects.create(order=order, product=item['product'], price=item['price'],
                                     quantity=item['quantity'])
        context = {
            'order': order,
        }
        # clear the cart
        cart.clear()
        # launch asynchronous task
        order_created.delay(order.id)
        # set the order in the session
        request.session['order_id'] = order.id
        # redirect for payment
        return redirect(reverse('payment:process'))
else:
    form = OrderCreateForm()
context = {'cart': cart,
           'form': form,
           }
return render(request, 'order/create.html', context)

it seems everything is ok because the tasks are recieved in celery and they show too in rabbitmq panel messages but there are unacked but still i can not see the email in my run terminal. My Email setting are correct and i'm using django email backend. i'm on windows 11 and using celery v5.2.2 and rabbitmq v3.9.11

Upvotes: 1

Views: 574

Answers (1)

jjdoherty
jjdoherty

Reputation: 504

It states in your screenshot that the task is received but execution phase never appears to happen, what I would recommend checking is that when your celery worker starts it will state a list of all the celery tasks the worker recognises it exists. First update your logging when starting the worker, celery -A Onlineshop worker -Q celery,emailQueue --loglevel=INFO. If the task order_created is never listed when the celery worker starts then this indicates that there is an issue with the imports when the celery worker starts and a __init__.py for the python module might need updated to auto import the celery task.

Another possible reason for this to occur is that the celery task might be assigned to a queue that the worker isn't listening for. I would check your arguments for when starting the celery worker include all the correct queue names. celery -A Onlineshop worker -Q celery,emailQueue

Upvotes: 1

Related Questions