Resolved

YouTube scheduler silently ignores sync failures causing double scheduling

Python
YouTube API
Scheduling
Backend
Error Handling
API Integration
Debugging
Data Sync

YouTube Data API v3 quota exceeded errors caused scheduler to use empty schedule list | Resulted in videos scheduled 3 hours apart instead of 6 and overlapping time slots

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_times list 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:

  1. State Flag for Sync Validation: Added a _sync_required flag to enforce successful synchronization before scheduling.
  2. Boolean Return from sync_with_youtube(): Modified the function to return a boolean indicating success or failure.
  3. Failure Path Blocking: If synchronization fails and no cached schedule exists, block uploads to prevent incorrect scheduling.
  4. Cached Schedule Utilization: Use cached schedule data from the last successful sync if available.
  5. Integration with YouTube Uploader: Added logic to propagate synchronization errors to the upload system for retry handling.

Key Behavior Changes

ScenarioOld BehaviorNew Behavior
First sync failsSchedule from empty list, causing incorrect schedulingBlock uploads until synchronization succeeds
Sync fails after successSchedule from empty list, causing conflictsUse cached schedule from the last successful sync
Quota exceededSchedule from empty list, leading to conflictsUse last known good schedule or block uploads until quota is reset
Restart + quota exceededCatastrophic empty schedule, causing scheduling conflictsBlock 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.