我真的很喜欢使用upstart。我目前有一些启动任务,可以在多个virtualenv中运行不同的gunicorn实例。然而,我在互联网上为芹菜新贵脚本找到的2-3个例子对我来说不起作用。
那么,使用以下变量,我将如何编写一个Upstart作业来在虚拟环境中运行django-celery。
Django项目的路径:
/srv/projects/django_project此项目的virtualenv的路径:
/srv/environments/django_projectcelery设置的路径是Django项目设置文件(django-celery):
/srv/projects/django_project/settings.py此Celery实例的日志文件的路径:
/srv/logs/celery.log对于这个虚拟环境,用户:
iamtheuser和这个组:
www-data我想用celerybeat运行芹菜守护进程,所以,我想传递给django-admin.py (或manage.py)的命令是:
python manage.py celeryd -B如果脚本在gunicorn作业开始后启动,并在gunicorn作业停止时停止,那就更好了。假设它的文件是:
/etc/init/gunicorn.conf发布于 2012-04-21 01:51:10
您可能需要添加更多配置,但这是我编写的一个初创脚本,用于在虚拟环境中以特定用户身份启动django-celery:
start on started mysql
stop on stopping mysql
exec su -s /bin/sh -c 'exec "$0" "$@"' user -- /home/user/project/venv/bin/python /home/user/project/django_project/manage.py celeryd
respawn它对我来说很有用。
我知道它看起来很丑陋,但它似乎是当前基于this superuser answer以非特权用户身份运行暴发户作业的“合适”技术。
我认为我需要做更多的工作才能让它在virtualenv中工作,但是在virtualenv中调用python二进制文件就足够了。
发布于 2018-03-05 16:46:00
这是我使用在Ubuntu 16.04 LTS上运行的较新的systemd的工作配置。芹菜在虚拟环境中。App是一个Python/Flask。
系统文件:/etc/systemd/system/celery.service
您需要更改用户和路径。
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=nick
Group=nick
EnvironmentFile=-/home/nick/myapp/server_configs/celery_env.conf
WorkingDirectory=/home/nick/myapp
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target 环境文件(如上引用):/home/nick/myapp/server_configs/celery_env.conf
# 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 or relative path to the 'celery' command:
CELERY_BIN="/home/nick/myapp/venv/bin/celery"
# App instance to use
CELERY_APP="myapp.tasks"
# How to call manage.py
CELERYD_MULTI="multi"
# 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.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"要使用您的用户的正确权限自动创建日志和运行文件夹,请在/usr/lib/tmpfiles.d中创建一个文件。我在重启时删除/var/run/celery文件夹时遇到了问题,然后芹菜无法正常启动。
我的/usr/lib/tmpfiles.d/celery.conf文件:
d /var/log/celery 2775 nick nick -
d /var/run/celery 2775 nick nick -要启用,请执行以下操作:sudo systemctl enable celery.service
现在,您需要重新启动系统,以便创建/var/log/celery和/var/run/celery文件夹。您可以通过查看/var/log/celery中的日志来查看重启后是否启动了celery。
要重新启动celery:sudo systemctl restart celery.service Debugging:tail -f /var/log/syslog并尝试重新启动celery,查看错误是什么。这可能与后端或其他事情有关。
希望这能有所帮助!
https://stackoverflow.com/questions/10250682
复制相似问题