5

据我所知,有两份文件描述了如何设置 celery。有“将工作人员作为守护进程运行”和“使用 Django 的第一步”。

在 Django 文档中,它说:

我们还将 Django 设置模块添加为 Celery 的配置源。这意味着您不必使用多个配置文件,而是直接从 Django 设置中配置 Celery。

听起来很棒。但是,据我所知,这些是完整的 Celery 守护进程所需的文件:

  • /etc/init.d/celeryd
  • /etc/defaults/celery
  • /my-proj/celery.py
  • /my-proj/__init__.py

并且可能:

  • /my-proj/settings.py

男孩,这是很多文件。我想我已经正确设置了它们:

  • /etc/init.d/celeryd具有celery 提供的默认 init.d 脚本。
  • /etc/defaults/celery几乎什么都没有。只是指向我的应用程序的指针:

    export DJANGO_SETTINGS_MODULE='cl.settings'
    
  • /my-proj/celery.py具有Django 的第一步中的推荐文件:

    from __future__ import absolute_import
    
    import os
    
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
    
    from django.conf import settings  # noqa
    
    app = Celery('proj')
    
    # Using a string here means the worker will not have to
    # pickle the object when using Windows.
    app.config_from_object('django.conf:settings')
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
  • /my-proj/__init__.py具有Django 的第一步中的推荐代码:

    from __future__ import absolute_import
    
    # This will make sure the app is always imported when
    # Django starts so that shared_task will use this app.
    from .celery import app as celery_app
    

我的settings.py文件中有所有与芹菜相关的设置,如下所示:

CELERY_BIN = '/var/www/.virtualenvs/my-env/bin/celery'
CELERYD_USER = 'www-data'
CELERYD_GROUP = 'www-data'
CELERYD_CONCURRENCY = 20
BROKER_URL = 'redis://'
BROKER_POOL_LIMIT = 30

然而,当我开始使用 celery 时sudo service celeryd start,它不起作用。相反,很明显它没有从我的 Django 项目中获取我的设置,因为它说:

Nov 05 20:51:59 pounamu celeryd[30190]: celery init v10.1.
Nov 05 20:51:59 pounamu celeryd[30190]: Using config script: /etc/default/celeryd
Nov 05 20:51:59 pounamu celeryd[30190]: No passwd entry for user 'celery'
Nov 05 20:51:59 pounamu su[30206]: No passwd entry for user 'celery'
Nov 05 20:51:59 pounamu su[30206]: FAILED su for celery by root
Nov 05 20:51:59 pounamu su[30206]: - ??? root:celery
Nov 05 20:51:59 pounamu systemd[1]: celeryd.service: control process exited, code=exited status=1

有什么想法可以让吊索不起作用?我错过了什么重要的东西吗?

4

2 回答 2

0

You are attempting to run celery as the system user "celery" which is the default used by the init script. You should create this user or you can override this by setting CELERYD_USER in /etc/defaults/celery.

Personally I prefer to use supervisord to manage celery.

于 2015-11-06T09:26:28.333 回答
-1

您缺少CELERY_APP设置。将其设置在配置文件 /etc/defaults/celery中或作为 worker 命令的参数:

celery worker -A my-proj

否则,Celery 不知道它应该看看/my-proj/celery.py。django 环境变量不会影响 Celery 加载的内容。

于 2015-11-06T08:16:07.563 回答