Overview
Scribe Backend consists of three main services that work together:- FastAPI Server (uvicorn) - Handles HTTP requests and API endpoints
- Celery Worker - Processes background email generation tasks
- Redis - Message broker and result backend for Celery
- Flower (optional) - Web-based monitoring UI for Celery tasks
All services must be running for the application to function properly. This guide shows both quick-start and manual startup methods.
Prerequisites
Before starting, ensure you’ve completed:- Installation - Python 3.13+, dependencies, Playwright
- Configuration -
.envfile with API keys - Database Setup - Alembic migrations applied
Quick Start (Recommended)
Start all services with a single command:make serve does:
- Starts uvicorn server on port 8000
- Starts Celery worker with concurrency=1
- Displays logs from both services in the terminal
Use Ctrl+C to stop both services. Redis continues running in the background.
Manual Start (Detailed Control)
For debugging or customization, start services individually:Start FastAPI Server
Open a terminal and start the development server with hot reload:Expected output:
The
--reload flag enables automatic reloading when code changes are detected. Remove this in production.Start Flower Monitoring (Optional)
Open a third terminal for the Flower UI:Access Flower:
- URL: http://localhost:5555
- View: Active tasks, worker stats, task history, success/failure rates
Service Configuration
FastAPI Server (uvicorn)
Development:--reload: Enable hot reload (development only)--host 0.0.0.0: Listen on all network interfaces--port 8000: HTTP port--timeout-keep-alive 120: Keep-alive timeout for long-polling clients--workers 1: Number of worker processes (increase for production)
Celery Worker
Standard Configuration:| Option | Value | Purpose |
|---|---|---|
-A | celery_config.celery_app | Celery app instance |
--loglevel | info | Logging verbosity (debug, info, warning, error) |
--queues | email_default | Queues to consume from |
--concurrency | 1 | Number of parallel tasks (1 for Raspberry Pi) |
--pool | solo | Execution pool (solo = single-threaded, memory efficient) |
Memory Optimization: Using
--pool=solo and --concurrency=1 keeps memory usage under 512MB, ideal for Raspberry Pi deployments.Redis Configuration
Default Configuration:- DB 0: Celery broker (task queue)
- DB 1: Celery result backend (task state + results)
Health Checks
Verify all services are running correctly:FastAPI Health Check
Health check failed
Health check failed
Possible causes:
- Database connection failed → Check
.envdatabase credentials - Supabase service unreachable → Verify
SUPABASE_URL - Missing environment variables → Review
.envfile
API Documentation
FastAPI auto-generates interactive API docs:- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
The interactive docs allow you to test API endpoints directly from the browser. JWT authentication is supported via the “Authorize” button.
Celery Worker Health
Check worker status using Celery’s inspect commands:Flower Monitoring Dashboard
If Flower is running, access comprehensive monitoring: URL: http://localhost:5555 Features:- Real-time task progress
- Worker resource usage (CPU, memory)
- Task success/failure rates
- Task execution history
- Broker connection status
Flower is optional but highly recommended for debugging task issues and monitoring queue depth.
Testing the Pipeline
Generate a Test Email
Send a test request to the email generation endpoint:Poll Task Status
Retrieve Generated Email
Development Workflows
Hot Reload
When running with--reload, FastAPI automatically restarts when you edit:
- Route handlers (
api/routes/*.py) - Models (
models/*.py) - Services (
services/*.py)
Viewing Logs
FastAPI Logs:- Printed to the terminal where
uvicornis running - Structured logging via Logfire (if configured)
- Printed to the terminal where
celery workeris running - Includes task start, success, failure, and retry events
Debugging
Enable debug logging
Set log level to DEBUG in your Restart the FastAPI server and Celery worker.
.env file:Use Logfire for tracing
If
LOGFIRE_TOKEN is configured, view distributed traces at:Filter by service_name=scribe-api or service_name=scribe-celery-worker.Stopping Services
Stop All Services (Quick)
uvicorn, celery, and flower processes.
Stop Services Manually
FastAPI Server:- Press Ctrl+C in the uvicorn terminal
- Press Ctrl+C in the Celery terminal
- Or send SIGTERM:
pkill -f "celery.*worker"
- Press Ctrl+C in the Flower terminal
Always stop Celery workers gracefully with Ctrl+C to allow them to finish in-progress tasks.
Production Setup
For production deployments, use systemd services or process managers:Using systemd (Linux)
Create service files for each component: FastAPI Service (/etc/systemd/system/scribe-api.service):
/etc/systemd/system/scribe-celery.service):
Troubleshooting
Redis connection refused
Redis connection refused
Error:
redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379Solution:- Start Redis:
redis-server --daemonize yes - Verify:
redis-cli ping→ Should respond “PONG” - Check port: Ensure Redis is listening on 6379 (or update
.env)
Celery worker not picking up tasks
Celery worker not picking up tasks
Issue: Tasks stay in PENDING statusSolutions:
- Verify worker is running:
celery -A celery_config.celery_app inspect active - Check queue name matches:
--queues=email_default - Restart worker: Ctrl+C and rerun the worker command
- Clear Redis:
redis-cli FLUSHALL(development only)
Port 8000 already in use
Port 8000 already in use
Error:
OSError: [Errno 48] Address already in useSolution:- Find process:
lsof -i :8000 - Kill it:
kill -9 <PID> - Or use a different port:
uvicorn main:app --port 8001
Out of memory (Celery worker crash)
Out of memory (Celery worker crash)
Issue: Worker crashes with “Killed” messageSolution:
- Verify concurrency=1:
--concurrency=1 - Use solo pool:
--pool=solo - Add swap space (Raspberry Pi):
Hot reload not working
Hot reload not working
Issue: Code changes not reflectedSolution:
- FastAPI: Ensure
--reloadflag is present - Celery: Manual restart required (no auto-reload)
- Check file permissions: Virtual environment must be writable
Next Steps
- Deploy to production → Deployment Guide
- Understand the pipeline → Pipeline Architecture
- API reference → API Documentation
