N
N
nurzhannogerbek2018-06-04 07:50:06
Django
nurzhannogerbek, 2018-06-04 07:50:06

How to properly register a task in Celery 4?

Hello! I ran into a problem that Celery does not see tasks from the profile application. When trying to start Celery, an error occurs (see below). The list of registered tasks does not contain the task that I need (in my case, amount_counting). At the same time, if you put the task file in celery.pydjango.setup() , it appears in the list of registered tasks and the task itself can be launched, but in such cases, the runserver command already gives an error. Could anyone advise what to do?
Structure:

TestProject
   TestProject
      - settings.py
      - __init__.py
      - celery.py
  profile
      - tasks.py

celery.py:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestProject.settings')
app = Celery('TestProject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

settings.py:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Almaty'
CELERY_BEAT_SCHEDULE = {
    "amount-counting": {
        "task": "profile.tasks.amount_counting",
        "schedule": 60.0,
    },
}

__init__.py:
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
 
__all__ = ['celery_app']

profile/tasks.py:
from __future__ import absolute_import, unicode_literals
from celery import task
 
 
@task
def amount_counting():
    # Some Code

ERROR:
[2018-06-04 04:29:22,641: INFO/Beat] Scheduler: Sending due task amount-counting (profile.tasks.amount_counting)
[2018-06-04 04:29:22,654: ERROR/MainProcess] Received unregistered task of type 'profile.tasks.amount_counting'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)
Traceback (most recent call last):
  File "/Users/nurzhan_nogerbek/Virtualenvs/enjoy_jumping/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 557, in on_task_received
    strategy = strategies[type_]
KeyError: 'profile.tasks.amount_counting'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nurzhannogerbek, 2018-06-05
@nurzhannogerbek

I found the problem and I must say it was unexpected for me.
In the tasks.py file, I specified all the dependencies at the beginning of the file as usual. So just the dependencies put in the body of the function the amount_countingproblem disappeared. I would be glad if someone could explain why this is so.

from __future__ import absolute_import, unicode_literals
from celery import task
 
 
@task
def amount_counting():
    from  app_name.models import ModelName
    # Some other code

A
Artem, 2018-06-04
@ulkoart

zhmak - a normal example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question