Skip to content

Commit

Permalink
Fixed the issue where MySql was sometimes throwing errors about not b…
Browse files Browse the repository at this point in the history
…eing able to execute the retry strat
  • Loading branch information
tidusjar committed Jan 29, 2021
1 parent f50536c commit 530e55a
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 112 deletions.
29 changes: 18 additions & 11 deletions src/Ombi.Schedule/Jobs/Couchpotato/CouchPotatoSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
var movies = await _api.GetMovies(settings.FullUri, settings.ApiKey, new[] {"active"});
if (movies != null)
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM CouchPotatoCache");
tran.Commit();
}
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM CouchPotatoCache");
tran.Commit();
}
});

// Save
var movieIds = new List<CouchPotatoCache>();
Expand All @@ -102,14 +106,17 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
_log.LogError("TMDBId is not > 0 for movie {0}", m.title);
}
}

using (var tran = await _ctx.Database.BeginTransactionAsync())
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.CouchPotatoCache.AddRangeAsync(movieIds);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.CouchPotatoCache.AddRangeAsync(movieIds);

await _ctx.SaveChangesAsync();
tran.Commit();
}
await _ctx.SaveChangesAsync();
tran.Commit();
}
});

await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
.SendAsync(NotificationHub.NotificationEvent, "Couch Potato Sync Finished");
Expand Down
29 changes: 18 additions & 11 deletions src/Ombi.Schedule/Jobs/Lidarr/LidarrAlbumSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
var albums = await _lidarrApi.GetAllAlbums(settings.ApiKey, settings.FullUri);
if (albums != null && albums.Any())
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM LidarrAlbumCache");
tran.Commit();
}
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM LidarrAlbumCache");
tran.Commit();
}
});

var albumCache = new List<LidarrAlbumCache>();
foreach (var a in albums)
Expand All @@ -76,14 +80,17 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
});
}
}

using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.LidarrAlbumCache.AddRangeAsync(albumCache);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.LidarrAlbumCache.AddRangeAsync(albumCache);

await _ctx.SaveChangesAsync();
tran.Commit();
}
await _ctx.SaveChangesAsync();
tran.Commit();
}
});
}
}
catch (System.Exception ex)
Expand Down
29 changes: 18 additions & 11 deletions src/Ombi.Schedule/Jobs/Lidarr/LidarrArtistSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
var artists = await _lidarrApi.GetArtists(settings.ApiKey, settings.FullUri);
if (artists != null && artists.Any())
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM LidarrArtistCache");
tran.Commit();
}
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM LidarrArtistCache");
tran.Commit();
}
});

var artistCache = new List<LidarrArtistCache>();
foreach (var a in artists)
Expand All @@ -71,14 +75,17 @@ await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
});
}
}

using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.LidarrArtistCache.AddRangeAsync(artistCache);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.LidarrArtistCache.AddRangeAsync(artistCache);

await _ctx.SaveChangesAsync();
tran.Commit();
}
await _ctx.SaveChangesAsync();
tran.Commit();
}
});
}
}
catch (Exception ex)
Expand Down
29 changes: 18 additions & 11 deletions src/Ombi.Schedule/Jobs/Radarr/RadarrSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public async Task Execute(IJobExecutionContext job)
var movies = await RadarrApi.GetMovies(settings.ApiKey, settings.FullUri);
if (movies != null)
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM RadarrCache");
tran.Commit();
}
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM RadarrCache");
tran.Commit();
}
});

var movieIds = new List<RadarrCache>();
foreach (var m in movies)
Expand All @@ -72,14 +76,17 @@ public async Task Execute(IJobExecutionContext job)
}
}
}

using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.RadarrCache.AddRangeAsync(movieIds);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.RadarrCache.AddRangeAsync(movieIds);

await _ctx.SaveChangesAsync();
tran.Commit();
}
await _ctx.SaveChangesAsync();
tran.Commit();
}
});
}

await OmbiQuartz.TriggerJob(nameof(IArrAvailabilityChecker), "DVR");
Expand Down
29 changes: 18 additions & 11 deletions src/Ombi.Schedule/Jobs/SickRage/SickRageSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ public async Task Execute(IJobExecutionContext job)
if (shows != null)
{
var srShows = shows.data.Values;
var ids = srShows.Select(x => x.tvdbid);
using (var tran = await _ctx.Database.BeginTransactionAsync())
var ids = srShows.Select(x => x.tvdbid);
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SickRageCache");
tran.Commit();
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SickRageCache");
tran.Commit();
}
});

var entites = ids.Select(id => new SickRageCache { TvDbId = id }).ToList();

Expand All @@ -77,13 +81,16 @@ public async Task Execute(IJobExecutionContext job)
}

}

using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.SickRageEpisodeCache.AddRangeAsync(episodesToAdd);
await _ctx.SaveChangesAsync();
tran.Commit();
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SickRageEpisodeCache.AddRangeAsync(episodesToAdd);
await _ctx.SaveChangesAsync();
tran.Commit();
}
});
}
}
catch (Exception e)
Expand Down
43 changes: 27 additions & 16 deletions src/Ombi.Schedule/Jobs/Sonarr/SonarrSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ public async Task Execute(IJobExecutionContext job)
if (series != null)
{
var sonarrSeries = series as ImmutableHashSet<SonarrSeries> ?? series.ToImmutableHashSet();
var ids = sonarrSeries.Select(x => x.tvdbId);
using (var tran = await _ctx.Database.BeginTransactionAsync())
var ids = sonarrSeries.Select(x => x.tvdbId);
var strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrCache");
tran.Commit();
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrCache");
tran.Commit();
}
});

var existingSeries = await _ctx.SonarrCache.Select(x => x.TvDbId).ToListAsync();

Expand All @@ -62,11 +66,15 @@ public async Task Execute(IJobExecutionContext job)

await _ctx.SonarrCache.AddRangeAsync(entites);
entites.Clear();
using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache");
tran.Commit();
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache");
tran.Commit();
}
});

foreach (var s in sonarrSeries)
{
Expand Down Expand Up @@ -116,14 +124,17 @@ public async Task Execute(IJobExecutionContext job)
// }

//}

using (var tran = await _ctx.Database.BeginTransactionAsync())
strat = _ctx.Database.CreateExecutionStrategy();
await strat.ExecuteAsync(async () =>
{
await _ctx.SonarrEpisodeCache.AddRangeAsync(episodesToAdd);
_log.LogDebug("Commiting the transaction");
await _ctx.SaveChangesAsync();
tran.Commit();
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SonarrEpisodeCache.AddRangeAsync(episodesToAdd);
_log.LogDebug("Commiting the transaction");
await _ctx.SaveChangesAsync();
tran.Commit();
}
});
}

}
Expand Down
20 changes: 8 additions & 12 deletions src/Ombi.Store/Repository/AuditRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ public async Task Record(AuditType type, AuditArea area, string description)

public async Task Record(AuditType type, AuditArea area, string description, string user)
{
using (var tran = await Ctx.Database.BeginTransactionAsync())
await Ctx.Audit.AddAsync(new Audit
{
await Ctx.Audit.AddAsync(new Audit
{
User = user,
AuditArea = area,
AuditType = type,
DateTime = DateTime.UtcNow,
Description = description
});
User = user,
AuditArea = area,
AuditType = type,
DateTime = DateTime.UtcNow,
Description = description
});

await Ctx.SaveChangesAsync();
tran.Commit();
}
await Ctx.SaveChangesAsync();
}
}
}
Loading

0 comments on commit 530e55a

Please sign in to comment.