first commit
This commit is contained in:
404
DOCKER_CELERY.md
Normal file
404
DOCKER_CELERY.md
Normal file
@@ -0,0 +1,404 @@
|
||||
# 🚀 Docker Compose ile Celery Kullanımı
|
||||
|
||||
Bu döküman, Docker Compose üzerinde Celery worker ve beat'in nasıl çalıştırılacağını açıklar.
|
||||
|
||||
## 📋 Genel Bakış
|
||||
|
||||
Projede iki Docker Compose yapılandırması bulunmaktadır:
|
||||
|
||||
1. **docker-compose.yml** - Development (Geliştirme) ortamı
|
||||
2. **docker-compose.prod.yml** - Production (Canlı) ortamı
|
||||
|
||||
Her iki ortamda da Celery worker ve beat aynı container içinde çalışır:
|
||||
```bash
|
||||
celery -A core worker --beat --scheduler django --loglevel=info
|
||||
```
|
||||
|
||||
## 🏗️ Servis Yapısı
|
||||
|
||||
### Development (docker-compose.yml)
|
||||
|
||||
```yaml
|
||||
services:
|
||||
web: # Django uygulaması
|
||||
celery: # Celery worker + beat
|
||||
```
|
||||
|
||||
### Production (docker-compose.prod.yml)
|
||||
|
||||
```yaml
|
||||
services:
|
||||
web-atahan: # Django uygulaması (Gunicorn)
|
||||
celery-atahan: # Celery worker + beat
|
||||
nginx: # Reverse proxy
|
||||
```
|
||||
|
||||
## 🚀 Başlatma
|
||||
|
||||
### Development Ortamı
|
||||
|
||||
```bash
|
||||
# Tüm servisleri başlat (web + celery)
|
||||
docker-compose up
|
||||
|
||||
# Arka planda çalıştır
|
||||
docker-compose up -d
|
||||
|
||||
# Sadece Celery'yi başlat
|
||||
docker-compose up celery
|
||||
|
||||
# Rebuild ile başlat
|
||||
docker-compose up --build
|
||||
```
|
||||
|
||||
### Production Ortamı
|
||||
|
||||
```bash
|
||||
# Tüm servisleri başlat
|
||||
docker-compose -f docker-compose.prod.yml up -d
|
||||
|
||||
# Sadece Celery'yi başlat
|
||||
docker-compose -f docker-compose.prod.yml up -d celery-atahan
|
||||
|
||||
# Rebuild ile başlat
|
||||
docker-compose -f docker-compose.prod.yml up --build -d
|
||||
```
|
||||
|
||||
## 📊 Log İzleme
|
||||
|
||||
### Development
|
||||
|
||||
```bash
|
||||
# Tüm servislerin logları
|
||||
docker-compose logs -f
|
||||
|
||||
# Sadece Celery logları
|
||||
docker-compose logs -f celery
|
||||
|
||||
# Son 100 satır
|
||||
docker-compose logs --tail=100 celery
|
||||
|
||||
# Belirli bir tarihten sonraki loglar
|
||||
docker-compose logs --since 2026-01-15T10:00:00 celery
|
||||
```
|
||||
|
||||
### Production
|
||||
|
||||
```bash
|
||||
# Celery logları
|
||||
docker-compose -f docker-compose.prod.yml logs -f celery-atahan
|
||||
|
||||
# Tüm servisler
|
||||
docker-compose -f docker-compose.prod.yml logs -f
|
||||
```
|
||||
|
||||
## 🔄 Yeniden Başlatma
|
||||
|
||||
### Development
|
||||
|
||||
```bash
|
||||
# Celery'yi yeniden başlat
|
||||
docker-compose restart celery
|
||||
|
||||
# Tüm servisleri yeniden başlat
|
||||
docker-compose restart
|
||||
```
|
||||
|
||||
### Production
|
||||
|
||||
```bash
|
||||
# Celery'yi yeniden başlat
|
||||
docker-compose -f docker-compose.prod.yml restart celery-atahan
|
||||
|
||||
# Tüm servisleri yeniden başlat
|
||||
docker-compose -f docker-compose.prod.yml restart
|
||||
```
|
||||
|
||||
## 🛑 Durdurma
|
||||
|
||||
### Development
|
||||
|
||||
```bash
|
||||
# Servisleri durdur (container'ları kaldır)
|
||||
docker-compose down
|
||||
|
||||
# Sadece Celery'yi durdur
|
||||
docker-compose stop celery
|
||||
|
||||
# Volume'leri de sil
|
||||
docker-compose down -v
|
||||
```
|
||||
|
||||
### Production
|
||||
|
||||
```bash
|
||||
# Servisleri durdur
|
||||
docker-compose -f docker-compose.prod.yml down
|
||||
|
||||
# Sadece Celery'yi durdur
|
||||
docker-compose -f docker-compose.prod.yml stop celery-atahan
|
||||
```
|
||||
|
||||
## 🔧 Container'a Bağlanma
|
||||
|
||||
### Development
|
||||
|
||||
```bash
|
||||
# Celery container'a bash ile bağlan
|
||||
docker-compose exec celery bash
|
||||
|
||||
# Celery container'da komut çalıştır
|
||||
docker-compose exec celery ls -la
|
||||
docker-compose exec celery python manage.py shell
|
||||
```
|
||||
|
||||
### Production
|
||||
|
||||
```bash
|
||||
# Celery container'a bash ile bağlan
|
||||
docker-compose -f docker-compose.prod.yml exec celery-atahan bash
|
||||
|
||||
# Komut çalıştır
|
||||
docker-compose -f docker-compose.prod.yml exec celery-atahan python manage.py shell
|
||||
```
|
||||
|
||||
## 📝 Celery Task Yönetimi
|
||||
|
||||
### Task Sonuçlarını Görüntüleme
|
||||
|
||||
```bash
|
||||
# Django shell aç
|
||||
docker-compose exec web python manage.py shell
|
||||
|
||||
# Shell içinde:
|
||||
from django_celery_results.models import TaskResult
|
||||
|
||||
# Tüm task sonuçları
|
||||
TaskResult.objects.all()
|
||||
|
||||
# Başarılı task'lar
|
||||
TaskResult.objects.filter(status='SUCCESS')
|
||||
|
||||
# Başarısız task'lar
|
||||
TaskResult.objects.filter(status='FAILURE')
|
||||
|
||||
# En son 10 task
|
||||
TaskResult.objects.order_by('-date_done')[:10]
|
||||
```
|
||||
|
||||
### Django Admin'den Task İzleme
|
||||
|
||||
1. Tarayıcıda admin panele girin:
|
||||
- Development: http://localhost:8000/admin/
|
||||
- Production: http://your-domain.com/admin/
|
||||
|
||||
2. Şu bölümlere gidin:
|
||||
- **Django Celery Results** → **Task results** - Task sonuçları
|
||||
- **Django Celery Beat** → **Periodic tasks** - Zamanlanmış task'lar
|
||||
- **Django Celery Beat** → **Intervals** - Periyodik aralıklar
|
||||
|
||||
### Manuel Task Çalıştırma
|
||||
|
||||
```bash
|
||||
# Django shell aç
|
||||
docker-compose exec web python manage.py shell
|
||||
|
||||
# Shell içinde bir task çalıştır:
|
||||
from contact.tasks import send_contact_email
|
||||
|
||||
# Hemen çalıştır (test için)
|
||||
result = send_contact_email(
|
||||
name='Test',
|
||||
email='test@example.com',
|
||||
subject='Test Subject',
|
||||
message='Test message',
|
||||
ip='127.0.0.1'
|
||||
)
|
||||
print(result)
|
||||
|
||||
# Celery queue'ya ekle (asenkron)
|
||||
task = send_contact_email.delay(
|
||||
name='Test',
|
||||
email='test@example.com',
|
||||
subject='Test Subject',
|
||||
message='Test message',
|
||||
ip='127.0.0.1'
|
||||
)
|
||||
print(f"Task ID: {task.id}")
|
||||
print(f"Task Status: {task.status}")
|
||||
```
|
||||
|
||||
## ⚙️ Environment Variables
|
||||
|
||||
### Development (.env veya docker-compose.yml)
|
||||
|
||||
```bash
|
||||
CELERY_BROKER_URL=redis://default:password@host:6379/5
|
||||
CELERY_RESULT_BACKEND=django-db
|
||||
```
|
||||
|
||||
### Production (.env)
|
||||
|
||||
```bash
|
||||
CELERY_BROKER_URL=redis://default:password@host:6379/5
|
||||
CELERY_RESULT_BACKEND=django-db
|
||||
```
|
||||
|
||||
## 🐛 Sorun Giderme
|
||||
|
||||
### Celery Başlamıyor
|
||||
|
||||
```bash
|
||||
# Logları kontrol et
|
||||
docker-compose logs celery
|
||||
|
||||
# Container durumunu kontrol et
|
||||
docker-compose ps
|
||||
|
||||
# Container'ı yeniden başlat
|
||||
docker-compose restart celery
|
||||
|
||||
# Container'ı rebuild et
|
||||
docker-compose up --build celery
|
||||
```
|
||||
|
||||
### Redis Bağlantı Hatası
|
||||
|
||||
```bash
|
||||
# Redis bağlantısını test et
|
||||
docker-compose exec celery python -c "
|
||||
from celery import Celery
|
||||
app = Celery('core')
|
||||
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||
print('Connection OK')
|
||||
"
|
||||
|
||||
# Environment variable'ları kontrol et
|
||||
docker-compose exec celery env | grep CELERY
|
||||
```
|
||||
|
||||
### Task Çalışmıyor
|
||||
|
||||
```bash
|
||||
# Celery worker'ın çalıştığını doğrula
|
||||
docker-compose logs -f celery
|
||||
|
||||
# Task'ın queue'ya eklendiğini kontrol et
|
||||
docker-compose exec web python manage.py shell
|
||||
>>> from django_celery_results.models import TaskResult
|
||||
>>> TaskResult.objects.latest('date_created')
|
||||
|
||||
# Task'ı manuel çalıştır
|
||||
>>> from contact.tasks import send_contact_email
|
||||
>>> send_contact_email.delay('Test', 'test@test.com', 'Subject', 'Message')
|
||||
```
|
||||
|
||||
### Email Gönderilmiyor
|
||||
|
||||
```bash
|
||||
# Email backend ayarlarını kontrol et
|
||||
docker-compose exec web python manage.py shell
|
||||
>>> from django.conf import settings
|
||||
>>> print(settings.EMAIL_BACKEND)
|
||||
>>> print(settings.EMAIL_HOST)
|
||||
>>> print(settings.EMAIL_PORT)
|
||||
|
||||
# MailPit kontrol et (development)
|
||||
# http://localhost:8025
|
||||
```
|
||||
|
||||
## 📈 Performans İzleme
|
||||
|
||||
### Container Kaynak Kullanımı
|
||||
|
||||
```bash
|
||||
# Tüm container'ların kaynak kullanımı
|
||||
docker stats
|
||||
|
||||
# Sadece Celery
|
||||
docker stats django_celery_worker
|
||||
|
||||
# Production
|
||||
docker stats django_celery_prod_atahan
|
||||
```
|
||||
|
||||
### Celery Worker İstatistikleri
|
||||
|
||||
```bash
|
||||
# Celery inspect komutu
|
||||
docker-compose exec celery celery -A core inspect active
|
||||
docker-compose exec celery celery -A core inspect stats
|
||||
docker-compose exec celery celery -A core inspect registered
|
||||
```
|
||||
|
||||
## 🔐 Production Best Practices
|
||||
|
||||
### 1. Log Rotation
|
||||
|
||||
Production'da log dosyaları büyüyebilir. Docker log rotation kullanın:
|
||||
|
||||
```yaml
|
||||
# docker-compose.prod.yml
|
||||
celery-atahan:
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
```
|
||||
|
||||
### 2. Resource Limits
|
||||
|
||||
Container'lara kaynak limiti koyun:
|
||||
|
||||
```yaml
|
||||
celery-atahan:
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.0'
|
||||
memory: 512M
|
||||
reservations:
|
||||
cpus: '0.5'
|
||||
memory: 256M
|
||||
```
|
||||
|
||||
### 3. Health Checks
|
||||
|
||||
Celery health check ekleyin:
|
||||
|
||||
```yaml
|
||||
celery-atahan:
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "celery -A core inspect ping"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
```
|
||||
|
||||
### 4. Restart Policy
|
||||
|
||||
Otomatik yeniden başlatma:
|
||||
|
||||
```yaml
|
||||
celery-atahan:
|
||||
restart: unless-stopped
|
||||
```
|
||||
|
||||
## 📚 Ek Kaynaklar
|
||||
|
||||
- [Celery Documentation](https://docs.celeryq.dev/)
|
||||
- [Django Celery Results](https://django-celery-results.readthedocs.io/)
|
||||
- [Django Celery Beat](https://django-celery-beat.readthedocs.io/)
|
||||
- [Docker Compose Documentation](https://docs.docker.com/compose/)
|
||||
|
||||
## 🆘 Yardım
|
||||
|
||||
Sorun yaşıyorsanız:
|
||||
|
||||
1. Logları kontrol edin: `docker-compose logs -f celery`
|
||||
2. Container durumunu kontrol edin: `docker-compose ps`
|
||||
3. Environment variable'ları kontrol edin: `docker-compose exec celery env`
|
||||
4. Redis bağlantısını test edin
|
||||
5. Task'ları manuel çalıştırın ve sonucu gözlemleyin
|
||||
|
||||
Reference in New Issue
Block a user