【问题标题】:Cannot setup Celery as daemon on server无法在服务器上将 Celery 设置为守护进程
【发布时间】:2016-05-05 01:54:07
【问题描述】:

我无法在服务器上将 Celery 设置为守护进程(django 1.6.11、celery 3.1、Ubuntu 14.04) 尝试了很多选项,任何人都可以设置完整的工作配置来运行 celery 作为守护进程吗?

我对官方文档http://docs.celeryproject.org/en/latest/tutorials/daemonizing.html#generic-init-scripts 感到非常失望——这些都不起作用,没有完整的分步教程。 youtube 上关于如何设置守护进程的零 (!!!) 视频。

现在我可以通过 celery worker -A engine -l info -E 简单地运行 celery 来自 django 的任务已成功执行。

我已经完成了配置:

/etc/defaults/celery

    # Name of nodes to start
# here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"

# Absolute path to "manage.py"
CELERY_BIN="/var/www/engine/manage.py"

# How to call manage.py
CELERYD_MULTI="celery multi"

# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=2"

# %N will be replaced with the first part of 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="root"
CELERYD_GROUP="root"

/etc/init.d/celeryd
来自https://github.com/celery/celery/blob/3.1/extra/generic-init.d/celeryd,没有变化

现在,当我转到控制台并运行时: cd /etc/init.d celery multi start w1

我看到了输出:

celery multi v3.1.11 (Cipater)
> Starting nodes...
        > w1@engine: OK

所以,没有错误!没有调用任务,我不知道出了什么问题。

【问题讨论】:

    标签: python django celery daemon


    【解决方案1】:

    我建议使用Supervisor。它比初始化脚本更好,因为您可以在一台服务器上为不同的项目运行多个 Celery 实例。您可以在 Celery repo 中找到 Supervisor 的示例配置或我的项目中的完整工作示例:

    # /etc/supervisor/conf.d/celery.conf
    [program:celery]
    command=/home/newspos/.virtualenvs/newspos/bin/celery worker -A newspos --loglevel=INFO
    user=newspos
    environment=DJANGO_SETTINGS_MODULE="newspos.settings"
    directory=/home/newspos/projects/newspos/
    autostart=true
    autorestart=true
    stopwaitsecs = 600
    killasgroup=true
    startsecs=10
    stdout_logfile=/var/log/celery/newspos-celeryd.log
    stderr_logfile=/var/log/celery/newspos-celeryd.log
    

    【讨论】:

    • 不幸的是:Supervisor 需要 Python 2.4 或更高版本,但不适用于任何版本的 Python 3。您使用的是 3.4.3 版本 - 何时安装 pip
    • @Vic 使用包管理器在系统范围内安装 supervisord。您可以在 Ubuntu 中使用 sudo apt-get install supervisor 进行操作。这样,主管将使用系统的 Python 2.7。
    • 谢谢,这行得通,但下一步是什么?我将文件 celery.conf 放入 /etc/supervisor/conf.d,更改了 command=/var/www/engine/celery worker -A engine --loglevel=INFO 然后 user=root 和 environment=DJANGO_SETTINGS_MODULE="engine.settings"和 directory=/var/www/engine/ 现在如何启动守护进程?我应该删除 /etc/init.d/celeryd 脚本吗?
    • @Vic 是的,您可以删除您创建的初始化脚本。现在重新加载主管,这样它就会读取并应用您的新配置:sudo supervisorctl reload。将来您可以使用sudo supervisorctl restart celery 重新启动 celery 实例,但如果您再次更改 celery.conf 重新加载主管。
    • 如预期不工作),什么时候做:sudo supervisorctl reload 显示:重新启动 supervisord。然后我测试任务 - 未调用。 'sudo supervisorctl restart celery' 什么时候显示' celery: ERROR (not running) celery: ERROR (no such file) ' 我不确定:它真的可以在 python 2.7 下工作并处理 django 和其他模块,全部安装到蟒蛇 3.4 ?
    猜你喜欢
    • 1970-01-01
    • 2011-02-15
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多