Django                
            Esecuzione di Celery con Supervisor
        
        
            
    Ricerca…
Configurazione del sedano
SEDANO
- Installazione - - pip install django-celery
- Inserisci 
- Struttura di progetto di base. - - src/ - bin/celery_worker_start # will be explained later on - logs/celery_worker.log - stack/__init __.py - stack/celery.py - stack/settings.py - stack/urls.py - manage.py
- Aggiungi il file - celery.pyal tuo- stack/stack/cartella.- from __future__ import absolute_import import os from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stack.settings') from django.conf import settings # noqa app = Celery('stack') app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
- al tuo - stack/stack/__init__.pyaggiungi il seguente codice:- from __future__ import absolute_import from .celery import app as celery_app # noqa
- Crea un'attività e contrassegnala per esempio come - @shared_task()- @shared_task() def add(x, y): print("x*y={}".format(x*y))
- Eseguendo il lavoratore del sedano "a mano": - celery -A stack worker -l infose vuoi aggiungere anche
Supervisore in esecuzione
- Crea uno script per avviare il gestore di sedano. Inserisci il tuo script nella tua app. Ad esempio: - stack/bin/celery_worker_start- #!/bin/bash NAME="StackOverflow Project - celery_worker_start" PROJECT_DIR=/home/stackoverflow/apps/proj/proj/ ENV_DIR=/home/stackoverflow/apps/proj/env/ echo "Starting $NAME as `whoami`" # Activate the virtual environment cd "${PROJECT_DIR}" if [ -d "${ENV_DIR}" ] then . "${ENV_DIR}bin/activate" fi celery -A stack --loglevel='INFO'
- Aggiungi i diritti di esecuzione allo script appena creato: - chmod u+x bin/celery_worker_start
- Installa supervisore (salta questo test se il supervisore è già installato) - apt-get install supervisor
- Aggiungi il file di configurazione per il tuo supervisore per iniziare il sedano. Collocarlo in - /etc/supervisor/conf.d/stack_supervisor.conf- [program:stack-celery-worker] command = /home/stackoverflow/apps/stack/src/bin/celery_worker_start user = polsha stdout_logfile = /home/stackoverflow/apps/stack/src/logs/celery_worker.log redirect_stderr = true environment = LANG = en_US.UTF-8,LC_ALL = en_US.UTF-8 numprocs = 1 autostart = true autorestart = true startsecs = 10 stopwaitsecs = 600 priority = 998
- Rileggere e aggiornare il supervisore - sudo supervisorctl reread stack-celery-worker: available sudo supervisorctl update stack-celery-worker: added process group
- Comandi di base - sudo supervisorctl status stack-celery-worker stack-celery-worker RUNNING pid 18020, uptime 0:00:50 sudo supervisorctl stop stack-celery-worker stack-celery-worker: stopped sudo supervisorctl start stack-celery-worker stack-celery-worker: started sudo supervisorctl restart stack-celery-worker stack-celery-worker: stopped stack-celery-worker: started
Celery + RabbitMQ con Supervisor
Celery richiede a un broker di gestire il passaggio dei messaggi. Usiamo RabbitMQ perché è facile da configurare ed è ben supportato.
Installa rabbitmq utilizzando il seguente comando
sudo apt-get install rabbitmq-server
Una volta completata l'installazione, creare un utente, aggiungere un host virtuale e impostare le autorizzazioni.
sudo rabbitmqctl add_user myuser mypassword
sudo rabbitmqctl add_vhost myvhost
sudo rabbitmqctl set_user_tags myuser mytag
sudo rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*"
Per avviare il server:
sudo rabbitmq-server
Possiamo installare sedano con pip:
pip install celery
Nel tuo file Django settings.py, l'URL del tuo broker sarebbe quindi simile
BROKER_URL = 'amqp://myuser:mypassword@localhost:5672/myvhost'
Ora inizia il sedano
celery -A your_app worker -l info
Questo comando avvia un operatore di Celery per eseguire tutte le attività definite nell'app django.
Supervisor è un programma Python che consente di controllare e mantenere in esecuzione qualsiasi processo unix. Può anche riavviare i processi bloccati. Lo usiamo per assicurarci che i lavoratori di Celery siano sempre in esecuzione.
Innanzitutto, installa supervisore
sudo apt-get install supervisor
Crea il tuo file_proj.conf nel tuo supervisore conf.d (/etc/supervisor/conf.d/your_proj.conf):
[program:your_proj_celery]
command=/home/your_user/your_proj/.venv/bin/celery --app=your_proj.celery:app worker -l info
directory=/home/your_user/your_proj
numprocs=1
stdout_logfile=/home/your_user/your_proj/logs/celery-worker.log
stderr_logfile=/home/your_user/your_proj/logs/low-worker.log
autostart=true
autorestart=true
startsecs=10
Una volta che il nostro file di configurazione è stato creato e salvato, possiamo informare il Supervisore del nostro nuovo programma attraverso il comando supervisorctl. Per prima cosa chiediamo a Supervisor di cercare eventuali configurazioni di programma nuove o modificate nella directory /etc/supervisor/conf.d con:
sudo supervisorctl reread
Seguito dicendogli di applicare qualsiasi modifica con:
sudo supervisorctl update
Una volta che i nostri programmi sono in esecuzione, ci sarà indubbiamente un momento in cui vogliamo fermarci, riavviarci o vedere il loro stato.
sudo supervisorctl status
Per riavviare l'istanza di sedano:
sudo supervisorctl restart your_proj_celery