Bypass sync window check on startup to fetch missing tracks immediately

- On startup, if no cache exists, fetch immediately regardless of sync window
- Regular background checks still respect sync window timing
- Ensures playlists are populated even if app restarts before sync time
This commit is contained in:
2026-02-01 11:22:12 -05:00
parent e860bbe0ee
commit c0c7668cc4

View File

@@ -78,10 +78,10 @@ public class SpotifyMissingTracksFetcher : BackgroundService
var shouldRunOnStartup = await ShouldRunOnStartupAsync(); var shouldRunOnStartup = await ShouldRunOnStartupAsync();
if (shouldRunOnStartup) if (shouldRunOnStartup)
{ {
_logger.LogInformation("Running initial fetch on startup"); _logger.LogInformation("Running initial fetch on startup (bypassing sync window check)");
try try
{ {
await FetchMissingTracksAsync(stoppingToken); await FetchMissingTracksAsync(stoppingToken, bypassSyncWindowCheck: true);
_hasRunOnce = true; _hasRunOnce = true;
} }
catch (Exception ex) catch (Exception ex)
@@ -91,7 +91,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
} }
else else
{ {
_logger.LogInformation("Skipping startup fetch - already ran within last 24 hours"); _logger.LogInformation("Skipping startup fetch - already have recent cache");
_hasRunOnce = true; _hasRunOnce = true;
} }
} }
@@ -213,7 +213,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
} }
} }
private async Task FetchMissingTracksAsync(CancellationToken cancellationToken) private async Task FetchMissingTracksAsync(CancellationToken cancellationToken, bool bypassSyncWindowCheck = false)
{ {
var settings = _spotifySettings.Value; var settings = _spotifySettings.Value;
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
@@ -222,13 +222,20 @@ public class SpotifyMissingTracksFetcher : BackgroundService
.AddMinutes(settings.SyncStartMinute); .AddMinutes(settings.SyncStartMinute);
var syncEnd = syncStart.AddHours(settings.SyncWindowHours); var syncEnd = syncStart.AddHours(settings.SyncWindowHours);
// Only run after the sync window has passed // Only run after the sync window has passed (unless bypassing for startup)
if (now < syncEnd) if (!bypassSyncWindowCheck && now < syncEnd)
{ {
return; return;
} }
if (bypassSyncWindowCheck)
{
_logger.LogInformation("Fetching missing tracks (startup mode, bypassing sync window check)...");
}
else
{
_logger.LogInformation("Sync window passed, searching last 24 hours for missing tracks..."); _logger.LogInformation("Sync window passed, searching last 24 hours for missing tracks...");
}
foreach (var kvp in _playlistIdToName) foreach (var kvp in _playlistIdToName)
{ {