2012-04-20 43 views
15

我非常喜歡使用新貴。我現在有一些暴發戶在一些虛擬世界中運行不同的gunicorn實例。但是,我在互聯網上爲Celery新貴腳本找到的2-3個例子對我來說並不適用。如何在virtualenv上編寫Celery(django-celery)的Ubuntu Upstart作業

所以,用下面的變量,我將如何編寫一個Upstart作業來在virtualenv中運行django-celery。

路徑Django項目:

/srv/projects/django_project 

路徑這個項目的的virtualenv:

/srv/environments/django_project 

路徑芹菜設置是Django的項目設置文件(Django的芹菜):

/srv/projects/django_project/settings.py 

此Celery實例的日誌文件的路徑:

/srv/logs/celery.log 

對於該虛擬的env,用戶:

iamtheuser 

和基團:

www-data 

我想與celerybeat運行芹菜守護程序,所以,我想通過命令到django-admin.py(或manage.py)是:

python manage.py celeryd -B 

這將是e如果腳本在gunicorn工作開始後啓動,則更好,並在gunicorn工作停止時停止。比方說,文件是:

/etc/init/gunicorn.conf 

回答

17

您可能需要增加一些配置,但是這是一個暴發戶劇本我寫了開始Django的芹菜在virtualenv中一個特定的用戶:

start on started mysql 
stop on stopping mysql 

exec su -s /bin/sh -c 'exec "$0" "[email protected]"' user -- /home/user/project/venv/bin/python /home/user/project/django_project/manage.py celeryd 

respawn 

它對我很好。

我知道它看起來很醜陋,但它似乎是當前'非常適用'的技術,用於根據this superuser answer作爲非特權用戶運行新興作業。

我以爲我不得不做更多的工作來讓它在virtualenv裏面工作,但是在virtualenv裏面調用python二進制文件是它所需要的。

+0

太棒了,我調整了這個,它正在工作。 – pwalsh 2012-04-21 08:39:28

+0

可能是很好的添加你的調整...我張貼在這裏:http://stackoverflow.com/questions/14275821/how-to-run-celery-as-a-deamon-in-production/16470913#16470913 – 2013-05-09 20:54:07

0

這是我使用在Ubuntu 16.04 LTS上運行的較新systemd的工作配置。芹菜是一個虛擬世界。應用程序是一個Python /燒瓶。

Systemd文件:/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中的日誌來檢查重新啓動後是否啓動了芹菜。

要重新啓動芹菜:sudo systemctl restart celery.service 調試:tail -f /var/log/syslog並嘗試重新啓動芹菜以查看錯誤是什麼。它可能與後端或其他事物有關。

希望這會有所幫助!