ignabe
ignabe

Reputation: 2320

Django + Celery 4 wrong settings

I'm using Django 1.10 and Celery 4.

I found leaks in Celery's Documentation :(

The worker configuration is well done and It run fine (I can see the worker connected in RabbitMQ webmin). But my tasks can't connect to RabbitMQ to publish their messages.

settings.py

CELERY_BROKER_URL = 'amqp://dev:dev@localhost/dev_virtualhost'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

celery.py

from __future__ import absolute_import
import os
from celery import Celery

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

app = Celery('dev_app')
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))

tasks.py

from celery import shared_task

@shared_task(queue='data_to_write')
def test_task(data):
    open('/tmp/test', 'w').write(data)

From Django's Shell I run this code and fail :(

test_task.delay('hello world')

AccessRefused: (0, 0): (403) ACCESS_REFUSED - Login was refused using authentication mechanism AMQPLAIN. For details see the broker logfile.

In the RabbitMQ logs I see that credentials are guest:guest, no dev:dev like I wrote in the settings.py.

Where is my error? Thanks

Upvotes: 4

Views: 1854

Answers (1)

ignabe
ignabe

Reputation: 2320

The solution consists in rename celery.py to celery_app.py to avoid auto import.

Then you must execute celery in this way: celery --app=PACKAGE.celery_app:app worker

PACKAGE is the module (folder) where you put the celery_app.py

Upvotes: 3

Related Questions