Skip to content

Commit 89b8c52

Browse files
author
Jamie
committed
fix: SonarrSync now deletes episodes per-series to prevent incomplete cache
Fixes #5306 - Sonarr items are not showing as available Root cause: SonarrSync was deleting ALL episodes from SonarrEpisodeCache at the start of the job, then re-adding them one series at a time. If the job failed partway through (API timeout, network issue, etc.), the cache would be left incomplete with only some series' episodes. This caused shows to be incorrectly marked as unavailable. Changes: - Moved DELETE FROM SonarrEpisodeCache inside the series loop - Now deletes episodes per series (by TvDbId) before adding new ones - If job fails mid-way, cache for already-processed series remains intact This ensures that partial failures don't result in completely empty cache.
1 parent bb6300f commit 89b8c52

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ await strat.ExecuteAsync(async () =>
9292
await _ctx.SonarrCache.AddRangeAsync(sonarrCacheToSave);
9393
await _ctx.SaveChangesAsync();
9494
sonarrCacheToSave.Clear();
95-
strat = _ctx.Database.CreateExecutionStrategy();
96-
await strat.ExecuteAsync(async () =>
97-
{
98-
using var tran = await _ctx.Database.BeginTransactionAsync();
99-
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache");
100-
await tran.CommitAsync();
101-
});
10295

10396
foreach (var s in ids)
10497
{
@@ -111,6 +104,15 @@ await strat.ExecuteAsync(async () =>
111104
var episodes = await _api.GetEpisodes(s.Id, settings.ApiKey, settings.FullUri);
112105
var monitoredEpisodes = episodes.Where(x => x.monitored || x.hasFile);
113106

107+
// Delete existing episodes for this series before adding new ones
108+
strat = _ctx.Database.CreateExecutionStrategy();
109+
await strat.ExecuteAsync(async () =>
110+
{
111+
using var tran = await _ctx.Database.BeginTransactionAsync();
112+
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache WHERE TvDbId = {0}", s.TvDbId);
113+
await tran.CommitAsync();
114+
});
115+
114116
//var allExistingEpisodes = await _ctx.SonarrEpisodeCache.Where(x => x.TvDbId == s.tvdbId).ToListAsync();
115117
// Add to DB
116118
_log.LogDebug("We have the episodes, adding to db transaction");

0 commit comments

Comments
 (0)