tikej
tikej

Reputation: 312

Celery is connecting to amqp://guest**@localhost instead of Redis. Heroku deploy

I have read simillar topics and done everything there was being suggested, but the problem remains.

I am deploying my app to Heroku. Locally everything was working fine, but during deploy after specyfying every setting I could think off to specify the celery worker sends following error:

:22:50.722826+00:00 app[worker.1]: [2018-10-21 18:22:50,722: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.

I tried switching from CloudAMQP to Redis. And the problem remains.

Here are my config files:

Django's settings.py:

try: 
    CELERY_BROKER_URL = os.environ['REDIS_URL']
except KeyError: 
    CELERY_BROKER_URL = 'amqp://admin:admin@localhost:5672/admin_host'

try:
    CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
except KeyError:
    CELERY_RESULT_BACKEND = 'amqp://admin:admin@localhost:5672/admin_host'


CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

django_heroku.settings(locals())

celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from . import settings

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

app = Celery('file_upload', broker_pool_limit=1)

app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

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

__init__.py from the package containig celery.py and settings.py:

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

and the os.environ['REDIS_URL'] does return a url, I checked it.

anyone help?

Upvotes: 1

Views: 3915

Answers (3)

tikej
tikej

Reputation: 312

Couple of days fighting with this thing, and just after posting this I answered myself. Somebody may find this useful.

In my case the key for solving this was that I hardcoded redis URL in celery.py and passed it as argument while creating celery app object:

app = Celery('file_upload', broker_pool_limit=1, broker=redis_url,
             result_backend=redis_url)

Upvotes: 5

Fargo
Fargo

Reputation: 11

In local/development environments, when we use kombu instead of ampq, celery broker url configuration it's set like:

CELERY_BROKER_URL = 'django://'  # for example

In your celery.py the call to the constructor for create the celery app must be set like this:

django_url = settings.CELERY_BROKER_URL
app = Celery('tone_insurance', broker=django_url, result_backend=django_url)

This is based on the answer here

Upvotes: 0

Dantheman91
Dantheman91

Reputation: 884

In order to solve this, I think what needs to be done is change the CELERY_BROKER_URL var to BROKER_URL. It should only be CELERY_BROKER_URL if you give it the namespace CELERY when you're fetching the config from the object. Change from:

app.config_from_object('django.conf:settings')

To:

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

Upvotes: 2

Related Questions