芹菜:自动启动工人(启动时)

时间:2014-10-28 22:46:25

标签: python celery virtualenv message-queue

我有/var/tasks/tasks.py中定义的任务(针对Celery)。

我在/var/tasks/venv有一个virtualenv,应该用来运行/var/tasks/tasks.py

我可以手动启动一个工作人员来处理这样的任务:

cd /var/tasks
. venv/bin/activate
celery worker -A tasks -Q queue_1

现在,我想要守护这个。

我从GitHub复制了init.d脚本,并使用/etc/default/celeryd中的以下配置文件:

# name(s) of nodes to start
CELERYD_NODES="worker1"

# absolute or relative path to celery binary
CELERY_BIN="/var/tasks/venv/bin/celery"

# app instance
CELERY_APP="tasks"

# change to directory on upstart
CELERYD_CHDIR="/var/tasks"

# options
CELERYD_OPTS="-Q queue_1 --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"

# unprivileged user/group
CELERYD_USER="celery"
CELERYD_GROUP="celery"

# create pid and log directories, if missing
CELERY_CREATE_DIRS=1

当我启动服务时(通过init.d脚本),它会显示:

celery init v10.1.
Using config script: /etc/default/celeryd

但是,它不处理队列中的任何任务,日志文件中也没有任何内容。

我做错了什么?

3 个答案:

答案 0 :(得分:3)

Supervisor可能是一个不错的选择,但如果你想使用Celery Init.d脚本会建议你从他们的Github源复制它。

sudo vim /etc/init.d/celeryd

将代码从https://github.com/celery/celery/blob/master/extra/generic-init.d/celeryd复制到文件中。有关详细信息,请参阅daemonizing tutorial

sudo chmod 755 /etc/init.d/celeryd
sudo chown root:root /etc/init.d/celeryd
sudo nano /etc/default/celeryd

复制粘贴以下配置并相应更改

#Where your Celery is present
CELERY_BIN="/home/shivam/Desktop/deploy/bin/celery"

# App instance to use 
CELERY_APP="app.celery"

# Where to chdir at start
CELERYD_CHDIR="/home/shivam/Desktop/Project/demo/"

# 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%I.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="shivam" 
CELERYD_GROUP="shivam"

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

保存并退出

sudo /etc/init.d/celeryd start
sudo /etc/init.d/celeryd status

这将自动启动Celery on Boot

sudo update-rc.d celeryd defaults

答案 1 :(得分:1)

如果您使用systemd,则应启用芹菜服务。它会在启动时激活你的celery守护进程。

case;

答案 2 :(得分:0)

我最终在/etc/supervisor/conf.d/celery.conf上使用了Supervisor和一个脚本,类似于:

https://github.com/celery/celery/blob/3.1/extra/supervisord/celeryd.conf

除其他外,它可以很好地自动处理恶魔化。