Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unregistered task of type 'users.tasks.get_users_count' in celery #4763

Open
re4lfl0w opened this issue Dec 24, 2023 · 0 comments
Open

unregistered task of type 'users.tasks.get_users_count' in celery #4763

re4lfl0w opened this issue Dec 24, 2023 · 0 comments
Labels

Comments

@re4lfl0w
Copy link

re4lfl0w commented Dec 24, 2023

What happened?

unregistered task of type 'users.tasks.get_users_count'

What should've happened instead?

return users count

I tried many thins

1. create new app and wrote tasks.py

I try startapp shop.
tasks.py in shop

# tasks.py

from config import celery_app


@celery_app.task()
def add(a: int, b: int) -> int:
    return a + b
In [1]: from shop.tasks import *

In [2]: add.delay(1,2).get()
Out[2]: 3

result: Good working

[2023-12-24 19:36:28,767: INFO/MainProcess] Task shop.tasks.add[c6eaabd8-cf80-4d21-9974-000bf7a9fe9d] received
[2023-12-24 19:36:28,767: DEBUG/MainProcess] TaskPool: Apply <function fast_trace_task at 0x106824700> (args:('shop.tasks.add', 'c6eaabd8-cf80-4d21-9974-000bf7a9fe9d', {'lang': 'py', 'task': 'shop.tasks.add', 'id': 'c6eaabd8-cf80-4d21-9974-000bf7a9fe9d', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': 'c6eaabd8-cf80-4d21-9974-000bf7a9fe9d', 'parent_id': None, 'argsrepr': '(1, 2)', 'kwargsrepr': '{}', 'origin': 'gen19317@Neos-MacBook-Pro.local', 'ignore_result': False, 'replaced_task_nesting': 0, 'stamped_headers': None, 'stamps': {}, 'properties': {'correlation_id': 'c6eaabd8-cf80-4d21-9974-000bf7a9fe9d', 'reply_to': 'bad5509d-676c-3c89-bd15-64929f7e7df0', 'delivery_mode': 2, 'delivery_info': {'exchange': '', 'routing_key': 'celery'}, 'priority': 0, 'body_encoding': 'base64', 'delivery_tag': '4a28df74-63a1-4027-ae9b-a30433cc53ab'}, 'reply_to': 'bad5509d-676c-3c89-bd15-64929f7e7df0', 'correlation_id': 'c6eaabd8-cf80-4d21-9974-000bf7a9fe9d', 'hostname': 'celery@Neos-MacBook-Pro.local', 'delivery_info': {'exchange': '',... kwargs:{})
[2023-12-24 19:36:28,772: INFO/ForkPoolWorker-8] Task shop.tasks.add[c6eaabd8-cf80-4d21-9974-000bf7a9fe9d] succeeded in 0.004394083996885456s: 3

2. add function in app_name.app_name.users.tasks

# app_name.config.celery_app.py

import os

from celery import Celery

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

app = Celery(
    "app_name",
)

# 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()

# tasks.py

from django.contrib.auth import get_user_model

from config import celery_app

User = get_user_model()


@celery_app.task()
def get_users_count():
    """A pointless Celery task to demonstrate usage."""
    return User.objects.count()


@celery_app.task()
def add(a: int, b: int) -> int:
    return a + b
In [1]: from shop.tasks import *

In [2]: add.delay(1,2).get()

NotRegistered                             Traceback (most recent call last)
Cell In[4], line 1
----> 1 add.delay(1,2).get()

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/celery/result.py:251, in AsyncResult.get(self, timeout, propagate, interval, no_ack, follow_parents, callback, on_message, on_interval, disable_sync_subtasks, EXCEPTION_STATES, PROPAGATE_STATES)
    248     return self.result
    250 self.backend.add_pending_result(self)
--> 251 return self.backend.wait_for_pending(
    252     self, timeout=timeout,
    253     interval=interval,
    254     on_interval=_on_interval,
    255     no_ack=no_ack,
    256     propagate=propagate,
    257     callback=callback,
    258     on_message=on_message,
    259 )

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/celery/backends/asynchronous.py:223, in AsyncBackendMixin.wait_for_pending(self, result, callback, propagate, **kwargs)
    221 for _ in self._wait_for_pending(result, **kwargs):
    222     pass
--> 223 return result.maybe_throw(callback=callback, propagate=propagate)

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/celery/result.py:365, in AsyncResult.maybe_throw(self, propagate, callback)
    362 state, value, tb = (
    363     cache['status'], cache['result'], cache.get('traceback'))
    364 if state in states.PROPAGATE_STATES and propagate:
--> 365     self.throw(value, self._to_remote_traceback(tb))
    366 if callback is not None:
    367     callback(self.id, value)

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/celery/result.py:358, in AsyncResult.throw(self, *args, **kwargs)
    357 def throw(self, *args, **kwargs):
--> 358     self.on_ready.throw(*args, **kwargs)

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/vine/promises.py:235, in promise.throw(self, exc, tb, propagate)
    233 if tb is None and (exc is None or exc is current_exc):
    234     raise
--> 235 reraise(type(exc), exc, tb)

File ~/.pyenv/versions/app_name/lib/python3.10/site-packages/vine/utils.py:27, in reraise(tp, value, tb)
     25 if value.__traceback__ is not tb:
     26     raise value.with_traceback(tb)
---> 27 raise value

NotRegistered: 'users.tasks.add'

3. stackoverflow answer not working

[Celery Received unregistered task of type (run example)](https://stackoverflow.com/questions/9769496/celery-received-unregistered-task-of-type-run-example?page=1&tab=votes#tab-top)

Additional details

  • Host system configuration:

    • Version of cookiecutter CLI (get it with cookiecutter --version): 2.5.0

    • OS name and version: On Mac

      On MacOs, run

      celery -A config.celery_app worker --loglevel=debug
    • Python version, run python3 -V: 3.10.11

Logs:

[2023-12-24 19:25:47,761: DEBUG/MainProcess] | Worker: Preparing bootsteps.
[2023-12-24 19:25:47,762: DEBUG/MainProcess] | Worker: Building graph...
[2023-12-24 19:25:47,762: DEBUG/MainProcess] | Worker: New boot order: {StateDB, Timer, Hub, Pool, Autoscaler, Beat, Consumer}
[2023-12-24 19:25:47,764: DEBUG/MainProcess] | Consumer: Preparing bootsteps.
[2023-12-24 19:25:47,764: DEBUG/MainProcess] | Consumer: Building graph...
[2023-12-24 19:25:47,769: DEBUG/MainProcess] | Consumer: New boot order: {Connection, Events, Mingle, Gossip, Tasks, Control, Agent, Heart, event loop}

celery@Neos-MacBook-Pro.local v5.3.6 (emerald-rush)

macOS-13.3.1-arm64-arm-64bit 2023-12-24 19:25:47

[config]
.> app: app_name:0x106891ba0
.> transport: redis://localhost:6379//
.> results: redis://localhost:6379/
.> concurrency: 10 (prefork)
.> task events: ON

[queues]
.> celery exchange=celery(direct) key=celery

[tasks]
. celery.accumulate
. celery.backend_cleanup
. celery.chain
. celery.chord
. celery.chord_unlock
. celery.chunks
. celery.group
. celery.map
. celery.starmap
. app_name.users.tasks.add
. app_name.users.tasks.get_users_count
. shop.tasks.add

[2023-12-24 19:25:47,785: DEBUG/MainProcess] | Worker: Starting Hub
[2023-12-24 19:25:47,785: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:47,785: DEBUG/MainProcess] | Worker: Starting Pool
[2023-12-24 19:25:48,019: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:48,019: DEBUG/MainProcess] | Worker: Starting Consumer
[2023-12-24 19:25:48,019: DEBUG/MainProcess] | Consumer: Starting Connection
[2023-12-24 19:25:48,020: WARNING/MainProcess] /Users/user_name/.pyenv/versions/3.10.11/envs/app_name/lib/python3.10/site-packages/celery/worker/consumer/consumer.py:507: CPendingDeprecationWarning: The broker_connection_retry configuration setting will no longer determine
whether broker connection retries are made during startup in Celery 6.0 and above.
If you wish to retain the existing behavior for retrying connections on startup,
you should set broker_connection_retry_on_startup to True.
warnings.warn(

[2023-12-24 19:25:48,026: INFO/MainProcess] Connected to redis://localhost:6379//
[2023-12-24 19:25:48,026: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:48,026: DEBUG/MainProcess] | Consumer: Starting Events
[2023-12-24 19:25:48,026: WARNING/MainProcess] /Users/user_name/.pyenv/versions/3.10.11/envs/app_name/lib/python3.10/site-packages/celery/worker/consumer/consumer.py:507: CPendingDeprecationWarning: The broker_connection_retry configuration setting will no longer determine
whether broker connection retries are made during startup in Celery 6.0 and above.
If you wish to retain the existing behavior for retrying connections on startup,
you should set broker_connection_retry_on_startup to True.
warnings.warn(

[2023-12-24 19:25:48,028: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:48,028: DEBUG/MainProcess] | Consumer: Starting Mingle
[2023-12-24 19:25:48,028: INFO/MainProcess] mingle: searching for neighbors
[2023-12-24 19:25:49,037: INFO/MainProcess] mingle: all alone
[2023-12-24 19:25:49,038: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:49,038: DEBUG/MainProcess] | Consumer: Starting Gossip
[2023-12-24 19:25:49,048: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:49,049: DEBUG/MainProcess] | Consumer: Starting Tasks
[2023-12-24 19:25:49,053: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:49,053: DEBUG/MainProcess] | Consumer: Starting Control
[2023-12-24 19:25:49,058: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:49,059: DEBUG/MainProcess] | Consumer: Starting Heart
[2023-12-24 19:25:49,061: DEBUG/MainProcess] ^-- substep ok
[2023-12-24 19:25:49,061: DEBUG/MainProcess] | Consumer: Starting event loop
[2023-12-24 19:25:49,061: DEBUG/MainProcess] | Worker: Hub.register Pool...
[2023-12-24 19:25:49,062: INFO/MainProcess] celery@Neos-MacBook-Pro.local ready.
[2023-12-24 19:25:49,062: DEBUG/MainProcess] basic.qos: prefetch_count->40
[2023-12-24 19:25:53,429: ERROR/MainProcess] Received unregistered task of type 'users.tasks.get_users_count'.
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
https://docs.celeryq.dev/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)

The full contents of the message headers:
{'lang': 'py', 'task': 'users.tasks.get_users_count', 'id': '119c005b-f4f2-4823-991b-6645c8a5fa18', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '119c005b-f4f2-4823-991b-6645c8a5fa18', 'parent_id': None, 'argsrepr': '()', 'kwargsrepr': '{}', 'origin': 'gen16235@Neos-MacBook-Pro.local', 'ignore_result': False, 'replaced_task_nesting': 0, 'stamped_headers': None, 'stamps': {}}

The delivery info for this task is:
{'exchange': '', 'routing_key': 'celery'}
Traceback (most recent call last):
File "/Users/user_name/.pyenv/versions/3.10.11/envs/app_name/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 658, in on_task_received
strategy = strategies[type_]
KeyError: 'users.tasks.get_users_count'

celery Have tasks

. massage_queen.users.tasks.add
. massage_queen.users.tasks.get_users_count
. shop.tasks.add

But not working

Result

Tasks.py in the shop app works well, but I don't know why the function in tasks.py created by the cookie cutter doesn't work properly. Of course, it's okay if you don't use it, but I don't understand the operation from a developer's perspective.

@re4lfl0w re4lfl0w added the bug label Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant