Issue
The YouTube Data API-based scheduler encountered synchronization failures, leading to incorrect scheduling behavior. When API sync operations failed (e.g., due to quota exceeded or network errors), the scheduler continued with an empty _scheduled_times list, causing:
- Videos scheduled 3 hours apart instead of 6 hours, leading to double scheduling.
- Overlapping time slots due to new videos being scheduled without regard for existing YouTube schedule.
- Incorrect series interleaving, where parts from different series appeared out of order.
Culprit
The root issue was in the exception handling logic of the scheduler.py module. When synchronization failed, the scheduler silently continued execution with an empty schedule list, assuming no videos were scheduled:
- The
_scheduled_timeslist was only populated during successful synchronization. - On failure, the list remained empty, and the scheduler treated it as a valid empty state.
- The
get_next_available_slot()function returned an incorrect time slot (now + min_days_advance), causing scheduling conflicts.
Solution
Fail-Safe Synchronization Mechanism
Implemented a robust synchronization mechanism with the following improvements:
- State Flag for Sync Validation: Added a
_sync_requiredflag to enforce successful synchronization before scheduling. - Boolean Return from
sync_with_youtube(): Modified the function to return a boolean indicating success or failure. - Failure Path Blocking: If synchronization fails and no cached schedule exists, block uploads to prevent incorrect scheduling.
- Cached Schedule Utilization: Use cached schedule data from the last successful sync if available.
- Integration with YouTube Uploader: Added logic to propagate synchronization errors to the upload system for retry handling.
Key Behavior Changes
| Scenario | Old Behavior | New Behavior |
|---|---|---|
| First sync fails | Schedule from empty list, causing incorrect scheduling | Block uploads until synchronization succeeds |
| Sync fails after success | Schedule from empty list, causing conflicts | Use cached schedule from the last successful sync |
| Quota exceeded | Schedule from empty list, leading to conflicts | Use last known good schedule or block uploads until quota is reset |
| Restart + quota exceeded | Catastrophic empty schedule, causing scheduling conflicts | Block uploads until synchronization resolves |
Result
The solution ensures:
- Scheduling integrity: Prevents incorrect scheduling by blocking uploads when no valid schedule exists.
- Graceful degradation: Uses cached schedule data when available.
- Error propagation: Clearly communicates synchronization failures to the upload system for appropriate handling.
- Robustness: Maintains schedule integrity even during API failures or quota issues.