Skip to content

Commit 71b3bca

Browse files
committed
add celery to docker-compose
1 parent f6967ff commit 71b3bca

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ A Django RESTful API for managing personal or team tasks — featuring PostgreSQ
99

1010
- ✅ Django 5 + Django REST Framework
1111
- ✅ PostgreSQL database (via Docker)
12-
- ✅ RabbitMQ for background tasks (Celery-ready)
12+
- ✅ RabbitMQ for background tasks (Celery integrated)
13+
- ✅ Asynchronous task logging using Celery
1314
- ✅ Environment config with `.env` and `python-decouple`
1415
- ✅ Swagger UI for API documentation
1516
- ✅ Containerized with Docker
@@ -21,12 +22,13 @@ A Django RESTful API for managing personal or team tasks — featuring PostgreSQ
2122

2223
```
2324
taskflow-api/
24-
├── taskflow_api/ # Django project
25-
├── tasks/ # App: task models, views, serializers
25+
├── taskflow_api/ # Django project (includes celery.py)
26+
├── tasks/ # App: task models, views, serializers, signals, celery tasks
2627
├── requirements.txt # Python dependencies
2728
├── Dockerfile # Production image for gunicorn
2829
├── docker-compose.yml # DB and RabbitMQ container setup
2930
├── .env # Environment configuration
31+
├── logs/ # Directory for activity logs (auto-created)
3032
├── run_server.sh # Run production server (Gunicorn)
3133
└── start-dev-services.sh # Run DB + RabbitMQ for development
3234
```
@@ -79,20 +81,38 @@ Use this when you want to run Django locally (`runserver`) and containers only f
7981

8082
> This will:
8183
> - Start PostgreSQL and RabbitMQ containers
82-
> - Stop and remove any running `web` container
84+
> - Stop and remove any running web container
8385
> - Run DB migrations automatically
8486
8587
### ▶️ Then Run Django:
8688
```bash
8789
python3 manage.py runserver
8890
```
8991

92+
### ▶️ Run Celery Worker:
93+
```bash
94+
celery -A taskflow_api worker --loglevel=info
95+
```
96+
9097
Open:
9198
- Swagger docs: http://localhost:8000/docs/
9299
- API root: http://localhost:8000/api/tasks/
93100

94101
---
95102

103+
## 🧩 Celery Logging Task
104+
105+
When a task is created through the API, a Celery worker will automatically:
106+
107+
- Run `log_task_action` in the background using `celery -A taskflow_api worker --loglevel=info`
108+
- Write an entry like this to `logs/task_activity.log`:
109+
110+
```
111+
[2025-09-14 19:45:00] Task #12 ('Example Task') was created
112+
```
113+
114+
---
115+
96116
## 🏭 Production Mode (Dockerized Web)
97117

98118
### ▶️ Build & Run All Services:
@@ -117,11 +137,12 @@ docker compose up --build
117137
- **Backend**: Django 5, DRF
118138
- **Database**: PostgreSQL (Docker)
119139
- **Broker**: RabbitMQ (Docker)
140+
- **Background Jobs**: Celery (activity logging)
120141
- **Containerization**: Docker, Docker Compose
121142
- **CI-ready**: Gunicorn + environment-based config
122143

123144
---
124145

125146
## 📜 License
126147

127-
MIT © Omid Hashemzadeh
148+
MIT © Omid Hashemzadeh

docker-compose.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ services:
2626
image: rabbitmq:management
2727
ports:
2828
- "5672:5672"
29-
- "15672:15672"
29+
- "15672:15672"
30+
31+
celery:
32+
build: .
33+
command: celery -A taskflow_api worker --loglevel=info
34+
volumes:
35+
- .:/app
36+
depends_on:
37+
- db
38+
- rabbitmq
39+
env_file:
40+
- .env

tasks/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def log_task_action(task_id, action):
88
"""Log task ID and title to a file."""
99
try:
1010
task = Task.objects.get(id=task_id)
11-
log_line = f"[{now()}] Logging Celery Task : Task #{task.id} ('{task.title}') was {action} using celery.\n"
11+
log_line = f"[{now()}] Task #{task.id} ('{task.title}') was {action} via Celery background task.\n"
1212
except Task.DoesNotExist:
1313
log_line = f"[{now()}] ERROR: Task {task_id} not found for action '{action}'\n"
1414

0 commit comments

Comments
 (0)