简体   繁体   English

芹菜“收到未注册类型的任务”

[英]Celery “Received unregistered task of type”

I have read quite a few posts that are similar to this but none seem to make sense to me. 我已经阅读了很多与此类似的文章,但对我来说似乎没有意义。

I am trying to configure a Celery PeriodicTask to fire every 5 seconds but I'm getting hung up on a Celery configuration issue (I think) 我正在尝试将Celery PeriodicTask配置为每5秒触发一次,但我正挂在Celery配置问题上(我认为)

comm/tasks.py comm / tasks.py

import datetime
from celery.decorators import periodic_task

@periodic_task
def send_queued_messages():
    # do something...

myapp/settings.py myapp / settings.py

...
from comm.tasks import send_queued_messages
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
    'send_queued_messages_every_5_seconds': {
        'task': 'comm.tasks.send_queued_messages',   # Is the issue here?  I've tried a dozen variations!!
        'schedule': timedelta(seconds=5),
        },
    }

The relevant output from my error logs: 我的错误日志的相关输出:

23:41:00 worker.1 | [2015-06-10 03:41:00,657: ERROR/MainProcess] Received unregistered task of type 'send_queued_messages'.
23:41:00 worker.1 | The message has been ignored and discarded.
23:41:00 worker.1 | 
23:41:00 worker.1 | Did you remember to import the module containing this task?
23:41:00 worker.1 | Or maybe you are using relative imports?
23:41:00 worker.1 | Please see http://bit.ly/gLye1c for more information.
23:41:00 worker.1 | 
23:41:00 worker.1 | The full contents of the message body was:
23:41:00 worker.1 | {'utc': True, 'chord': None, 'args': [], 'retries': 0, 'expires': None, 'task': 'send_queued_messages', 'callbacks': None, 'errbacks': None, 'timelimit': (None, None), 'taskset': None, 'kwargs': {}, 'eta': None, 'id': 'a8ca18...227a56'} (216b)

I ran into this exact problem, and it turns out the issue is not with the name of the task, but that the Celery worker isn't aware of your task module. 我遇到了一个确切的问题,事实证明问题不在于任务的名称 ,而是Celery工作人员不知道您的任务模块。

In other words, you have the name of the task correct ( 'comm.tasks.send_queued_messages' ), which was generated by the task decorator, you just haven't told Celery where to look for it. 换句话说,您具有由任务装饰器生成的正确任务的名称( 'comm.tasks.send_queued_messages' ),只是您没有告诉Celery在哪里寻找它。

The quickest solution is to add the following to myapp/settings.py : 最快的解决方案是将以下内容添加到myapp/settings.py

CELERY_IMPORTS = ['comm.tasks']

According to the docs , this determines the "sequence of modules to import when the worker starts." 根据文档 ,这确定了“工作者启动时要导入的模块的顺序”。

Alternatively, you can configure your settings to auto discover tasks ( see docs here ), but then you would have to namespace your task module(s), moving comm/tasks.py to comm/comm/tasks.py . 另外,您可以配置设置以自动发现任务( 请参阅此处的文档 ),但是随后您必须为任务模块命名空间,将comm/tasks.py移至comm/comm/tasks.py

For me, the confusion came from Celery's automatic naming convention, which looks like an import statement, which led me to believe I was using CELERYBEAT_SCHEDULE['task'] to tell Celery where to look for the task. 对我来说,混乱来自Celery的自动命名约定,它看起来像是导入语句,这使我相信我正在使用CELERYBEAT_SCHEDULE['task']告诉Celery 在哪里寻找任务。 Instead, the scheduler just takes the name as a string. 而是,调度程序仅将名称作为字符串。

If you are using below code in proj/proj/celery.py following celery document , 如果您在celery文档之后使用proj/proj/celery.py以下代码,

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

and it looks pefect with tutorial, 看起来很完美,

Don't forget add your new app to INSTALLED_APPS in settings.py 不要忘记在settings.py中将新应用添加到INSTALLED_APPS

See the celery docs for an explanation on task naming. 有关任务命名的说明,请参见celery文档

In this case you need to provide celerybeat with a task name that it can find. 在这种情况下,您需要为celerybeat提供可以找到的任务名称。

try this: 尝试这个:

CELERYBEAT_SCHEDULE = {
    'send_queued_messages_every_5_seconds': {
        'task': 'myapp.tasks.send_queued_messages', 
        'schedule': timedelta(seconds=5),
        },
    }

I just run into the same problem. 我只是遇到了同样的问题。 What I did wrong was that I didn't terminate one of my celery task properly (like the one named ' send_queued_messages ' in your case), leaving it run in background and keep sending task named 'send_queued_messages'. 我没有什么错是,我并没有终止我的芹菜任务的一个正确(如在你的情况下,一个名为“send_queued_messages”),把它留在后台运行,并不断发出名为“send_queued_messages”任务。 Which seems like not a big problem, as long as you still have that task in your code. 只要您的代码中仍然包含该任务,这似乎就不是什么大问题。

But in my case I modified my task name after that to something different (like ' comm.tasks.send_queued_messages '). 但就我而言,我在此之后将任务名称修改为其他名称(例如' comm.tasks.send_queued_messages ')。 Which made the task named ' send_queued_messages ' become a ' unregistered task ', I think. 我认为,这使名为“ send_queued_messages ”的任务成为“ 未注册任务 ”。

Below is what I did to solve: 以下是我要解决的问题:

  1. kill all celery process: (grep 'celery' instead of 'celery worker' as indicated by celery documents) 杀死所有芹菜过程:(用grep'celery'代替celery文档指示的'celery worker')

ps auxww | ps auxww | grep 'celery' | grep“芹菜” | awk '{print $2}' | awk'{print $ 2}'| xargs kill -9 xargs杀死-9

  1. restart rabbitmq: (or whatever broker you use) 重新启动rabbitmq :(或您使用的任何代理)

sudo -u rabbitmq rabbitmqctl stop sudo -u rabbitmq rabbitmqctl停止
sudo rabbitmq-server 须藤rabbitmq服务器

Then the error was gone. 然后错误消失了。

Celery-beat was throwing me the same issue and nothing what was wrote above didn't help me. Celery-beat引发了我同样的问题,上面写的内容对我没有帮助。

I tried to insert this snippet into my settings file: 我试图将此代码段插入到我的设置文件中:

CELERY_IMPORTS = ['myapp.tasks']

although I didn't need it. 尽管我不需要它。 But it helped me because new error log was appeared. 但这对我有所帮助,因为出现了新的错误日志。 In my tasks file I had an import of non-existing apps. 在我的任务文件中,我导入了不存在的应用程序。

This can be caused by very confusion reasons... A lot of time there is a syntax error or something like this. 这可能是由于非常混乱的原因引起的。很多时候出现语法错误或类似的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM