Skip to main content
FootyCollect uses Celery for asynchronous task processing, including photo optimization, external image downloads, and periodic cleanup tasks.

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 in config/celery_app.py:
config/celery_app.py
Celery automatically discovers tasks from tasks.py files in all installed Django apps.

Django Settings

Celery settings in config/settings/base.py:

Background Tasks

Photo Processing Tasks

Defined in footycollect/collection/tasks.py:
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_avif field on Photo model
Checks if all photos for an item have been processed:
Usage:
  • Monitors photo processing status
  • Updates is_processing_photos flag when complete
  • Used to show processing indicators in UI

External Image Download

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
Security:
  • Only downloads from whitelisted hosts
  • Configurable via ALLOWED_EXTERNAL_IMAGE_HOSTS setting

Cleanup Tasks

Removes incomplete photo uploads (older than 24 hours):
Schedule: Runs daily (configured in django-celery-beat)
Removes old incomplete photos (older than 7 days):
Schedule: Runs weekly

Running Celery

Development

1

Start Redis

2

Start Celery Worker

This starts a worker that processes tasks from the queue.
3

Start Celery Beat (Optional)

This starts the scheduler for periodic tasks.

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

This creates default periodic tasks:
  • Orphaned photos cleanup (daily)
  • Old incomplete photos cleanup (weekly)
3

Customize in Django Admin

Navigate to Django Admin → django_celery_beatPeriodic 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
Use the Django Admin interface to manage periodic tasks without code changes.

Monitoring

Flower (Task Monitor)

Flower provides a web-based UI for monitoring Celery:
Access at http://localhost:5555 Features:
  • Real-time task monitoring
  • Worker status
  • Task history
  • Task statistics

Logs

Celery logs task execution:

Task Best Practices

Tasks should be idempotent (safe to run multiple times):
Handle errors gracefully:
Set task time limits:

Troubleshooting

Check:
  1. Redis is running: redis-cli ping
  2. Celery worker is running
  3. Task is registered: celery -A config.celery_app inspect registered
  4. No errors in Celery logs
Solutions:
  • Increase time_limit setting
  • Break task into smaller subtasks
  • Check for blocking operations
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