Search code examples
rabbitmqcelerydjango-celery

Django & Celery & Rabbit getting Not Registered error


I am trying to set up Django & Celery & Rabbit for the first time following this tutorial. I am using Django 2.0 Celery 4.2.0 and Rabbit on Windows

I am getting the error: celery.exceptions.NotRegistered: 'GeneratePDF'

I have set up as follows:

in my init.py:

from __future__ import absolute_import, unicode_literals
import celery
from .celery import app as celery_app

__all__ = ['celery_app']

in my celery.py:

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

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

app = Celery('abc')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

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

in my tasks.py:

from celery import shared_task
from abc.celery import app
@shared_task(name='GeneratePDF')
class GeneratePDF(View):
    def get(self, request, *args, **kwargs):
      ....

in my views.py:

from abc.tasks import GeneratePDF

@method_decorator(login_required, name='dispatch')
class ClientProfilePDF(RedirectView):

    def get(self, request, *args, **kwargs):
        GeneratePDF.delay(request)
        return  HttpResponseRedirect('/home/')

in my settings.py:

CELERY_BROKER_URL = 'amqp://localhost'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_BACKEND = 'django-db'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Australia/Sydney'
CELERY_IMPORTS = ('abc.tasks',)

Can anyone point me in the right direction as to where I am going wrong and why I am getting this error? Any help is much appreciated!


Solution

  • Two quick things:

    1. No need for any parameters to app.autodiscover_tasks() Celery alreayd knows how to use settings.INSTALLED_APPS.
    2. The @shared_task decorator is for tasks that live in apps that do not have their own celery.py file that instantiates an app. From the looks of it, your tasks.py file lives in the same django app as the celery.py file. In this case, you should use @app.task and not @shared_task.

    before you start, you can get a list of registered tasks by doing celery -A myapp inspect registered. That will let you see if your GeneratePDF task is registered or not.