Home > Back-end >  OperationalError, Error 111 connecting to 127.0.0.1:6379. Connection refused. After deploying in her
OperationalError, Error 111 connecting to 127.0.0.1:6379. Connection refused. After deploying in her

Time:12-12

I am getting the below error after I deployed my website on heroku.

Error 111 connecting to 127.0.0.1:6379. Connection refused.
Request Method: POST
Request URL:    https://website.herokuapp.com/account/register
Django Version: 3.2.8
Exception Type: OperationalError
Exception Value:    
Error 111 connecting to 127.0.0.1:6379. Connection refused.
Exception Location: /app/.heroku/python/lib/python3.8/site-packages/kombu/connection.py, line 451, in _reraise_as_library_errors
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.8.12
Python Path:    
['/app',
 '/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python38.zip',
 '/app/.heroku/python/lib/python3.8',
 '/app/.heroku/python/lib/python3.8/lib-dynload',
 '/app/.heroku/python/lib/python3.8/site-packages']
Server time:    Sat, 11 Dec 2021 21:17:12  0530

So basically my website has to send email regarding otp, after registration and also some contract related emails. These email are neccessary to be sent hence can't be avoided. I posted a question earlier here regardig how to minimize the time that sending emails takes so that the user doesn't have to wait the entire time. I was suggested to use asynchronous code for this. So i decided to use celery for this. I followed the youtube video that taught how to use it.

Now after I pushed the code in the website I am getting this error. I am beginner andd have no idea how to rectify it. Please suggest me what shoul I do. Below are the details and configurations.

settings.py


CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT =['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SELERLIZER = 'json'

requirements.txt

amqp==5.0.6
asgiref==3.4.1
billiard==3.6.4.0
celery==5.2.1
click==8.0.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
colorama==0.4.4
Deprecated==1.2.13
dj-database-url==0.5.0
Django==3.2.8
django-ckeditor==6.1.0
django-filter==21.1
django-js-asset==1.2.2
django-multiselectfield==0.1.12
dnspython==2.1.0

As I mentioned I am beginer, please suggest me a detailed ans as to how I can recctify this error.

CodePudding user response:

Here's the problem:

CELERY_BROKER_URL = 'redis://127.0.0.1:6379'

Redis won't be running on your local dyno. You'll have to run it somewhere else and configure your code to connect to it. A common choice is to run Redis via an addon:

Once you’ve chosen a broker, create your Heroku app and attach the add-on to it. In the examples we’ll use Heroku Redis as the Redis provider but there are plenty of other Redis providers in the Heroku Elements Marketplace.

If you choose to use Heroku Redis, you'll be able to get the connection string to your instance via the REDIS_URL environment variable:

Heroku add-ons provide your application with environment variables which can be passed to your Celery app. For example:

import os
app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

Your Celery app now knows to use your chosen broker and result store for all of the tasks you define in it.

Other addons will provide similar configuration mechanisms.

All quoted documentation here, and most links, come from Heroku's Using Celery on Heroku article. I suggest you read the entire document for more information.

  • Related