Fixed the issues where the DB was being disposed too early

This commit is contained in:
tidusjar 2019-11-07 22:22:53 +00:00
parent 47f323fcdd
commit 96e3e88261
16 changed files with 86 additions and 91 deletions

View file

@ -188,11 +188,7 @@ namespace Ombi.Core.Engine
var requests = await (OrderMovies(allRequests, orderFilter.OrderType)).Skip(position).Take(count) var requests = await (OrderMovies(allRequests, orderFilter.OrderType)).Skip(position).Take(count)
.ToListAsync(); .ToListAsync();
requests.ForEach(async x => await CheckForSubscription(shouldHide, requests);
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
return new RequestsViewModel<MovieRequests> return new RequestsViewModel<MovieRequests>
{ {
Collection = requests, Collection = requests,
@ -251,16 +247,20 @@ namespace Ombi.Core.Engine
allRequests = await MovieRepository.GetWithUser().ToListAsync(); allRequests = await MovieRepository.GetWithUser().ToListAsync();
} }
allRequests.ForEach(async x => await CheckForSubscription(shouldHide, allRequests);
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
return allRequests; return allRequests;
} }
private async Task CheckForSubscription(HideResult shouldHide, MovieRequests x) private async Task CheckForSubscription(HideResult shouldHide, List<MovieRequests> movieRequests)
{ {
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.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
if (shouldHide.UserId == x.RequestedUserId) if (shouldHide.UserId == x.RequestedUserId)
{ {
x.ShowSubscribe = false; x.ShowSubscribe = false;
@ -268,9 +268,9 @@ namespace Ombi.Core.Engine
else else
{ {
x.ShowSubscribe = true; x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s => var hasSub = sub.FirstOrDefault(r => r.RequestId == x.Id);
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.Movie); x.Subscribed = hasSub != null;
x.Subscribed = sub != null; }
} }
} }
@ -293,11 +293,8 @@ namespace Ombi.Core.Engine
} }
var results = allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToList(); var results = allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToList();
results.ForEach(async x => await CheckForSubscription(shouldHide, results);
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
return results; return results;
} }
@ -493,7 +490,7 @@ namespace Ombi.Core.Engine
RequestType = RequestType.Movie, 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) public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)

View file

@ -158,8 +158,8 @@ namespace Ombi.Core.Engine
.Skip(position).Take(count).ToListAsync(); .Skip(position).Take(count).ToListAsync();
} }
await CheckForSubscription(shouldHide, allRequests);
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); }); allRequests.ForEach(async r => { });
return new RequestsViewModel<TvRequests> return new RequestsViewModel<TvRequests>
{ {
@ -194,7 +194,8 @@ namespace Ombi.Core.Engine
{ {
return new RequestsViewModel<TvRequests>(); return new RequestsViewModel<TvRequests>();
} }
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return new RequestsViewModel<TvRequests> return new RequestsViewModel<TvRequests>
{ {
@ -216,7 +217,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.Get().ToListAsync(); allRequests = await TvRepository.Get().ToListAsync();
} }
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); }); await CheckForSubscription(shouldHide, allRequests);
return allRequests; return allRequests;
} }
@ -236,7 +237,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.GetLite().ToListAsync(); allRequests = await TvRepository.GetLite().ToListAsync();
} }
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); }); await CheckForSubscription(shouldHide, allRequests);
return allRequests; return allRequests;
} }
@ -255,7 +256,7 @@ namespace Ombi.Core.Engine
request = await TvRepository.Get().Where(x => x.Id == requestId).FirstOrDefaultAsync(); request = await TvRepository.Get().Where(x => x.Id == requestId).FirstOrDefaultAsync();
} }
await CheckForSubscription(shouldHide, request); await CheckForSubscription(shouldHide, new List<TvRequests>{request});
return request; return request;
} }
@ -304,7 +305,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.GetChild().Include(x => x.SeasonRequests).Where(x => x.ParentRequestId == tvId).ToListAsync(); 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; return allRequests;
} }
@ -323,7 +324,7 @@ namespace Ombi.Core.Engine
} }
var results = await allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToListAsync(); 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; return results;
} }
@ -520,15 +521,21 @@ namespace Ombi.Core.Engine
} }
} }
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)
{
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)
{ {
if (shouldHide.UserId == x.RequestedUserId) if (shouldHide.UserId == x.RequestedUserId)
{ {
@ -537,9 +544,9 @@ namespace Ombi.Core.Engine
else else
{ {
x.ShowSubscribe = true; x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s => var result = relevantSubs.FirstOrDefault(s => s.RequestId == x.Id);
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.TvShow); x.Subscribed = result != null;
x.Subscribed = sub != null; }
} }
} }
@ -597,7 +604,7 @@ namespace Ombi.Core.Engine
var result = await TvSender.Send(model); var result = await TvSender.Send(model);
if (result.Success) if (result.Success)
{ {
return new RequestEngineResult { Result = true, RequestId = model.Id}; return new RequestEngineResult { Result = true, RequestId = model.Id };
} }
return new RequestEngineResult return new RequestEngineResult
{ {

View file

@ -213,7 +213,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing) if (disposing)
{ {
_movieRepo?.Dispose();
} }
_disposed = true; _disposed = true;
} }

View file

