Django Celery calling redis but I am using RabbitMq as Message broker

I am using celery and RabbitMq as message broker with Django and Docker.

after putting my docker image up I run the following command: docker-compose run --rm web bash to enter my container and then I run: celery -A wine_shop worker --loglevel=info instead of getting a confirmation that my borker is working I get the following message:

[2022-08-31 09:30:57,796: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6380//: Error 99 connecting to localhost:6380. Cannot assign requested address..
Trying again in 2.00 seconds... (1/100)

I can notice that the transport is wrong, it should be calling the py-amqp transport but it's calling redis.

 
 -------------- celery@4c40607a9f71 v5.2.6 (dawn-chorus)
--- ***** ----- 
-- ******* ---- Linux-5.15.0-46-generic-x86_64-with-glibc2.31 2022-08-31 09:30:57
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         wine_shop:0x7f2546178460
- ** ---------- .> transport:   redis://localhost:6380//
- ** ---------- .> results:     
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

Any ideas on how to solve it?

Down here you can find my configuration files:

Settings.py

from pathlib import Path
import os
from decouple import config


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-+%)59bb-wx7*0j9625oa&zs8(!&gh2xv+rplfm_uzlhh6sw*aj'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [
    '0.0.0.0', '127.0.0.1', 'localhost', 'wine-shop-project.herokuapp.com/',
    'wine-shop-project.herokuapp.com'
]

CSRF_TRUSTED_ORIGINS = ["https://wine-shop-project.herokuapp.com"]

# a
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'shop.apps.ShopConfig',
    'cart.apps.CartConfig',
    'payment.apps.PaymentConfig',
    'orders.apps.OrdersConfig',
    'django_celery_beat',
    'django_celery_results',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

ROOT_URLCONF = 'wine_shop.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'cart.context_processors.cart',
            ],
        },
    },
]

WSGI_APPLICATION = 'wine_shop.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '...',
        'USER': '...',
        'PASSWORD':
        '...',
        'HOST': '...',
        'PORT': '5432',
    }
}

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticFiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME':
        'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME':
        'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


CART_SESSION_ID = 'cart'

# STRIP SETTINGS
STRIPE_PUBLISHABLE_KEY = '...'  # PUBLISHABLE KEY
STRIPE_SECRET_KEY = '...'  #SECRET KEY

STRIPE_WEBHOOK_SECRET = '...'

#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#  Email server configuration
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '...'
EMAIL_HOST_PASSWORD = '...'
EMAIL_PORT = 587
EMAIL_USE_TLS = True


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


#broker_url = 'amqp://victor:PASSWORD_FOR_EVENTS@localhost:5672/victor'
CELERY_BROKER_URL = 'amqp://rabbitmq'
CELERY_BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
CELERY_RESULT_BACKEND = 'amqp://rabbitmq'
BROKER_URL = 'amqp://rabbitmq'

#CELERY_ENABLED = True
#EVENTS_PUSH_BACKEND_OPTIONS = {"url": "amqp://victor:PASSWORD_FOR_EVENTS@localhost:5672/victor"}

Celery.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from __future__ import absolute_import, unicode_literals
import os
from django.conf import settings
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wine_shop.settings')

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




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

Docker-compose.yml

version: "3.8"
   
services:
     

    web:
        build: .
        container_name: django2 
        command: >
            sh -c "python manage.py migrate &&
                   python manage.py runserver 0.0.0.0:8000"
        volumes:
            - app_data:/usr/src/app/
        ports:
            - "8000:8000"
        env_file:
            - ./env.dev
        

        depends_on:
            - pgdb
            - rabbitmq
    celery:
        restart: always
        build: 
            context: .
        container_name: celery
        command: celery -A wine_shop worker -l info
        volumes:
            - .:/usr/src/app
        env_file:
            - ./.env.dev

        depends_on:
            - pgdb
            - web
            - rabbitmq
    pgdb:
        image: postgres
        container_name: pgdb
        env_file:
            - ./.env.dev
        volumes:
            - pgdata:/var/lib/postgresql/data/

    rabbitmq:
        image: rabbitmq:3.7-alpine
        container_name: 'rabbitmq'



volumes:
    pgdata:
    app_data:

Thanks for everyone!!!

I faced a similar problem as I configured my celery to use redis but my django app was still using rabbit mq

if fixed it by add the below lines in proj/init.py

from .celery import app as celery_app
__all__ = ("celery_app",)

I hope this helps someone passing here

Back to Top