N
N
nurzhannogerbek2018-06-02 15:48:14
Django
nurzhannogerbek, 2018-06-02 15:48:14

Why are tasks not running in Celery 4 + Django?

Hello!
There is a task that needs to be run periodically. The task amount_countingwrites to the database. For this task I use Celery, and Redis is used as a broker.
I start redis, then I run the following commands:

$ celery -A TestProject worker -l info
$ celery -A TestProject beat -l info

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

Please help me understand where I made a mistake.
celery.py: (the file is in the same directory as the default settings.py file)
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestProject.settings')

app = Celery('TestProject')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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

init.py: (the file is in the same directory as the settings.py file by default)
from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

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'

# Other Celery settings
CELERY_BEAT_SCHEDULE = {
    'amount-counting': {
        'task': 'profile.tasks.amount_counting',
        'schedule': timedelta(seconds=60), 
    }
}

tasks.py: (the file is in the profile app)
from __future__ import absolute_import, unicode_literals
from celery import task


@task()
def amount_counting():
    # Здесь код

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question