Skip to content

Commit

Permalink
Fixed the issues where the DB was being disposed too early
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Nov 7, 2019
1 parent 47f323f commit 96e3e88
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 94 deletions.
51 changes: 24 additions & 27 deletions src/Ombi.Core/Engine/MovieRequestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ public async Task<RequestsViewModel<MovieRequests>> GetRequests(int count, int p
var requests = await (OrderMovies(allRequests, orderFilter.OrderType)).Skip(position).Take(count)
.ToListAsync();

requests.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, requests);
return new RequestsViewModel<MovieRequests>
{
Collection = requests,
Expand Down Expand Up @@ -251,26 +247,30 @@ public async Task<IEnumerable<MovieRequests>> GetRequests()
allRequests = await MovieRepository.GetWithUser().ToListAsync();
}

allRequests.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, allRequests);

return allRequests;
}

private async Task CheckForSubscription(HideResult shouldHide, MovieRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<MovieRequests> movieRequests)
{
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
var requestIds = movieRequests.Select(x => x.Id);
var sub = await _subscriptionRepository.GetAll().Where(s =>
s.UserId == shouldHide.UserId && requestIds.Contains(s.RequestId) && s.RequestType == RequestType.Movie)
.ToListAsync();
foreach (var x in movieRequests)
{
x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s =>
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.Movie);
x.Subscribed = sub != null;
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var hasSub = sub.FirstOrDefault(r => r.RequestId == x.Id);
x.Subscribed = hasSub != null;
}
}
}

Expand All @@ -293,11 +293,8 @@ public async Task<IEnumerable<MovieRequests>> SearchMovieRequest(string search)
}

var results = allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToList();
results.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, results);

return results;
}

Expand Down Expand Up @@ -493,7 +490,7 @@ await _requestLog.Add(new RequestLog
RequestType = RequestType.Movie,
});

return new RequestEngineResult {Result = true, Message = $"{movieName} has been successfully added!", RequestId = model.Id};
return new RequestEngineResult { Result = true, Message = $"{movieName} has been successfully added!", RequestId = model.Id };
}

public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)
Expand Down Expand Up @@ -533,7 +530,7 @@ public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)

return new RequestQuotaCountModel()
{
HasLimit = true,
HasLimit = true,
Limit = limit,
Remaining = count,
NextRequest = DateTime.SpecifyKind(oldestRequestedAt.AddDays(7), DateTimeKind.Utc),
Expand Down
65 changes: 36 additions & 29 deletions src/Ombi.Core/Engine/TvRequestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ public async Task<RequestsViewModel<TvRequests>> GetRequests(int count, int posi
.ThenInclude(x => x.Episodes)
.OrderByDescending(x => x.ChildRequests.Select(y => y.RequestedDate).FirstOrDefault())
.Skip(position).Take(count).ToListAsync();

}

allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
}
await CheckForSubscription(shouldHide, allRequests);
allRequests.ForEach(async r => { });

return new RequestsViewModel<TvRequests>
{
Expand Down Expand Up @@ -194,7 +194,8 @@ public async Task<RequestsViewModel<TvRequests>> GetRequestsLite(int count, int
{
return new RequestsViewModel<TvRequests>();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });

await CheckForSubscription(shouldHide, allRequests);

return new RequestsViewModel<TvRequests>
{
Expand All @@ -216,7 +217,7 @@ public async Task<IEnumerable<TvRequests>> GetRequests()
allRequests = await TvRepository.Get().ToListAsync();
}

allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}

Expand All @@ -236,7 +237,7 @@ public async Task<IEnumerable<TvRequests>> GetRequestsLite()
allRequests = await TvRepository.GetLite().ToListAsync();
}

allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}

Expand All @@ -255,7 +256,7 @@ public async Task<TvRequests> GetTvRequest(int requestId)
request = await TvRepository.Get().Where(x => x.Id == requestId).FirstOrDefaultAsync();
}

await CheckForSubscription(shouldHide, request);
await CheckForSubscription(shouldHide, new List<TvRequests>{request});
return request;
}

Expand Down Expand Up @@ -304,7 +305,7 @@ public async Task<IEnumerable<ChildRequests>> GetAllChldren(int tvId)
allRequests = await TvRepository.GetChild().Include(x => x.SeasonRequests).Where(x => x.ParentRequestId == tvId).ToListAsync();
}

allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);

return allRequests;
}
Expand All @@ -323,7 +324,7 @@ public async Task<IEnumerable<TvRequests>> SearchTvRequest(string search)
}
var results = await allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToListAsync();

results.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, results);
return results;
}

Expand Down Expand Up @@ -420,7 +421,7 @@ public async Task<RequestEngineResult> DenyChildRequest(int requestId, string re

public async Task<ChildRequests> UpdateChildRequest(ChildRequests request)
{
await TvRepository.UpdateChild(request);
await TvRepository.UpdateChild(request);
return request;
}

Expand All @@ -438,14 +439,14 @@ public async Task RemoveTvChild(int requestId)
// Delete the parent
TvRepository.Db.TvRequests.Remove(parent);
}

await TvRepository.Db.SaveChangesAsync();
}

