Django +芹菜 - 为什么我的周期性任务没有运行?

时间:2016-06-23 14:35:59

标签: python django django-celery

我正在使用django + celery进行定期任务。我的项目有django-celery == 3.1.17,使用的代理是Redis,它正在工作。

在我的设置文件中:

[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.de:MyApplication:jar:1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.de:MyApplication:${projectVersion}, ...MyApplication directory.../pom.xml, line ..., column ...
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

有更多的芹菜设置,但我想这两个与这个问题最相关。

任务:

CELERYBEAT_SCHEDULE={
    'delivery_send': {
        'task': 'delivery.tasks.DeliverySendTask',
        'schedule': timedelta(minutes=1),
        'args': [],
    },
}

CELERY_ROUTES = {
    'delivery.tasks.DeliverySendTask': {
        'queue': 'periodic_tasks',
        'routing_key': 'periodic_tasks'
    }, 
}

然后我运行class DeliverySendTask(Task): def run(self, *args, **kwargs): logger.info('executing task!') from .models import Dispatch Dispatch.objects.all().delete() 并在控制台中查看

python manage.py celery beat

但删除实际上从未执行过。我错过了什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

芹菜殴打不执行任务。仅将任务计划到队列中。 celery worker执行任务。

要定期执行任务,您必须同时启动celery beatcelery worker

python manage.py celery beat
python manage.py celery worker

答案 1 :(得分:1)

也要安装芹菜。然后不要用manage.py运行它。像这样运行:

celery -A your_app_name worker -l info -B -E

所以没有python manage.py