Celery with redis and django

Hello All.

I used to work with rabbitmq but with celery I am having these problems. Is there something I’m not adding in setting.py or celery.py?

kombu.exceptions.OperationalError: Error -2 connecting to redis:6379. Name or service not known.

I having this console output

return view_func(*args, **kwargs)
callservicebackend-backendcallservices-1 | File “/code/callserviceapp/views.py”, line 141, in register
callservicebackend-backendcallservices-1 | send_user_mail.delay(randomNumber, email)
callservicebackend-backendcallservices-1 | File “/usr/local/lib/python3.8/site-packages/celery/app/task.py”, line 425, in delay
callservicebackend-backendcallservices-1 | return self.apply_async(args, kwargs)
callservicebackend-backendcallservices-1 | File “/usr/local/lib/python3.8/site-packages/celery/app/task.py”, line 575, in apply_async
callservicebackend-backendcallservices-1 | return app.send_task(
callservicebackend-backendcallservices-1 | File “/usr/local/lib/python3.8/site-packages/celery/app/base.py”, line 788, in send_task
callservicebackend-backendcallservices-1 | amqp.send_task_message(P, name, message, **options)
callservicebackend-backendcallservices-1 | File “/usr/local/lib/python3.8/contextlib.py”, line 131, in exit
callservicebackend-backendcallservices-1 | self.gen.throw(type, value, traceback)
callservicebackend-backendcallservices-1 | File “/usr/local/lib/python3.8/site-packages/kombu/connection.py”, line 450, in _reraise_as_library_errors
callservicebackend-backendcallservices-1 | raise ConnectionError(str(exc)) from exc
callservicebackend-backendcallservices-1 | kombu.exceptions.OperationalError: Error -2 connecting to redis:6379. Name or service not known.
callservicebackend-backendcallservices-1 | [01/Apr/2023 13:54:41] “POST /registro/ HTTP/1.0” 500 231177

This is the celery.py configuration:

    from __future__ import absolute_import, unicode_literals
    import os
    from celery import Celery
    from django.conf import settings
    from django.core.mail import EmailMultiAlternatives, send_mail
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'callservices.settings')
    app = Celery('callservices',broker='redis://redis:6379/0')
    app.config_from_object('django.conf:settings')
    app.autodiscover_tasks(settings.INSTALLED_APPS)

    @app.task()
    def send_user_mail(randomNumber, email):
        subject = 'Email validation'
        cuerpo="The random number is: "+str(randomNumber)
    
        #message.attach_alternative(content, 'text/html')
        send_mail(subject, cuerpo ,'xxxxx.xxxxxxx@gmail.com', [email],fail_silently = False) #Destinatario)
        return 1

And this is the setting.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

ALLOWED_HOSTS=['*',]

# Application definition

INSTALLED_APPS = [
    'sslserver',
    'rest_framework',
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'callserviceapp.apps.CallserviceappConfig',
    'rest_framework.authtoken',

]


#CELERY_BROKER_URL = 'amqp://localhost'
CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.common.CommonMiddleware',
    
]
#......

Do you have a system with a DNS entry for the name “redis”? If not, you need to change that to the dns name / ip address of the server running redis.

1 Like