Skip to content

Commit af0b8e5

Browse files
authored
Merge pull request #6 from omidcodes/006-add-celery-to-docker-compose
006 add celery to docker compose
2 parents f6967ff + 0582894 commit af0b8e5

4 files changed

Lines changed: 58 additions & 8 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 via Celery background task.
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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ services:
1212
env_file:
1313
- .env
1414
environment:
15+
# Useful if override needed, else .env handles it
1516
POSTGRES_HOST: db
17+
CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672//
18+
1619

1720
db:
1821
image: postgres
@@ -25,5 +28,20 @@ services:
2528
rabbitmq:
2629
image: rabbitmq:management
2730
ports:
28-
- "5672:5672"
29-
- "15672:15672"
31+
- "5672:5672" # AMQP (used by Celery)
32+
- "15672:15672" # RabbitMQ management UI
33+
34+
celery:
35+
build: .
36+
command: celery -A taskflow_api worker --loglevel=info
37+
volumes:
38+
- .:/app
39+
depends_on:
40+
- db
41+
- rabbitmq
42+
env_file:
43+
- .env
44+
environment:
45+
# Useful if override needed, else .env handles it
46+
POSTGRES_HOST: db
47+
CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672//

lint-clean.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
echo "🔍 Linting and fixing with Ruff (ignoring migrations)..."
4+
5+
# Lint and auto-fix, ignoring migration files
6+
ruff check --fix . --exclude migrations
7+
8+
# Format code (also ignores excluded by default)
9+
ruff format . --exclude migrations
10+
11+
echo "✅ Code linted and formatted!"

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)