Overview
Celery handles resource-intensive operations asynchronously:- Photo optimization (AVIF conversion)
- External image downloads from Football Kit Archive
- Orphaned photo cleanup
- Periodic maintenance tasks
Celery requires Redis as the message broker. Make sure Redis is running before starting Celery workers.
Celery Configuration
config/celery_app.py
The Celery application is configured inconfig/celery_app.py:
config/celery_app.py
Django Settings
Celery settings inconfig/settings/base.py:
Background Tasks
Photo Processing Tasks
Defined infootycollect/collection/tasks.py:
process_photo_to_avif
process_photo_to_avif
Converts uploaded photos to AVIF format for optimal compression:Usage:
- Triggered automatically when photos are uploaded
- Optimizes image file size while maintaining quality
- Updates
image_aviffield on Photo model
check_item_photo_processing
check_item_photo_processing
Checks if all photos for an item have been processed:Usage:
- Monitors photo processing status
- Updates
is_processing_photosflag when complete - Used to show processing indicators in UI
External Image Download
download_external_image_and_attach
download_external_image_and_attach
Downloads images from Football Kit Archive and attaches them to items:Features:
- Downloads images from Football Kit Archive
- Validates allowed image hosts (security)
- Supports rotating proxies (if configured)
- Automatically triggers photo processing
- Only downloads from whitelisted hosts
- Configurable via
ALLOWED_EXTERNAL_IMAGE_HOSTSsetting
Cleanup Tasks
cleanup_orphaned_photos
cleanup_orphaned_photos
Removes incomplete photo uploads (older than 24 hours):Schedule: Runs daily (configured in django-celery-beat)
cleanup_old_incomplete_photos
cleanup_old_incomplete_photos
Removes old incomplete photos (older than 7 days):Schedule: Runs weekly
Running Celery
Development
1
Start Redis
2
Start Celery Worker
3
Start Celery Beat (Optional)
Docker Compose
Using Docker Compose (recommended):docker-compose.local.yml
Periodic Tasks (django-celery-beat)
FootyCollect uses django-celery-beat for scheduling periodic tasks.Setup Schedule
1
Run migrations
2
Setup default schedule
- Orphaned photos cleanup (daily)
- Old incomplete photos cleanup (weekly)
3
Customize in Django Admin
Navigate to Django Admin → django_celery_beat → Periodic tasksAdjust task intervals as needed.
Managing Periodic Tasks
In Django Admin, you can:- Create new periodic tasks
- Edit task schedules (cron/interval)
- Enable/disable tasks
- View task execution history
Monitoring
Flower (Task Monitor)
Flower provides a web-based UI for monitoring Celery:http://localhost:5555
Features:
- Real-time task monitoring
- Worker status
- Task history
- Task statistics
Logs
Celery logs task execution:Task Best Practices
Idempotency
Idempotency
Tasks should be idempotent (safe to run multiple times):
Error Handling
Error Handling
Handle errors gracefully:
Task Timeouts
Task Timeouts
Set task time limits:
Troubleshooting
Tasks not executing
Tasks not executing
Check:
- Redis is running:
redis-cli ping - Celery worker is running
- Task is registered:
celery -A config.celery_app inspect registered - No errors in Celery logs
Tasks timing out
Tasks timing out
Solutions:
- Increase
time_limitsetting - Break task into smaller subtasks
- Check for blocking operations
Memory issues
Memory issues
Solutions:
- Reduce worker concurrency:
--concurrency=2 - Enable worker max tasks:
--max-tasks-per-child=1000 - Monitor memory usage
Next Steps
Project Structure
Understanding the codebase
Running Tests
Testing Celery tasks
Deployment
Production Celery setup