【问题标题】:How to write an Ubuntu Upstart job for Celery (django-celery) in a virtualenv如何在 virtualenv 中为 Celery (django-celery) 编写 Ubuntu Upstart 作业
【发布时间】:2012-05-02 07:14:46
【问题描述】:

我真的很喜欢使用暴发户。我目前有新贵的工作来在许多 virtualenvs 中运行不同的 gunicorn 实例。但是,我在互联网上找到的 2-3 个 Celery upstart 脚本示例对我不起作用。

那么,使用以下变量,我将如何编写一个 Upstart 作业以在 virtualenv 中运行 django-celery。

Django 项目的路径:

/srv/projects/django_project

此项目的 virtualenv 的路径:

/srv/environments/django_project

celery 设置的路径是 Django 项目设置文件(django-celery):

/srv/projects/django_project/settings.py

此 Celery 实例的日志文件路径:

/srv/logs/celery.log

对于这个虚拟环境,用户:

iamtheuser

和小组:

www-data

我想用 celerybeat 运行 Celery Daemon,所以,我想传递给 django-admin.py(或 manage.py)的命令是:

python manage.py celeryd -B

如果脚本在 gunicorn 作业开始后启动,并在 gunicorn 作业停止时停止,那就更好了。假设文件是​​:

/etc/init/gunicorn.conf

【问题讨论】:

    标签: django ubuntu celery django-celery upstart


    【解决方案1】:

    您可能需要添加更多配置,但这是我为在 virtualenv 中以特定用户身份启动 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 二进制文件。

    【讨论】:

    【解决方案2】:

    这是我使用在 Ubuntu 16.04 LTS 上运行的较新 systemd 的工作配置。芹菜在一个虚拟环境中。应用程序是 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 文件夹,然后 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 调试:tail -f /var/log/syslog 并尝试重新启动 celery 以查看错误是什么。它可能与后端或其他事物有关。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2019-11-15
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 2018-02-12
      • 2014-07-18
      • 2013-08-04
      • 2014-12-06
      相关资源
      最近更新 更多