To run the project, first install RabbitMQ and celery.
- Install celery using pip:
$ pip install celery
- Install RabbitMQ using apt:
$ sudo apt-get install rabbitmq-server
- Add
sbin
in path in.bashrc
:
PATH=$PATH:/usr/local/sbin
- Check if RabbitMQ is installed by:
$ rabbitmq-server
It should print:
Starting broker... completed with 10 plugins.
If it says 0 plugins, troubleshoot to install RabbitMQ properly. - Configure RabbitMQ for celery:
(if your username is jimmy)
# add user 'jimmy' with password 'jimmy123'
$ rabbitmqctl add_user jimmy jimmy123
# add virtual host 'jimmy_vhost'
$ rabbitmqctl add_vhost jimmy_vhost
# add user tag 'jimmy_tag' for user 'jimmy'
$ rabbitmqctl set_user_tags jimmy jimmy_tag
# set permission for user 'jimmy' on virtual host 'jimmy_vhost'
$ rabbitmqctl set_permissions -p jimmy_vhost jimmy ".*" ".*" ".*"
- Update the file celery_test/celeryapp.py according to your configuration as set in the previous step.
After successfully installing RabbitMQ and celery, run the project on 2 or more terminals as follows:
- Create one or more servers for a task in multiple terminals in the parent directory by:
$ celery worker --app=celery_test.celeryapp:app --loglevel=info -Q add
In another terminal:
- Run the flask server in the parent directory by:
$ python initial.py
- Open the flask server at
localhost:5000
. - Create the file which you want to execute as a ".txt" file. The file must contain 2 space separated numbers in multiple lines.
- Select the task and upload the file.
The basic flow of the code is as follows:
- The client interacts with a webpage for uploading tasks, which is connected to a Flask backend. This Flask backend can host any number of clients and can be connected to multiple servers which run our tasks (called workers) at a time.
The client uploads a file with a task.
-
Once a task is received by Flask, it automatically forwards it to a celery worker which runs remotely. These workers can be made to run for multiple tasks or for a single task.
-
The celery worker executes the task and outputs the result in the output file, which is automatically downloaded on the client. Flask plays a large role in connecting the client and the worker which executes the task. Flask acts as the middleware in this case.
Please feel free to reach out in case of any questions.
- Video 1 (Client side usage)
- Video 2 (Load Balancing)
- Video 3 (Worker: Failure and Recovery)
- Video 4 (Worker: Works even when the other worker fails in Video 3):
The small pause in the video was when the server tried to retry connection with the other server which failed and reallocate the tasks which were currently started by the other server but was not able to complete. This attribute shows the fault tolerant approach of the project.
Link: https://drive.google.com/drive/folders/14wI51kcO9MGOCTzZuUb0bHy-L9cotulz
https://avilpage.com/2014/11/scaling-celery-sending-tasks-to-remote.html
http://docs.celeryproject.org/
https://www.rabbitmq.com/documentation.html
http://flask.pocoo.org/docs/1.0/