- Install a celery broker, in this case we are going to use RabbitMQ.
sudo apt-get install rabbitmq-server
- Install Celery as a pip package. Ensure virtual environment is activated using
source ENV_NAME/bin/activate
pip install celery
- Navigate to your project settings.py file at
/my_proj/my_proj/settings.py
and ensure that the following lines are set as such.
CELERY_BROKER_URL = 'amqp://guest:guest@localhost'
CELERY_RESULT_BACKEND = 'django-db' # Use 'django-cache' if you want to use your cache as your backend
You will probably want to create a new user and password and replace ‘guest:guest’ with the new users credentials.
- Start RabbitMQ service
sudo service rabbitmq-server start
- For development, celery can be run in a terminal window using
python manage.py celery start
However, for production, we need to set up celery as a service. We will do this using Supervisord.
- Install Supervisor as a pip package.
pip install supervisor
-
In the core arches repo, in
arches/install/supervisor_celery_setup
there exist example files for supervisor, celeryd, and celerybeat named "my_proj_name-supervisor.conf", "my_proj_name-celeryd.conf" and "my_proj_name-celerybeat.conf", respectively.
We are going to copy these files to resemble this structure: -
Enter a separate terminal window and enter
sudo su
then navigate to/etc
.
Runmkdir supervisor
and cd into this new directory. -
Copy the my_proj_name-supervisord.conf from core arches using
cp /arches/arches/install/supervisor_celery_setup/my_proj_name-supervisor.conf /etc/supervisor
- Make a new directory within /etc/supervisor
mkdir conf.d
- Copy the other two .conf files from core arches into this new directory
cp /arches/arches/install/supervisor_celery_setup/my_proj_name-celerybeat.conf /etc/supervisor/conf.d
cp /arches/arches/install/supervisor_celery_setup/my_proj_name-celeryd.conf /etc/supervisor/conf.d
- Rename the three files from my_proj_name to the name of your Arches project.
mv my_proj_name-supervisor.conf arches_project-supervisor.conf
-
In each of the files make sure that:
directory=/absolute/path/to/my_proj
is changed to the path of your Arches project e.g./home/ubuntu/arches_project
command=/path/to/virtualenv/
is changed to the path of your virtual environment e.g./home/ubuntu/env/
(usewhich python3
to find)
user=%(ENV_USER)s
is changed to the user going to run the supervisor e.g.user=ubuntu
[app]
is changed to the name of ELASTICSEARCH_PREFIX in your project’s settings.py
And in -supervisor.conf, make sure that:
files=./conf.d/my_proj_name-celeryd.conf
is changed to the name of your arches project. -
Currently the formatting of the -celeryd.conf is incorrect. Ensure these two lines of your file are formatted as such:
directory=/home/ubuntu/arches_project
command=/home/ubuntu/env/bin/celery -A arches_project worker --loglevel=INFO
-
Running supervisor now will cause errors as it cannot create the files due to current permissions. Before we can proceed, we need to change permissions of files/directories to the user entered to use supervisor.
As root user, navigate to/var/log
andmkdir supervisor
Cd to supervisor and create the log file usingtouch supervisord.log
and then change permissions to the user selected bychown USER supervisord.log
-
Still as root, navigate to
/var/log
andmkdir celery
Once again cd to celery and usetouch worker.log
andtouch beat.log
thenchown USER worker.log
andchown USER beat.log
-
Navigate to
/var/run/
and touchsupervisord.pid
Thenchown USER supervisord.pid
-
Exit as root user and return to the user you wish to run supervisor as.
-
Run the following to start the supervisord which will start celery workers for your tasks.
supervisord -c /etc/supervisor/ARCHES_PROJECT-supervisord.conf
- Run the following to stop your supervisord process.
unlink /tmp/supervisor.sock
- To see how many celery workers are running, use
ps aux | grep celery
- this should usually be 1 less than the no. CPU cores.