|
| 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 | +**Docket** (`pydocket` on PyPI) is a distributed background task system for Python functions with Redis-backed persistence. It enables scheduling both immediate and future work with comprehensive dependency injection, retry mechanisms, and fault tolerance. |
| 8 | + |
| 9 | +**Key Requirements**: Python 3.12+, Redis 6.2+ |
| 10 | + |
| 11 | +## Development Commands |
| 12 | + |
| 13 | +### Testing |
| 14 | + |
| 15 | +```bash |
| 16 | +# Run full test suite with coverage and parallel execution |
| 17 | +pytest |
| 18 | + |
| 19 | +# Run specific test |
| 20 | +pytest tests/test_docket.py::test_specific_function |
| 21 | + |
| 22 | +``` |
| 23 | + |
| 24 | +The project REQUIRES 100% test coverage |
| 25 | + |
| 26 | +### Code Quality |
| 27 | + |
| 28 | +```bash |
| 29 | +# Lint and format code |
| 30 | +ruff check |
| 31 | +ruff format |
| 32 | + |
| 33 | +# Type checking |
| 34 | +pyright |
| 35 | +pyright tests |
| 36 | + |
| 37 | +# Run all pre-commit hooks |
| 38 | +pre-commit run --all-files |
| 39 | +``` |
| 40 | + |
| 41 | +### Development Setup |
| 42 | + |
| 43 | +```bash |
| 44 | +# Install development dependencies |
| 45 | +uv sync --group dev |
| 46 | + |
| 47 | +# Install pre-commit hooks |
| 48 | +pre-commit install |
| 49 | +``` |
| 50 | + |
| 51 | +### Git Workflow |
| 52 | + |
1 | 53 | - This project uses Github for issue tracking |
| 54 | +- This project can use git worktrees under .worktrees/ |
| 55 | + |
| 56 | +## Core Architecture |
| 57 | + |
| 58 | +### Key Classes |
| 59 | + |
| 60 | +- **`Docket`** (`src/docket/docket.py`): Central task registry and scheduler |
| 61 | + |
| 62 | + - `add()`: Schedule tasks for execution |
| 63 | + - `replace()`: Replace existing scheduled tasks |
| 64 | + - `cancel()`: Cancel pending tasks |
| 65 | + - `strike()`/`restore()`: Conditionally block/unblock tasks |
| 66 | + - `snapshot()`: Get current state for observability |
| 67 | + |
| 68 | +- **`Worker`** (`src/docket/worker.py`): Task execution engine |
| 69 | + |
| 70 | + - `run_forever()`/`run_until_finished()`: Main execution loops |
| 71 | + - Handles concurrency, retries, and dependency injection |
| 72 | + - Maintains heartbeat for liveness tracking |
| 73 | + |
| 74 | +- **`Execution`** (`src/docket/execution.py`): Task execution context with metadata |
| 75 | + |
| 76 | +### Dependencies System (`src/docket/dependencies.py`) |
| 77 | + |
| 78 | +Rich dependency injection supporting: |
| 79 | + |
| 80 | +- Context access: `CurrentDocket`, `CurrentWorker`, `CurrentExecution` |
| 81 | +- Retry strategies: `Retry`, `ExponentialRetry` |
| 82 | +- Special behaviors: `Perpetual` (self-rescheduling), `Timeout` |
| 83 | +- Custom injection: `Depends()` |
| 84 | +- Contextual logging: `TaskLogger` |
| 85 | + |
| 86 | +### Redis Data Model |
| 87 | + |
| 88 | +- **Streams**: `{docket}:stream` (ready tasks), `{docket}:strikes` (commands) |
| 89 | +- **Sorted Sets**: `{docket}:queue` (scheduled tasks), `{docket}:workers` (heartbeats) |
| 90 | +- **Hashes**: `{docket}:{key}` (parked task data) |
| 91 | +- **Sets**: `{docket}:worker-tasks:{worker}` (worker capabilities) |
| 92 | + |
| 93 | +### Task Lifecycle |
| 94 | + |
| 95 | +1. Registration with `Docket.register()` or `@docket.task` |
| 96 | +2. Scheduling: immediate → Redis stream, future → Redis sorted set |
| 97 | +3. Worker processing: scheduler moves due tasks, workers consume via consumer groups |
| 98 | +4. Execution: dependency injection, retry logic, acknowledgment |
| 99 | + |
| 100 | +## Project Structure |
| 101 | + |
| 102 | +### Source Code |
| 103 | + |
| 104 | +- `src/docket/` - Main package |
| 105 | + - `__init__.py` - Public API exports |
| 106 | + - `docket.py` - Core Docket class |
| 107 | + - `worker.py` - Worker implementation |
| 108 | + - `execution.py` - Task execution context |
| 109 | + - `dependencies.py` - Dependency injection system |
| 110 | + - `tasks.py` - Built-in utility tasks |
| 111 | + - `cli.py` - Command-line interface |
| 112 | + |
| 113 | +### Testing and Examples |
| 114 | + |
| 115 | +- `tests/` - Comprehensive test suite |
| 116 | +- `examples/` - Usage examples |
| 117 | +- `chaos/` - Chaos testing framework |
| 118 | + |
| 119 | +## CLI Usage |
| 120 | + |
| 121 | +```bash |
| 122 | +# Run a worker |
| 123 | +docket worker --url redis://localhost:6379/0 --tasks your.module --concurrency 4 |
| 124 | + |
| 125 | +# See all commands |
| 126 | +docket --help |
| 127 | +``` |
0 commit comments