Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在virtualenv中为芹菜(django芹菜)编写Ubuntu Upstart作业_Django_Ubuntu_Celery_Django Celery_Upstart - Fatal编程技术网

如何在virtualenv中为芹菜(django芹菜)编写Ubuntu Upstart作业

如何在virtualenv中为芹菜(django芹菜)编写Ubuntu Upstart作业,django,ubuntu,celery,django-celery,upstart,Django,Ubuntu,Celery,Django Celery,Upstart,我真的很喜欢使用upstart。我目前有一个新的任务,可以在许多VirtualNV中运行不同的gunicorn实例。然而,我在互联网站上找到的芹菜新贵脚本的2-3个例子对我来说并不适用 因此,有了以下变量,我将如何编写一个Upstart作业来在virtualenv中运行django芹菜 Django项目路径: /srv/projects/django_project 此项目的virtualenv的路径: /srv/environments/django_project start on st

我真的很喜欢使用upstart。我目前有一个新的任务,可以在许多VirtualNV中运行不同的gunicorn实例。然而,我在互联网站上找到的芹菜新贵脚本的2-3个例子对我来说并不适用

因此,有了以下变量,我将如何编写一个Upstart作业来在virtualenv中运行django芹菜

Django项目路径:

/srv/projects/django_project
此项目的virtualenv的路径:

/srv/environments/django_project
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
芹菜设置的路径是Django项目设置文件(Django芹菜):

此芹菜实例的日志文件路径:

/srv/logs/celery.log
对于此虚拟环境,用户:

iamtheuser
该小组:

www-data
我想用celerybeat运行芹菜守护程序,因此,我想传递给django-admin.py(或manage.py)的命令是:

如果脚本在gunicorn作业开始后启动,并在gunicorn作业停止时停止,则效果会更好。假设该文件为:

/etc/init/gunicorn.conf

您可能需要添加更多配置,但这是我为在virtualenv中作为特定用户启动django芹菜而编写的一个upstart脚本:

/srv/environments/django_project
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
这对我很有用

我知道这看起来很难看,但它似乎是当前作为非特权用户运行暴发户作业的“正确”技术,基于


我原以为我必须做更多的工作才能让它在virtualenv中工作,但在virtualenv中调用python二进制文件就是一切。

这是我使用运行在Ubuntu 16.04 LTS上的较新systemd的工作配置。芹菜的味道很好。应用程序是一个Python/Flask

Systemd文件:
/etc/Systemd/system/芹菜。服务

您需要更改用户和路径

[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\u configs/celery\u 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/芹菜
文件夹,芹菜无法正常启动

我的
/usr/lib/tmpfiles.d/cellery.conf
文件:

d /var/log/celery 2775 nick nick -
d /var/run/celery 2775 nick nick -
要启用:
sudo系统ctl启用芹菜。服务

现在,您需要重新启动系统以创建
/var/log/芹菜
/var/run/芹菜
文件夹。您可以通过检查
/var/log/celery
中的日志来检查芹菜是否在重新启动后启动

要重新启动芹菜:
sudo系统ctl重新启动芹菜。服务
调试:
tail-f/var/log/syslog
并尝试重新启动芹菜以查看错误。它可能与后端或其他事物有关


希望这有帮助

可能很高兴添加您的调整。。。我把我的贴在这里: