2
votes

I am trying to setup a task to run every ten seconds.Using Celery Beat.

I am using:

 Django==1.11.3
 celery==4.1.0
 django-celery-beat==1.1.1
 django-celery-results==1.0.1

It is giving me the following error:

Received unregistered task of type 'operations.tasks.message'

I am new to Celery, I have tried numerous solutions and cannot seem to find a solution,would appreciate the help

settings.py

CELERY_BROKER_URL = 'pyamqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'django-db'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Johannesburg'
CELERY_BEAT_SCHEDULE = {
        'message': {
        'task': 'operations.tasks.message',
        'schedule': 10.0
    }
    }

celery.py

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', 'nodiso.settings')

app = Celery('nodiso')

# Using a string here means the worker don'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

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']

task.py

from __future__ import absolute_import, unicode_literals
from celery import shared_task
from operations import models
from .celery import periodic_task



@task
def message():
    t = models.Celerytest.objects.create(Message='Hello World')
    t.save()

files structure

proj-
     proj-
         __init__.py
         settings.py-
         celery.py-
     app-
         tasks.py-
1
IIRC Celery taks names are (by default) the method names, so your task is probably registered with just the name "message", rather than as "operations.tasks.message" from your config. - Tom Dalton
your file is named task.py (at least in the description of your problem), but you are giving this path instead operations.tasks.message, note the S. Also, you are using the decorator @task but it is not defined (imported) - trinchet

1 Answers

1
votes

Within my celery.py file I define app like this:

app = Celery(
    'your_celery_app_name',
    include=[
        'your_celery_app_name.module.task1',
        'your_celery_app_name.module.task2',
    ]
)
app.config_from_object('your_celery_app_name.celeryconfig')

My celeryconfig.py is where I define my beats and other settings (I think this would be same as your settings.py).

Below is probably not relevant - I'm not an expert with Python and how package should be put together - but from my limited understanding your tasks should be a submodule of your celery app module. Take this with pinch of salt though.

My project structure looks more like this:

your_celery_app_name (dir)
    setup.py (file)
    your_celery_app_name (dir)
        __init__.py (file)
        celery.py (file)
        celeryconfig.py (file)
        module (dir)
            __init__.py (importing task1 and task2 from tasks)
            tasks.py (implementing task1 and task2)