|
| 1 | +# File Completion Tracking |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + |
| 5 | +**Intelligent file completion tracking and stalled detection management** |
| 6 | + |
| 7 | +Last Updated: January 2025 |
| 8 | + |
| 9 | +[🏠 Back to Docs](README.md) | [🔄 Consumer Cancellation](consumer-cancellation.md) |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +The file completion tracking system ensures accurate monitoring of file processing status across the Discogsography platform. It prevents false warnings about stalled extractors and coordinates with the consumer cancellation feature for optimal resource management. |
| 16 | + |
| 17 | +## How It Works |
| 18 | + |
| 19 | +### 1. File Processing Lifecycle |
| 20 | + |
| 21 | +```mermaid |
| 22 | +graph LR |
| 23 | + A[File Processing Starts] --> B[Records Extracted] |
| 24 | + B --> C[Messages Sent to RabbitMQ] |
| 25 | + C --> D[File Complete Message Sent] |
| 26 | + D --> E[File Marked as Complete] |
| 27 | + E --> F[Consumer Cancellation Scheduled] |
| 28 | + F --> G[Stalled Detection Skips File] |
| 29 | +
|
| 30 | + style D fill:#f9f,stroke:#333,stroke-width:4px |
| 31 | + style E fill:#9f9,stroke:#333,stroke-width:4px |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Completion Tracking |
| 35 | + |
| 36 | +When a file finishes processing: |
| 37 | + |
| 38 | +1. **Extractor** sends a `file_complete` message with: |
| 39 | + |
| 40 | + - `type`: "file_complete" |
| 41 | + - `data_type`: The type of data (artists, labels, masters, releases) |
| 42 | + - `timestamp`: Completion time |
| 43 | + - `total_processed`: Number of records processed |
| 44 | + - `file`: Original filename |
| 45 | + |
| 46 | +1. **Extractor** adds the data type to `completed_files` set |
| 47 | + |
| 48 | +1. **Consumers** (graphinator/tableinator) receive the message and: |
| 49 | + |
| 50 | + - Mark the file as complete (🎉 in logs) |
| 51 | + - Schedule consumer cancellation after grace period |
| 52 | + |
| 53 | +### 3. Stalled Detection |
| 54 | + |
| 55 | +The extractor's progress monitoring: |
| 56 | + |
| 57 | +- Checks for files with no activity for >2 minutes |
| 58 | +- **Excludes** files in the `completed_files` set |
| 59 | +- Only reports actual stalls, not completed files |
| 60 | + |
| 61 | +## Implementation Details |
| 62 | + |
| 63 | +### Extractor Changes |
| 64 | + |
| 65 | +```python |
| 66 | +# Global tracking variables |
| 67 | +completed_files = set() # Track which files have been completed |
| 68 | + |
| 69 | +# When sending file completion message |
| 70 | +completed_files.add(self.data_type) |
| 71 | + |
| 72 | +# During stalled detection |
| 73 | +for data_type, last_time in last_extraction_time.items(): |
| 74 | + # Skip if this file type has been completed |
| 75 | + if data_type in completed_files: |
| 76 | + continue |
| 77 | + # ... stalled detection logic |
| 78 | +``` |
| 79 | + |
| 80 | +### Progress Reporting |
| 81 | + |
| 82 | +Enhanced progress reports show: |
| 83 | + |
| 84 | +``` |
| 85 | +📊 Extraction Progress: 50000 total records extracted |
| 86 | +(Artists: 20000, Labels: 15000, Masters: 10000, Releases: 5000) |
| 87 | +✅ Completed file types: ['artists', 'labels'] |
| 88 | +✅ Active extractors: ['masters', 'releases'] |
| 89 | +``` |
| 90 | + |
| 91 | +## Benefits |
| 92 | + |
| 93 | +1. **Accurate Monitoring**: No false warnings about completed files |
| 94 | +1. **Clear Status**: Easy to see which files are done vs. active |
| 95 | +1. **Resource Optimization**: Works with consumer cancellation for cleanup |
| 96 | +1. **Better Debugging**: Clear indication of actual vs. false stalls |
| 97 | + |
| 98 | +## Configuration |
| 99 | + |
| 100 | +No additional configuration needed - the feature works automatically with existing settings. |
| 101 | + |
| 102 | +### Related Environment Variables |
| 103 | + |
| 104 | +- `CONSUMER_CANCEL_DELAY`: Grace period before canceling consumers (default: 300s) |
| 105 | +- `FORCE_REPROCESS`: Set to "true" to reprocess all files |
| 106 | + |
| 107 | +## Monitoring |
| 108 | + |
| 109 | +### Log Messages to Watch |
| 110 | + |
| 111 | +**Extractor**: |
| 112 | + |
| 113 | +- `✅ Sent file completion message for {type}` - File marked complete |
| 114 | +- `✅ Completed file types: [...]` - Shows all completed files |
| 115 | +- `⚠️ Stalled extractors detected: [...]` - Only shows actual stalls |
| 116 | + |
| 117 | +**Consumers**: |
| 118 | + |
| 119 | +- `🎉 File processing complete for {type}!` - Completion received |
| 120 | +- `🔌 Canceling consumer for {type}` - Cancellation scheduled |
| 121 | + |
| 122 | +## Troubleshooting |
| 123 | + |
| 124 | +### Issue: Still seeing stalled warnings for completed files |
| 125 | + |
| 126 | +**Cause**: Service was restarted and lost completion state |
| 127 | + |
| 128 | +**Solution**: The `completed_files` set is reset on restart. This is expected behavior - the warnings will stop once files complete in the new session. |
| 129 | + |
| 130 | +### Issue: Consumer not being canceled after completion |
| 131 | + |
| 132 | +**Check**: |
| 133 | + |
| 134 | +1. Verify `CONSUMER_CANCEL_DELAY` is not 0 |
| 135 | +1. Check logs for cancellation messages |
| 136 | +1. Ensure RabbitMQ connection is stable |
| 137 | + |
| 138 | +## Testing |
| 139 | + |
| 140 | +Test the feature: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Start services |
| 144 | +docker-compose up -d |
| 145 | + |
| 146 | +# Watch logs for completion tracking |
| 147 | +docker-compose logs -f extractor | grep -E "(Completed file types|Stalled extractors)" |
| 148 | + |
| 149 | +# Force a quick test with small files |
| 150 | +# Files will complete quickly and should not show as stalled |
| 151 | +``` |
| 152 | + |
| 153 | +## Technical Architecture |
| 154 | + |
| 155 | +### State Management |
| 156 | + |
| 157 | +- `extraction_progress`: Tracks record counts per type |
| 158 | +- `last_extraction_time`: Tracks last activity time per type |
| 159 | +- `completed_files`: Set of completed data types |
| 160 | +- State is reset when processing new files |
| 161 | + |
| 162 | +### Integration Points |
| 163 | + |
| 164 | +1. **Extractor → RabbitMQ**: Sends completion message |
| 165 | +1. **Extractor Internal**: Updates completion tracking |
| 166 | +1. **Consumers → RabbitMQ**: Cancel queue consumers |
| 167 | +1. **Progress Reporter**: Excludes completed files |
| 168 | + |
| 169 | +## Future Enhancements |
| 170 | + |
| 171 | +- [ ] Persist completion state across restarts |
| 172 | +- [ ] Add completion timestamps to progress reports |
| 173 | +- [ ] Create completion metrics for monitoring |
| 174 | +- [ ] Add file-level (not just type-level) tracking |
0 commit comments