Django Celery not discovering the tasks

I have an app called taskapp in my Django project which is reponsible for celery
taskapp/celery.py

...
...
class CeleryAppConfig(AppConfig):
    name = "tasksapp"
    verbose_name = "Celery Config"

    def ready(self):
        installed_apps = [app_config.name for app_config in apps.get_app_configs()]
        app.autodiscover_tasks(lambda: installed_apps, force=True)

@app.task()
def debug_task():
    print("Debug task")

In another app called toy, I have tasks.py file where I have async task defined
toy/tasks.py

import logging

from tasksapp.celery import app

logger = logging.getLogger(__name__)

@app.task()
def destroy_toy(toy_id):
   ...
   ...
   ...

The problem is celery does discover that debug_task but not the task in toy app and other apps’ tasks as well.

I cannot figure it out what causes this issue.
Please help me.

How do you know that celery is not discovering those tasks? What error messages and/or unexpected behavior are you seeing?

Hi, @KenWhitesell

This is what I see.

 
 -------------- celery@mrpy v5.0.5 (singularity)
--- ***** ----- 
-- ******* ---- Linux-5.4.0-66-generic-x86_64-with-glibc2.29 2021-03-09 16:36:49
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         scorbit:0x7f84ab9189d0
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . tasksapp.celery.debug_task

[2021-03-09 16:36:49,997: INFO/MainProcess] Connected to redis://localhost:6379/0
[2021-03-09 16:36:50,002: INFO/MainProcess] mingle: searching for neighbors
[2021-03-09 16:36:51,016: INFO/MainProcess] mingle: all alone
[2021-03-09 16:36:51,023: INFO/MainProcess] celery@mrpy ready.

As you can see, Celery discovers that debug_task only.

Is your AppConfig in celery.py? If so, have you told Django to use that AppConfig? My guess is Django is totally ignoring your celery.py module and, thus, ignoring all tasks.py modules.

If you’re going to use a different AppConfig outside of a default one generated by startapp, you probably need to set default_app_config. Here are the relevant docs.

https://docs.djangoproject.com/en/3.1/ref/applications/#configuring-applications

I added that AppConfig to INSTALLED_APPS.
So it shouldn’t be a problem

How did you solve it ?

I tried it on another machine, and it worked.
So I guess it’s my environment that is problematic.