【问题标题】:Celery Running as Daemon stops芹菜作为守护进程运行停止
【发布时间】:2016-05-06 17:25:31
【问题描述】:

我在 centos linux 服务器上使用 redis 安装了 celery。

我使用以下命令启动 celery worker:

celery multi start worker1 -A proj -Q lopri,lopri2 -l info --pidfile="$HOME/run/celery/%n.pid" --logfile="$HOME/log/celery/%n.log"

问题是几个小时后,worker 不再响应任务创建。我必须重新启动工作人员才能再次处理任务。

这是位于 /etc/default/celeryd 的 celery 设置文件:

# Names of nodes to start
#   most people will only start one node:
CELERYD_NODES="worker1"
#   but you can also start multiple and configure settings
#   for each in CELERYD_OPTS (see `celery multi --help` for examples):
#CELERYD_NODES="worker1 worker2 worker3"
#   alternatively, you can specify the number of nodes to start:
#CELERYD_NODES=10

# Absolute or relative path to the 'celery' command:
CELERY_BIN="/usr/local/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"

# App instance to use
# comment out this line if you don't use an app
CELERY_APP="proj"
# or fully qualified:
#CELERY_APP="proj.tasks:app"

#django settings
#export DJANGO_SETTINGS_MODULE="settings"

# Where to chdir at start.
CELERYD_CHDIR="/var/www/html/proj/"

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

# %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.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists, e.g. nobody).
CELERYD_USER="ec2-user"
CELERYD_GROUP="ec2-user"

# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1

一个问题可能是关于正在发生的事情的线索是我必须从项目文件夹中运行“开始”命令,否则我会收到错误:

ImportError: 没有名为 proj 的模块

CELERYD_CHDIR 设置不应该解决这个问题吗?这是否意味着我的启动命令没有使用这个 celery 默认设置文件?

感谢您对此提供的任何启发。

【问题讨论】:

    标签: python linux django celery daemon


    【解决方案1】:

    如果您通过命令行运行 celery,那么您的设置文件几乎毫无用处。我的意思是,它们没有被应用,这就是为什么你会收到一个错误,说你的项目不能被导入。

    这是将 celery 作为守护进程运行的脚本: https://github.com/celery/celery/blob/3.1/extra/generic-init.d/celeryd

    如您所见:https://github.com/celery/celery/blob/3.1/extra/generic-init.d/celeryd#L56 这是脚本导入您的设置的地方。

    我不太确定为什么你的 celeryd 会在几个小时后停止工作,但这表明你并没有真正作为守护进程运行。 也许用设置设置属性 init.d 脚本可以解决这个问题,但我对此表示怀疑。

    【讨论】:

    • 那我应该如何运行 celery?如果我运行 service celeryd start 那么它不会连接到我在 django 项目中设置的队列。
    • 如果您正确设置了所有内容(将脚本添加到 init.d、创建自定义服务等),您的 celery 应该会在启动后自动运行。每个操作系统都不同,但您应该很容易找到如何做到这一点。我看到你的项目在 /var/www/html 里面,你的 celery 设置文件有一个自定义用户。请记住,该用户需要访问该文件夹,默认情况下该路径归根用户所有,因此即使您启动并运行脚本,也可能会遇到一些权限问题。
    最近更新 更多