Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DanielPopoola/async_worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Async PostgreSQL Worker with FastAPI

A low-level asynchronous job queue system built with FastAPI, PostgreSQL, and asyncio. This project demonstrates modern async Python patterns for basic background job processing systems.

πŸš€ Features

  • Async-First Design: Built with asyncio and async/await patterns throughout
  • Job Queue System: PostgreSQL-backed job queue with atomic job claiming
  • REST API: FastAPI endpoints for job submission and status tracking
  • Background Worker: Continuous job processing with graceful shutdown
  • Connection Pooling: Efficient database connection management
  • Duplicate Detection: Smart handling of duplicate job submissions
  • Concurrent Processing: Multiple jobs processed simultaneously

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI App   β”‚    β”‚   PostgreSQL     β”‚    β”‚ Background      β”‚
β”‚                 β”‚    β”‚                  β”‚    β”‚ Worker          β”‚
β”‚ β€’ Job Creation  │◄──►│ β€’ Job Queue      │◄──►│                 β”‚
β”‚ β€’ Status Check  β”‚    β”‚ β€’ Connection     β”‚    β”‚ β€’ Job Processingβ”‚
β”‚ β€’ Health Check  β”‚    β”‚   Pool           β”‚    β”‚ β€’ Error Handlingβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Prerequisites

  • Python 3.8+
  • PostgreSQL 12+
  • Docker (optional, for easy PostgreSQL setup)

πŸ”§ Installation

  1. Clone the repository:

    git clone <repository-url>
    cd async-worker-project
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up PostgreSQL:

    Option A: Using Docker:

    docker-compose up -d

    Option B: Local PostgreSQL:

    createdb async_worker_db
  4. Run database migrations:

    psql -d async_worker_db -f migrations/init.sql
  5. Configure environment:

    export DATABASE_URL="postgresql://user:password@localhost/async_worker_db"

πŸš€ Quick Start

  1. Start the application:

    uvicorn app.main:app --reload
  2. Create a job:

    curl -X POST "http://localhost:8000/jobs" \
         -H "Content-Type: application/json" \
         -d '{"payload": {"task": "hello_world", "data": "test message"}}'
  3. Check job status:

    curl "http://localhost:8000/jobs/{job_id}"
  4. Health check:

    curl "http://localhost:8000/health"

πŸ“ Project Structure

async_worker/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py              # FastAPI application and lifespan management
β”‚   β”œβ”€β”€ models.py            # Pydantic models for request/response
β”‚   β”œβ”€β”€ database.py          # Database connection and operations
β”‚   β”œβ”€β”€ worker.py            # Background job processor
β”œβ”€β”€ migrations/
β”‚   └── init.sql             # Database schema
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_api.py          # API endpoint tests
β”‚   β”œβ”€β”€ test_worker.py       # Worker logic tests
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ docker-compose.yml       # PostgreSQL for development
└── README.md

πŸ”„ Job Lifecycle

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ queued  │───►│ processing  │───►│ completed β”‚    β”‚   failed   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚                                  β–²
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              (on error)

🎯 API Endpoints

Create Job

  • POST /jobs
  • Body: {"payload": {"key": "value"}}
  • Response: {"job_id": "uuid", "status": "queued"}

Get Job Status

  • GET /jobs/{job_id}
  • Response:
    {
      "job_id": "uuid",
      "status": "completed",
      "attempt_count": 1,
      "payload": {"key": "value"}
    }

Health Check

  • GET /health
  • Response: {"status": "healthy"}

πŸ§ͺ Testing

Run the test suite:

# Install test dependencies
uv pip install -r requirements.txt

# Run tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=app --cov-report=html

πŸ—οΈ Key Design Patterns

1. Async Database Operations

  • Connection pooling for concurrent request handling
  • Atomic job claiming with FOR UPDATE SKIP LOCKED
  • Proper resource cleanup with async context managers

2. Job Queue Management

  • PostgreSQL-based persistence for reliability
  • Duplicate job detection and replacement
  • Worker polling with graceful shutdown

3. Error Handling

  • Structured error responses
  • Basic retry logic (extensible for advanced patterns)
  • Connection failure recovery

πŸ“ˆ Performance Characteristics

  • Concurrent Jobs: Handles multiple jobs simultaneously
  • Database Connections: Efficient pooling (2-5 connections by default)
  • Response Time: Sub-100ms for job submission
  • Throughput: Depends on job complexity and database performance

πŸ›£οΈ Roadmap

Phase 3: Robust Error Handling

  • Exponential backoff with jitter
  • Circuit breaker pattern
  • Dead letter queue for failed jobs
  • Comprehensive timeout hierarchy

Phase 4: Real Task Execution

  • Task registry system
  • Email sending tasks
  • File processing tasks
  • Rate limiting and resource management

Phase 5: Production Features

  • Structured logging and monitoring
  • Health check improvements
  • Horizontal scaling support
  • Admin dashboard

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ Development Notes

Core Async Principles Applied

  1. Control Flow Management: Just-in-time connection acquisition, proper use of await vs asyncio.gather()
  2. Critical Section Protection: Atomic job claiming prevents race conditions
  3. Task Lifecycle Management: Proper cleanup with async context managers
  4. Resource Management: Connection pooling with health monitoring
  5. Graceful Degradation: Error handling with structured responses

Database Design Decisions

  • Connection as Parameter Pattern: Enables flexible transaction boundaries
  • Atomic Operations: Single SQL queries for consistency
  • Pool-Level Health Checks: More efficient than per-connection validation
  • Structured Return Values: Clear distinction between business logic and system errors

πŸ“š Learning Resources

This project demonstrates several advanced async Python concepts:

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Troubleshooting

Common Issues

Database Connection Errors:

# Check PostgreSQL is running
pg_isready -h localhost -p 5432

# Verify database exists
psql -l | grep async_worker_db

Import Errors:

# Ensure you're in the project root and PYTHONPATH is set
export PYTHONPATH=$(pwd)

Worker Not Processing Jobs:

  • Check database connectivity
  • Verify worker is running (check logs)
  • Ensure jobs are in 'queued' status

Debug Mode

Run with debug logging:

uvicorn app.main:app --reload --log-level debug

πŸ“Š Monitoring

Basic monitoring endpoints:

  • /health - Application health status
  • /metrics - Basic application metrics (planned)
  • Database queries for job queue depth and processing rates

For production deployments, consider integrating with:

  • Prometheus for metrics collection
  • Grafana for dashboards
  • Sentry for error tracking
  • Structured logging with ELK stack

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages