A comprehensive demonstration of building async web content processing pipelines using FastAPI + Celery, designed for deployment on Upsun as a multi-app architecture.
- FastAPI Web Application: High-performance async web server with automatic OpenAPI documentation
- Celery Task Queue: Distributed task processing for AI/ML operations
- Redis Message Broker: Fast and reliable message queuing
- Flower Monitoring: Web-based monitoring dashboard for Celery workers
- Web Content Processing: Web scraping and content summarization using OpenAI
- Multi-App Architecture: Designed for Upsun platform deployment
- Comprehensive Logging: Structured logging with JSON output
- Error Handling: Robust error handling with custom exception types
- Validation: Thorough input validation and file type checking
fastapi-celery/
βββ api/ # FastAPI web application (Upsun app)
β βββ main.py # Main FastAPI app with endpoints
β βββ pyproject.toml # API service dependencies
β βββ uv.lock # UV lock file for API
β βββ shared/ # Local copy of shared utilities
β βββ .venv/ # Virtual environment
βββ worker/ # Celery worker processes (Upsun app)
β βββ tasks.py # Celery task definitions
β βββ web_scraper.py # Web content scraping and processing
β βββ pyproject.toml # Worker service dependencies
β βββ uv.lock # UV lock file for worker
β βββ shared/ # Local copy of shared utilities
β βββ .venv/ # Virtual environment
βββ monitoring/ # Flower monitoring (Upsun app)
β βββ tasks.py # Celery config for monitoring
β βββ flowerconfig.py # Flower configuration
β βββ start_flower.py # Flower startup script
β βββ pyproject.toml # Monitoring service dependencies
β βββ uv.lock # UV lock file for monitoring
β βββ shared/ # Local copy of shared utilities
β βββ .venv/ # Virtual environment
βββ shared/ # Original shared utilities (template)
β βββ __init__.py # Package exports
β βββ config.py # Configuration management
β βββ logging_config.py # Logging utilities
β βββ utils.py # Common utilities
β βββ exceptions.py # Custom exception classes
β βββ pyproject.toml # Shared utilities dependencies
βββ .upsun/
β βββ config.yaml # Upsun multi-app deployment config
βββ docker-compose.yml # Local development with Redis
βββ pyproject.toml # Root project dependencies (dev only)
βββ test_system.py # System integration tests
βββ main.py # Legacy main file
βββ README.md # This file
The project uses the following key dependencies:
- Web Framework: FastAPI, Uvicorn
- Task Queue: Celery, Redis, Flower
- AI/ML: OpenAI API for content summarization
- Web Scraping: BeautifulSoup, requests, readability-lxml
- Utilities: Pydantic, python-dotenv, structlog, tenacity
The application uses environment-based configuration with the following key variables:
# Redis/Celery Configuration
REDIS_URL=redis://localhost:6379/0
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0
# API Configuration
API_HOST=0.0.0.0
API_PORT=8000
# Logging
LOG_LEVEL=INFO
LOG_FORMAT=json
# AI/ML (Required for summarization)
OPENAI_API_KEY=your_openai_api_key# File Processing Limits
MAX_FILE_SIZE_MB=100
# Task Configuration
TASK_TIMEOUT_SECONDS=300
MAX_RETRIES=3
# Flower Monitoring
FLOWER_PORT=5555
FLOWER_BASIC_AUTH=admin:admin
# CORS Configuration
CORS_ORIGINS=*
# Web Scraping Configuration
STORAGE_PATH=/app/data
USER_AGENT=FastAPI-Celery-WebScraper/1.0pip install -e .docker-compose up redis -ddocker run -d --name redis -p 6379:6379 redis:7-alpineredis-servercd worker
celery -A tasks worker --loglevel=info --concurrency=4cd api
python main.py
# Or using uvicorn directly
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadcd monitoring
python start_flower.py
# Or using celery flower command
celery -A tasks flower --port=5555The project includes a docker-compose.yml for easy local development:
# Start Redis only
docker-compose up redis -d
# Start all services (when Docker images are built)
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose downThe Docker Compose setup includes:
- Redis: Message broker on port 6379
- API: FastAPI application on port 8000 (when built)
- Worker: Celery worker processes (when built)
- Flower: Monitoring dashboard on port 5555 (when built)
Run the comprehensive test suite:
python test_system.py --verboseThis will test:
- API health checks
- Web content processing endpoints
- Task status monitoring
- Worker statistics
curl http://localhost:8000/healthcurl -X POST "http://localhost:8000/tasks/web/summarize" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/article",
"summary_type": "standard"
}'curl http://localhost:8000/tasks/{task_id}/resultcurl http://localhost:8000/tasks/{task_id}/statuscurl http://localhost:8000/tasks- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Flower UI: http://localhost:5555
- Monitor worker status, task queues, and execution statistics
GET /- API informationGET /health- Health check with dependenciesPOST /tasks/web/summarize- Scrape and summarize web pagesGET /tasks/{task_id}/result- Get detailed results from storageGET /tasks/{task_id}/status- Get task statusGET /tasks- List active tasksGET /tasks/stats- Worker statistics
This project is designed for deployment on Upsun platform with a multi-app architecture. Each service runs in its own container with isolated dependencies.
The project uses Upsun's multi-app deployment with three separate applications:
-
API Application (
api/)- Source root:
api/ - Python 3.13 runtime
- UV package manager for fast dependency resolution
- Isolated dependencies in
pyproject.tomlanduv.lock - FastAPI-specific dependencies only
- Source root:
-
Worker Application (
worker/)- Source root:
worker/ - Python 3.13 runtime
- UV package manager for fast dependency resolution
- Web scraping dependencies (BeautifulSoup, requests, readability)
- Celery worker with web content processing capabilities
- Source root:
-
Flower Application (
monitoring/)- Source root:
monitoring/ - Python 3.13 runtime
- UV package manager for fast dependency resolution
- Minimal dependencies for monitoring dashboard
- Flower monitoring with Celery inspection
- Source root:
Each application uses UV for fast dependency installation:
dependencies:
python3:
uv: "*"
hooks:
build: |
# Use uv for fast dependency installation
uv sync --frozenBenefits of UV:
- Fast Installation: 10-100x faster than pip
- Reproducible Builds: Locked dependencies with
uv.lock - Isolated Environments: Each app has its own dependencies
- Better Caching: Efficient dependency resolution
- Connect Repository: Link your Git repository to Upsun
- Configure Environment Variables: Set required environment variables in Upsun dashboard
- Deploy: Push to main branch to trigger deployment
Each application will:
- Create its own container
- Install only required dependencies using UV
- Use shared utilities from local copies
- Connect to the shared Redis service
- API: FastAPI web application (
https://api.{default}/) - Worker: Celery background processors (internal service)
- Flower: Monitoring dashboard (
https://monitor.{default}/) - Redis: Message broker service (internal service)
- API:
https://api.{default}/ - API Documentation:
https://api.{default}/docs - Monitoring:
https://monitor.{default}/ - Main:
https://www.{default}/(redirects to API)
Each application uses environment-based configuration:
# Required for all services
CELERY_BROKER_URL=redis://redis.internal:6379/0
CELERY_RESULT_BACKEND=redis://redis.internal:6379/0
# Required AI/ML API keys (for worker)
OPENAI_API_KEY=your_openai_key
# Web Scraping Configuration
STORAGE_PATH=/app/data
USER_AGENT=FastAPI-Celery-WebScraper/1.0- Define Task: Add new task function in
worker/tasks.py - Add Processing Logic: Create processing module in
worker/ - Add API Endpoint: Add new endpoint in
api/main.py - Update Tests: Add tests in
test_system.py
# worker/tasks.py
@app.task(bind=True, name='tasks.new_ai_task')
def new_ai_task(self, input_data: dict, task_id: str):
# Implementation here
pass
# api/main.py
@app.post("/tasks/new-ai/process")
async def process_new_ai_task(request: NewAIRequest):
# Implementation here
passModify settings in shared/config.py and update environment variables accordingly.
The application uses structured logging with JSON output. Logs include:
- Request/response logging
- Task execution tracking
- Error handling with context
- Performance metrics
-
Redis Connection Failed
- Ensure Redis is running:
docker-compose up redis -d - Check REDIS_URL environment variable
- Verify port 6379 is not in use by other services
- Ensure Redis is running:
-
Tasks Not Processing
- Verify Celery worker is running
- Check worker logs for errors
- Ensure tasks are properly registered
-
File Upload Errors
- Check file size limits (MAX_FILE_SIZE_MB)
- Verify file types are supported
- Check disk space
-
High Memory Usage
- Reduce worker concurrency
- Implement result expiration
- Monitor task memory usage
Enable debug logging:
export LOG_LEVEL=DEBUG
export DEBUG=trueMonitor service health:
# API Health
curl http://localhost:8000/health
# Worker Stats
curl http://localhost:8000/tasks/statsThis project is a demonstration/example for Upsun deployment and is provided as-is for educational purposes.
This is a demonstration project. For production use, consider:
- Adding authentication/authorization
- Implementing real AI models
- Adding database persistence
- Implementing rate limiting
- Adding comprehensive monitoring
- Setting up CI/CD pipelines