|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Hammerwork is a high-performance, database-driven job queue library for Rust with support for both PostgreSQL and MySQL. It provides async/await job processing with configurable retry logic, delayed jobs, and worker pools. |
| 8 | + |
| 9 | +## Common Development Commands |
| 10 | + |
| 11 | +### Building and Testing |
| 12 | +```bash |
| 13 | +# Build the project |
| 14 | +cargo build |
| 15 | + |
| 16 | +# Run tests |
| 17 | +cargo test |
| 18 | + |
| 19 | +# Run tests with specific features |
| 20 | +cargo test --features postgres |
| 21 | +cargo test --features mysql |
| 22 | + |
| 23 | +# Run examples |
| 24 | +cargo run --example postgres_example --features postgres |
| 25 | +cargo run --example mysql_example --features mysql |
| 26 | + |
| 27 | +# Check code formatting |
| 28 | +cargo fmt --check |
| 29 | + |
| 30 | +# Run clippy for linting |
| 31 | +cargo clippy -- -D warnings |
| 32 | +``` |
| 33 | + |
| 34 | +### Feature Flags |
| 35 | +The project uses feature flags for database support: |
| 36 | +- `postgres`: Enable PostgreSQL support |
| 37 | +- `mysql`: Enable MySQL support |
| 38 | +- `default`: No database features enabled by default |
| 39 | + |
| 40 | +## Architecture |
| 41 | + |
| 42 | +The codebase follows a modular architecture with clear separation of concerns: |
| 43 | + |
| 44 | +### Core Components |
| 45 | + |
| 46 | +1. **Job (`src/job.rs`)** |
| 47 | + - Defines the `Job` struct with UUID, payload, status, retry logic |
| 48 | + - Job statuses: Pending, Running, Completed, Failed, Retrying |
| 49 | + - Supports delayed execution and configurable retry attempts |
| 50 | + |
| 51 | +2. **Queue (`src/queue.rs`)** |
| 52 | + - `DatabaseQueue` trait defines the interface for database operations |
| 53 | + - `JobQueue<DB>` generic struct wraps database connection pools |
| 54 | + - Database-specific implementations in `postgres` and `mysql` modules |
| 55 | + - Uses database transactions for atomic job operations |
| 56 | + |
| 57 | +3. **Worker (`src/worker.rs`)** |
| 58 | + - `Worker<DB>` processes jobs from specific queues |
| 59 | + - Configurable polling intervals, retry delays, and max retries |
| 60 | + - `WorkerPool<DB>` manages multiple workers with graceful shutdown |
| 61 | + - Uses async channels for shutdown coordination |
| 62 | + |
| 63 | +4. **Error Handling (`src/error.rs`)** |
| 64 | + - `HammerworkError` enum using thiserror for structured error handling |
| 65 | + - Wraps SQLx, serialization, and custom errors |
| 66 | + |
| 67 | +### Database Schema |
| 68 | + |
| 69 | +Both PostgreSQL and MySQL implementations use a single table `hammerwork_jobs` with: |
| 70 | +- Job metadata (id, queue_name, status, attempts) |
| 71 | +- Timing information (created_at, scheduled_at, started_at, completed_at) |
| 72 | +- Payload data (JSON/JSONB) and error messages |
| 73 | +- Optimized indexes for queue polling |
| 74 | + |
| 75 | +### Key Design Patterns |
| 76 | + |
| 77 | +- **Generic over Database**: Uses `sqlx::Database` trait to support multiple databases |
| 78 | +- **Feature-gated implementations**: Database-specific code behind feature flags |
| 79 | +- **Async-first**: Built on Tokio with async/await throughout |
| 80 | +- **Transaction safety**: Uses database transactions for job state changes |
| 81 | +- **Type-safe job handling**: Job handlers return `Result<()>` for error handling |
| 82 | + |
| 83 | +## Development Notes |
| 84 | + |
| 85 | +- SQLx compile-time query checking requires database connections |
| 86 | +- PostgreSQL uses `FOR UPDATE SKIP LOCKED` for efficient job polling |
| 87 | +- MySQL implementation uses transaction-based locking (less optimal) |
| 88 | +- Tracing is integrated for observability |
| 89 | +- Examples demonstrate both database configurations |
0 commit comments