@ -206,7 +206,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing) if (disposing)
{ {
_settings?.Dispose(); _settings?.Dispose();
_repo?.Dispose();
} }
_disposed = true; _disposed = true;
} }

View file

@ -156,7 +156,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing) if (disposing)
{ {
_settings?.Dispose(); _settings?.Dispose();
_repo?.Dispose();
} }
_disposed = true; _disposed = true;
} }

View file

@ -49,7 +49,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing) if (disposing)
{ {
_issuesRepository?.Dispose();
_settings?.Dispose(); _settings?.Dispose();
} }
_disposed = true; _disposed = true;

View file

@ -99,7 +99,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing) if (disposing)
{ {
_plexRepo?.Dispose();
_settings?.Dispose(); _settings?.Dispose();
} }
_disposed = true; _disposed = true;

View file

@ -66,7 +66,7 @@ namespace Ombi.Schedule.Jobs.Ombi
private readonly IPlexContentRepository _plex; private readonly IPlexContentRepository _plex;
private readonly IEmbyContentRepository _emby; private readonly IEmbyContentRepository _emby;
private readonly IExternalRepository<RecentlyAddedLog> _recentlyAddedLog; private readonly IRepository<RecentlyAddedLog> _recentlyAddedLog;
private readonly IMovieDbApi _movieApi; private readonly IMovieDbApi _movieApi;
private readonly ITvMazeApi _tvApi; private readonly ITvMazeApi _tvApi;
private readonly IEmailProvider _email; private readonly IEmailProvider _email;
@ -78,7 +78,7 @@ namespace Ombi.Schedule.Jobs.Ombi
private readonly UserManager<OmbiUser> _userManager; private readonly UserManager<OmbiUser> _userManager;
private readonly ILogger _log; private readonly ILogger _log;
private readonly ILidarrApi _lidarrApi; private readonly ILidarrApi _lidarrApi;
private readonly IRepository<LidarrAlbumCache> _lidarrAlbumRepository; private readonly IExternalRepository<LidarrAlbumCache> _lidarrAlbumRepository;
private readonly ISettingsService<LidarrSettings> _lidarrSettings; private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ISettingsService<PlexSettings> _plexSettings; private readonly ISettingsService<PlexSettings> _plexSettings;
private readonly ISettingsService<EmbySettings> _embySettings; private readonly ISettingsService<EmbySettings> _embySettings;
@ -931,12 +931,9 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing) if (disposing)
{ {
_plex?.Dispose();
_emby?.Dispose();
_newsletterSettings?.Dispose(); _newsletterSettings?.Dispose();
_customizationSettings?.Dispose(); _customizationSettings?.Dispose();
_emailSettings.Dispose(); _emailSettings.Dispose();
_recentlyAddedLog.Dispose();
_templateRepo?.Dispose(); _templateRepo?.Dispose();
_userManager?.Dispose(); _userManager?.Dispose();
} }

View file

@ -352,8 +352,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing) if (disposing)
{ {
_plexRepo?.Dispose();
_embyRepo?.Dispose();
_plexSettings?.Dispose(); _plexSettings?.Dispose();
} }
_disposed = true; _disposed = true;

View file

@ -193,8 +193,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing) if (disposing)
{ {
_movieRepo?.Dispose();
_repo?.Dispose();
} }
_disposed = true; _disposed = true;
} }

View file

@ -650,7 +650,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing) if (disposing)
{ {
Plex?.Dispose(); Plex?.Dispose();
Repo?.Dispose();
EpisodeSync?.Dispose(); EpisodeSync?.Dispose();
} }
_disposed = true; _disposed = true;

View file

@ -206,7 +206,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing) if (disposing)
{ {
_repo?.Dispose();
_settings?.Dispose(); _settings?.Dispose();
} }
_disposed = true; _disposed = true;

View file

@ -12,5 +12,10 @@ namespace Ombi.Store.Context.MySql
Database.Migrate(); Database.Migrate();
} }
public override void Dispose()
{
base.Dispose();
}
} }
} }

View file

@ -107,25 +107,25 @@ namespace Ombi.Store.Repository
} }
private bool _disposed; //private bool _disposed;
// Protected implementation of Dispose pattern. //// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing) //protected virtual void Dispose(bool disposing)
{ //{
if (_disposed) // if (_disposed)
return; // return;
if (disposing) // if (disposing)
{ // {
_ctx?.Dispose(); // _ctx?.Dispose();
} // }
_disposed = true; // _disposed = true;
} //}
public void Dispose() //public void Dispose()
{ //{
Dispose(true); // Dispose(true);
GC.SuppressFinalize(this); // GC.SuppressFinalize(this);
} //}
} }
} }

View file

@ -9,7 +9,7 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository namespace Ombi.Store.Repository
{ {
public interface IExternalRepository<T> : IDisposable where T : Entity public interface IExternalRepository<T> where T : Entity
{ {
Task<T> Find(object key); Task<T> Find(object key);
IQueryable<T> GetAll(); IQueryable<T> GetAll();

View file

@ -9,7 +9,7 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository namespace Ombi.Store.Repository
{ {
public interface IRepository<T> : IDisposable where T : Entity public interface IRepository<T> where T : Entity
{ {
Task<T> Find(object key); Task<T> Find(object key);
IQueryable<T> GetAll(); IQueryable<T> GetAll();