public async Task RemoveTvRequest(int requestId)
{
var request = await TvRepository.Get().FirstOrDefaultAsync(x => x.Id == requestId);
await TvRepository.Delete(request);
await TvRepository.Delete(request);
}

public async Task<bool> UserHasRequest(string userId)
Expand Down Expand Up @@ -520,26 +521,32 @@ public async Task<int> GetTotal()
}
}

private async Task CheckForSubscription(HideResult shouldHide, TvRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<TvRequests> x)
{
foreach (var tv in x.ChildRequests)
foreach (var tvRequest in x)
{
await CheckForSubscription(shouldHide, tv);
await CheckForSubscription(shouldHide, tvRequest.ChildRequests);
}
}

private async Task CheckForSubscription(HideResult shouldHide, ChildRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<ChildRequests> childRequests)
{
if (shouldHide.UserId == x.RequestedUserId)
var sub = _subscriptionRepository.GetAll();
var childIds = childRequests.Select(x => x.Id);
var relevantSubs = await sub.Where(s =>
s.UserId == shouldHide.UserId && childIds.Contains(s.Id) && s.RequestType == RequestType.TvShow).ToListAsync();
foreach (var x in childRequests)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s =>
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.TvShow);
x.Subscribed = sub != null;
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var result = relevantSubs.FirstOrDefault(s => s.RequestId == x.Id);
x.Subscribed = result != null;
}
}
}

Expand All @@ -560,7 +567,7 @@ private async Task<RequestEngineResult> AddRequest(TvRequests model)
return await AfterRequest(model.ChildRequests.FirstOrDefault());
}

private static List<ChildRequests> SortEpisodes(List<ChildRequests> items)
private static List<ChildRequests> SortEpisodes(List<ChildRequests> items)
{
foreach (var value in items)
{
Expand Down Expand Up @@ -597,7 +604,7 @@ await _requestLog.Add(new RequestLog
var result = await TvSender.Send(model);
if (result.Success)
{
return new RequestEngineResult { Result = true, RequestId = model.Id};
return new RequestEngineResult { Result = true, RequestId = model.Id };
}
return new RequestEngineResult
{
Expand Down Expand Up @@ -650,10 +657,10 @@ public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)
DateTime oldestRequestedAt = await log.OrderBy(x => x.RequestDate)
.Select(x => x.RequestDate)
.FirstOrDefaultAsync();

return new RequestQuotaCountModel()
{
HasLimit = true,
HasLimit = true,
Limit = limit,
Remaining = count,
NextRequest = DateTime.SpecifyKind(oldestRequestedAt.AddDays(7), DateTimeKind.Utc),
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Emby/EmbyAvaliabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_movieRepo?.Dispose();
}
_disposed = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
_settings?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
_settings?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Ombi/Interfaces/IssuesPurge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_issuesRepository?.Dispose();
_settings?.Dispose();
}
_disposed = true;
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Ombi/MediaDatabaseRefresh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_plexRepo?.Dispose();
_settings?.Dispose();
}
_disposed = true;
Expand Down
7 changes: 2 additions & 5 deletions src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, I

private readonly IPlexContentRepository _plex;
private readonly IEmbyContentRepository _emby;
private readonly IExternalRepository<RecentlyAddedLog> _recentlyAddedLog;
private readonly IRepository<RecentlyAddedLog> _recentlyAddedLog;
private readonly IMovieDbApi _movieApi;
private readonly ITvMazeApi _tvApi;
private readonly IEmailProvider _email;
Expand All @@ -78,7 +78,7 @@ public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, I
private readonly UserManager<OmbiUser> _userManager;
private readonly ILogger _log;
private readonly ILidarrApi _lidarrApi;
private readonly IRepository<LidarrAlbumCache> _lidarrAlbumRepository;
private readonly IExternalRepository<LidarrAlbumCache> _lidarrAlbumRepository;
private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ISettingsService<PlexSettings> _plexSettings;
private readonly ISettingsService<EmbySettings> _embySettings;
Expand Down Expand Up @@ -931,12 +931,9 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_plex?.Dispose();
_emby?.Dispose();
_newsletterSettings?.Dispose();
_customizationSettings?.Dispose();
_emailSettings.Dispose();
_recentlyAddedLog.Dispose();
_templateRepo?.Dispose();
_userManager?.Dispose();
}
Expand Down
2 changes: 0 additions & 2 deletions src/Ombi.Schedule/Jobs/Ombi/RefreshMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_plexRepo?.Dispose();
_embyRepo?.Dispose();
_plexSettings?.Dispose();
}
_disposed = true;
Expand Down
2 changes: 0 additions & 2 deletions src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_movieRepo?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,6 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
Plex?.Dispose();
Repo?.Dispose();
EpisodeSync?.Dispose();
}
_disposed = true;
Expand Down
1 change: 0 additions & 1 deletion src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
_repo?.Dispose();
_settings?.Dispose();
}
_disposed = true;
Expand Down
5 changes: 5 additions & 0 deletions src/Ombi.Store/Context/MySql/OmbiMySqlContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public OmbiMySqlContext(DbContextOptions<OmbiMySqlContext> options) : base(optio

Database.Migrate();
}

public override void Dispose()
{
base.Dispose();
}
}
}
Loading

0 comments on commit 96e3e88

Please sign in to comment.