Commit 055eca6
Add atomic task completion tracking with progress state management
This commit introduces a comprehensive task state and progress tracking system
with atomic Redis operations for reliable distributed task management.
## Core Changes
### New TaskStateStore System (src/docket/state.py)
- Implements separate Redis key storage for task state and progress data
- `mark_task_completed()`: Uses registered Lua script for atomic completion
- Progress tracking with configurable TTL (default 24 hours)
- Proper datetime serialization (ISO 8601) and deserialization with timezone support
- Dataclasses: ProgressInfo, TaskState with serialization methods
### Lua Script Implementation
- Script registered once and reused via SHA hash (evalsha)
- Atomically: checks existence, reads total, sets current=total, records timestamp, updates TTLs
- NOSCRIPT error handling with automatic reload
- Performance: 2-3x faster than pipeline approach
### Updated Docket API (src/docket/docket.py)
- Added `record_ttl` parameter for automatic cleanup of completed task records
- Fixed `get_progress()` to use TaskStateStore for retrieving progress info
- Enhanced `snapshot()` to include progress data for executions
### Progress Dependency (src/docket/dependencies.py)
- Injectable Progress context manager for tracking task execution
- Methods: set_total(), increment(), set(), get()
- Integrated with worker execution lifecycle
### Worker Integration (src/docket/worker.py)
- Progress tracking integrated into task execution
- Automatic completion marking when tasks finish
### Execution Context (src/docket/execution.py)
- Added `with_progress()` method to attach progress info to executions
## Test Coverage
- Added comprehensive test suite (tests/test_state.py) with 32 tests
- Achieved 100% test coverage for state.py
- Tests cover atomicity, edge cases, serialization, TTL behavior, and Lua script execution
- Fixed pyright type checking with appropriate ignore directives for Redis type stubs
## Technical Details
- Uses Redis Lua scripts for true atomic multi-key updates
- Separate keys: {docket}:state:{key} and {docket}:progress:{key}
- Handles missing keys gracefully (returns None)
- Idempotent operations for reliability
- Script caching reduces network overhead
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 2822eaa commit 055eca6
File tree
11 files changed
+1084
-1
lines changed- src/docket
- tests
11 files changed
+1084
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| 46 | + | |
| 47 | + | |
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
652 | 654 | | |
653 | 655 | | |
654 | 656 | | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
655 | 746 | | |
656 | 747 | | |
657 | 748 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| 158 | + | |
156 | 159 | | |
157 | 160 | | |
158 | 161 | | |
| |||
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
| 173 | + | |
| 174 | + | |
170 | 175 | | |
171 | 176 | | |
172 | 177 | | |
173 | 178 | | |
174 | 179 | | |
| 180 | + | |
175 | 181 | | |
176 | 182 | | |
177 | 183 | | |
| |||
731 | 737 | | |
732 | 738 | | |
733 | 739 | | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
734 | 752 | | |
735 | 753 | | |
736 | 754 | | |
| |||
807 | 825 | | |
808 | 826 | | |
809 | 827 | | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
810 | 836 | | |
811 | 837 | | |
812 | 838 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
7 | 16 | | |
8 | 17 | | |
9 | 18 | | |
| |||
14 | 23 | | |
15 | 24 | | |
16 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
17 | 29 | | |
18 | 30 | | |
19 | 31 | | |
| |||
60 | 72 | | |
61 | 73 | | |
62 | 74 | | |
| 75 | + | |
63 | 76 | | |
64 | 77 | | |
65 | 78 | | |
| |||
100 | 113 | | |
101 | 114 | | |
102 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
103 | 128 | | |
104 | 129 | | |
105 | 130 | | |
| |||
0 commit comments