Overview
The SharedServices grading system provides automated bet grading for both Player Bot and Team Bot. It uses FotMob API for fixture data, supports adaptive scheduling with rate limit backoff, and runs grading and syncing operations in parallel threads.Architecture
Unified Grading Scheduler
TheUnifiedGradingScheduler is the core component that orchestrates automated grading and syncing.
Features
- Parallel Operation: Runs grading and syncing in separate threads
- Adaptive Intervals: Adjusts polling frequency based on API responses
- Rate Limit Handling: Automatic backoff on 429 errors
- Timestamp Rotation: Ensures all alerts are checked over time
- Batch Size Adaptation: Optimizes sync batch sizes
Initialization
SharedServices/grading/scheduler/unified_scheduler.py
Adaptive Scheduling
The scheduler adjusts intervals based on success/failure:- ✅ On success: After 3 consecutive successes, interval is halved (min: 60s)
- ❌ On rate limit: Interval is doubled (max: 3600s)
Parallel Threads
The scheduler runs two independent threads:Thread 1: Grading Logic
SharedServices/grading/scheduler/unified_scheduler.py
Thread 2: Syncing Logic
SharedServices/grading/scheduler/unified_scheduler.py
- Alerts are fetched by oldest
last_sync_checktimestamp - After syncing, timestamp is updated
- Next sync cycle processes the next batch of oldest alerts
Status Monitoring
Immediate Bet Grader
TheImmediateBetGrader grades bets for fixtures that have already finished. Used when users track bets after the game ends.
Usage
SharedServices/grading/processors/immediate_bet_grader.py
Grading Logic
Result Statuses
Player Stats Extraction
SharedServices/grading/processors/immediate_bet_grader.py
Refund Scenarios
Result Processor
TheUnifiedResultProcessor processes all pending alerts and updates their grading status.
Features
- Batch Processing: Processes alerts in batches
- Rate Limit Handling: Exponential backoff on 429 errors
- Result Tracking: Updates
result_trackingfield in MongoDB - Bot Type Support: Works for both Player Bot and Team Bot
Example Usage
Rate Limiting
Exponential Backoff
Batch Size Adaptation
Google Sheets Integration
Graded alerts are automatically synced to Google Sheets for tracking.Factory Functions
Running Multiple Bots
Best Practices
Use Adaptive Scheduling
Use Adaptive Scheduling
Let the scheduler adjust intervals automatically. Don’t force fixed intervals.
Monitor Rate Limits
Monitor Rate Limits
Watch for
RateLimitException and ensure backoff is working correctly.Handle Player Non-Starters
Handle Player Non-Starters
Always check if player was in the squad before grading player bets.
Use Timestamp Rotation
Use Timestamp Rotation
The rotation ensures all alerts are eventually checked without missing any.
Next Steps
Market Mapping
Learn how markets are mapped to API stat types
FotMob API
Understand the FotMob service integration