A Flask Celery Redis based webservice to converting images into 100x100 thumbnails
Make sure you have docker (version 20+) and docker-compose (version 1.27+) already installed.
If docker is configured to run with a local user you can drop sudo from the commands below.
-
Clone the repository to your local:
git clone https://github.com/azh4r/flask-thumbnail-svc.git
-
Change to the directoy where you cloned the app:
cd flask-thumbnail-svc
-
Create the docker container locally, you must have docker and docker-componse installed already:
sudo docker-compose build
-
Run the container (sudo maybe dropped if your docker is configured to run with a local user):
sudo docker-compose up
To run with multiple celery workers:
sudo docker-compose up --scale worker=N
where N is the number of workers.The service is configured to run on:
http://0.0.0.0:5000or any other IP address from outside docker such as:
http://127.0.0.1:5000 -
One can use curl to send an image file in the payload of a POST request to REST endpoint : http://127.0.0.1:5000/convert
Assuming you are in the 'flask-thumbnail-svc' directory execute:
curl -X POST -H 'Content-Type: multipart/form-data' -F "file=@tests/data/painting_image1.jpg" http://127.0.0.1:5000/convert
or
curl -X POST -F "file=@tests/data/painting_image1.jpg" http://127.0.0.1:5000/convert
This will return the status of the task and task_id:
{"submission_task_id": "1952114c-ac35-4c5d-8203-5a946f3c71d8", "status": "PENDING"}
-
You can get the status of the submitted task by sending the id to the convert endpoint:
curl http://127.0.0.1:5000/convert/<task_id>
where task_id is from the previous post result e.g.:1952114c-ac35-4c5d-8203-5a946f3c71d8
This will give the current status of the task e.g.:
{"submission_task_id": "1952114c-ac35-4c5d-8203-5a946f3c71d8", "submission_status": "SUCCESS", "submission_result": True}
The App basically has 3 components:
- Flask-restful front end to send / load a local image to the server
- Redis which acts both as the message broker and the result backend.
- Celery to pick up the requests and execute them asynchrnously.
This enables long running processes to be executed asynchronously and multiple celery workers can be spawned to scale up in case of higher loads.
Flower is included in docker-compose and can be used for viewing the workers and status of tasks.
For Flower dashboard browse to: http://localhost:5556
Pytest module is used for unit and integration tests (Pending).
- Change directory to flask-thumbnail-svc:
cd flask-thumbnail-svc
- Execute pytest:
docker-compose exec flaskcelerypregen python -m pytest
Currently following tests have been implemented:
- A unit test for running thumbnail_task has been implemented.
- An integration test for uploading a file.
- A complete end to end test.