I have created a Django
app which is using Celery
for task processing. It’s working fine locally but when I pushed it to Heroku
, I found that app cannot connect to Celery worker.
I can see worker is running without any problem.
$ heroku ps
=== web (Free): gunicorn my_django_project.wsgi --log-file - (1)
web.1: up 2019/08/10 11:54:19 +0530 (~ 1m ago)
=== worker (Free): celery -A my_django_project worker -l info (1)
worker.1: up 2019/08/10 11:54:19 +0530 (~ 1m ago)
But when I checked the logs I found this error message
2019-08-10T06:04:30.402781+00:00 app[worker.1]: [2019-08-10 06:04:30,402: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
2019-08-10T06:04:30.402801+00:00 app[worker.1]: Trying again in 32.00 seconds...
Seems like app cannot connect to celery worker.
Project directory structure
Project
|
├── data/
├── db.sqlite3
├── manage.py
├── Procfile
├── README.md
├── requirements.txt
├── my_app/
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── tasks.py
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── my_django_project/
│ ├── celery.py
│ ├── __init__.py
│ ├── settings.py
│ ├── staticfiles
│ ├── urls.py
│ └── wsgi.py
├── runtime.txt
└── static/
Procfile
web: gunicorn my_django_project.wsgi --log-file -
worker: celery -A my_django_project worker -l info
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
#set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', my_django_project.settings')
app = Celery(my_django_project)
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
tasks.py
from celery import shared_task
@shared_task
def task1():
// do stuff
return results
amqp://guest:**@127.0.0.1:5672
Refers to the backend celery tries to use, you need to setup a backend either rabbitMQ, Redis or something else.