celery : Received unregistered task of type 'celery_deploy.tasks._stocks'

Viewed 982

When I run celery as below: celery -A bookmarks worker -l INFO

it returned:

[2020-11-10 23:14:21,649: ERROR/MainProcess] Received unregistered task of type 'celery_deploy.tasks._stocks'.
The message has been ignored and discarded.
Did you remember to import the module containing this task?
Or maybe you're using relative imports?
KeyError: 'celery_deploy.tasks._stocks'

my celery.py in project bookmarks directory(where the Django project settings.py is):

from __future__ import  absolute_import, unicode_literals
import os
from celery import Celery, platforms
from datetime import timedelta
from . import settings
BROKER_URL = 'redis://localhost:6379/0'
BACKEND_URL = 'redis://localhost:6379/1'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookmarks.settings')
app = Celery('bookmarks', backend=BACKEND_URL, broker=BROKER_URL)
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda : settings.INSTALLED_APPS)
platforms.C_FORCE_ROOT = True

app.conf.update(
    CELERY_ACKS_LATE = True,
    CELERY_ACCEPT_CONTENT = ['json'],
    CELERY_FORCE_EXECV = True,
    CELERY_CONCURRENCY = 4,
    CELERYD_MAX_TASKS_PER_CHILD = 10,
    CELERYBEAT_SCHEDULE = {
        'get_stock_': {
            'task': 'celery_deploy.tasks._stocks',
            'schedule': timedelta(seconds=21),
        },
    },
)

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

My tasks.py in Django app celery_deploy:

from __future__ import absolute_import
from celery import shared_task
from django.core.cache import cache
from urllib.request import urlopen
from bookmarks.celery import app
import json
import redis

apibase = "https://financialmodelingprep.com/api/v3/"
apikey = "[KEY]"
exchange_open_day = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
json_parsed = {}
CACHE_TIMEOUT = 60*60

@app.task
def _stocks():
    url = apibase + "quotes/nyse" + "?" + apikey
    response = urlopen(url)
    stocks_parsed = json.loads(response.read().decode("utf-8"))
    for _object in stocks_parsed:
        cache.set(_object['symbol'], _object['price'], CACHE_TIMEOUT)

I read many questions similar to mine, but still cannot figure out.

What should I do to fix it?Thank you.

1 Answers

The error is likely to do with your Instantiaion. Assuming you are trying an App-Wide usage, then the following will not work since you are required to set the task_cls attribute.

Here

app = Celery('bookmarks', backend=BACKEND_URL, broker=BROKER_URL)

The bookmarks refers to your directory but is failing to import the required method at runtime.

Can you change the line to include your filename, e.g.

app = Celery('bookmarks.tasks', backend=BACKEND_URL, broker=BROKER_URL)

This should hopefully solve the problem.

Related