Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery autodiscover_tasks not working for all Django 1.7 apps

I have a Django 1.7 project with Celery 3.1. All the apps in my Django project work with the new AppConfig. The problem is that not all the tasks are found with autodiscover_tasks:

app.autodiscover_tasks(settings.INSTALLED_APPS)

If i use autodiscover_tasks like this it wil work:

app.autodiscover_tasks(settings.INSTALLED_APPS + ('apps.core','apps.sales'))

The tasks defined in websites are found but the tasks in core and sales are not. All have the same layout with apps.py and tasks.py.

The project folder structure is:

apps
  core
  apps.py
  tasks.py
dashboard
  apps.py
sales
  apps.py
  tasks.py
websites
  apps.py
  tasks.py

The class definitions are as follows:

class WebsitesConfig(AppConfig):
    name = 'apps.websites'
    verbose_name = 'Websites'

class SalesConfig(AppConfig):
    name = 'apps.sales'
    verbose_name = 'Sales'
like image 773
Krukas Avatar asked Feb 20 '15 12:02

Krukas


2 Answers

This is discussed in a number of Celery issues, such as #2596 and #2597.

If you are using Celery 3.x, the fix is to use:

from django.apps import apps
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

As mentioned in #3341, if you are using Celery 4.x (soon to be released) you can use:

app.autodiscover_tasks()
like image 192
Doddie Avatar answered Nov 15 '22 19:11

Doddie


I just had this problem because of a misconfigured virtual environment.

If an installed app has a dependency missing from the virtual environment in which you're running celery, then the installed app's tasks will not be auto discovered. This hit me as I was moving from running my web server and celery on the same machine to a distributed solution. A bad build resulted in different environment files on different nodes.

I added the dependencies that were missing then restarted the celery service.

like image 32
Sevenless Avatar answered Nov 15 '22 20:11

Sevenless