Skip to content

Latest commit

 

History

History
123 lines (79 loc) · 2.48 KB

File metadata and controls

123 lines (79 loc) · 2.48 KB

FastAPI + Celery Template

Production-ready FastAPI application with Celery task processing. Includes health checks, task triggers, worker monitoring, and Railway deployment support.

Deploy on Railway

Manual Deployment

  1. Add Redis to your Railway project

    New → Database → Add Redis
    
  2. Deploy this service

    New → GitHub Repo → Select this repository
    
  3. Set environment variables

    REDIS_URL = ${{Redis.REDIS_URL}}
    
  4. Deploy! Railway will automatically detect the Dockerfile

Deploy with Worker and Beat

For full functionality, deploy all three templates:

  1. This FastAPI template (API)
  2. Celery Worker template (processes tasks)
  3. Celery Beat template (schedules tasks)

All should share the same Redis instance via ${{Redis.REDIS_URL}}.

Customization

Adding New Endpoints

Edit app/main.py:

@app.post("/your-endpoint")
async def your_endpoint(data: YourModel):
    # Your logic here
    task_id = trigger_your_task(data)
    return {"task_id": task_id}

Adding New Tasks

  1. Define task signature in app/tasks.py:
your_task = celery_app.signature("your_task")

def trigger_your_task(data):
    result = your_task.delay(data)
    return result.id
  1. Implement the actual task in the worker template

Configuration

Edit app/config.py to add more settings:

class Settings(BaseSettings):
    # Add your settings here
    database_url: str = ""
    api_key: str = ""

Testing

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# With coverage
pytest --cov=app tests/

API Documentation

Swagger UI: http://localhost:8000/docs | ReDoc: http://localhost:8000/redoc

Common Use Cases

Email sending, image processing, data exports, webhook handling, notifications.

Troubleshooting

Redis connection failed

# Check Redis is running
docker ps | grep redis

# Test connection
redis-cli -u $REDIS_URL ping

Workers not visible

Deploy the worker template and ensure it shares the same REDIS_URL

Tasks not processing

  1. Ensure worker service is running
  2. Check worker logs for errors
  3. Verify Redis connectivity

License

MIT