celeryd和celerybeat - “收到类型的未注册任务......”

时间:2013-06-17 08:28:07

标签: python celery daemon celerybeat

好的伙计们,这让我疯了。我现在一直在努力工作一整天,而且无法让它发挥作用! 我的芹菜项目结构如下:

# celery.py

from celery.schedules import crontab
from celery import Celery

celery = Celery('scheduler.celery',
                include=['scheduler.tasks'])

celery.config_from_object('celeryconfig')

# tasks.py

from scheduler.celery import celery

@celery.task
def test():
    do_something()

# celeryconfig.py

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'test-cron': {
        'task': 'tasks.test',
         'schedule': crontab(minute='*/1'),
    },
}
# CELERY_IMPORTS = ('tasks', )
BROKER_URL = 'redis://localhost:6379/0'

所有文件都在projects/scheduler/文件夹下 当我启动celeryd服务时,我可以看到它正在运行并连接到我的代理,但是当我启动celerybeat服务时,我可以在日志中看到消息:Received unregistered task of type 'tasks.test'
如果我取消注释CELERY_IMPORTS常量(如SO中的许多答案所示),celeryd服务甚至都不会启动!实际上,它输出OK但是使用ps ef | grep celery我可以看到它没有运行。

我的守护程序配置文件如下所示:

# Name of nodes to start
CELERYD_NODES="w1"

# Where to chdir at start.
CELERYD_CHDIR="/home/me/projects/scheduler/"

# Extra arguments to celeryd
CELERYD_OPTS="--time-limit=300 --concurrency=4"

# %n will be replaced with the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"

# Workers should run as an unprivileged user.
CELERYD_USER="celery"
CELERYD_GROUP="celery"

# Extra arguments to celerybeat
CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

如果您没有scheduler模块并从模块根目录运行芹菜:

<强> runner.py

from celery_test import celery

if __name__ == '__main__':
    celery.start()

celery.py 重命名为 celery_test.py

from celery import Celery

celery = Celery('scheduler.celery',
                include=['tasks'])

celery.config_from_object('celeryconfig')

<强> celeryconfig.py

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'test-cron': {
        'task': 'tasks.test',
         'schedule': crontab(minute='*/1'),
    },
}

BROKER_URL = 'redis://localhost:6379/0'

tasks.py ,修复导入:

from celery_test import celery

@celery.task
def test():
    do_something()

您必须小心导入并添加runner.py,因为它会导入celery_test,然后导入tasks,然后再导入celery_test


如果您有scheduler模块并从模块根目录运行芹菜:

__init__.py 为空。

<强> runner.py

from scheduler.celery_test import celery

if __name__ == '__main__':
    celery.start()

celery.py 重命名为 celery_test.py

from celery import Celery

celery = Celery('scheduler.celery',
                include=['scheduler.tasks'])

celery.config_from_object('celeryconfig')      

celeryconfig.py ,修复了默认任务名称:

from celery.schedules import crontab

CELERYBEAT_SCHEDULE = {
    'test-cron': {
        'task': 'scheduler.tasks.test',
         'schedule': crontab(minute='*/1'),
    },
}

BROKER_URL = 'redis://localhost:6379/0'

<强> tasks.py

from scheduler.celery_test import celery

@celery.task
def test():
    do_something()