From 6d5a823353af388c59a7a8a8269aebff19ff69ab Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 17 Nov 2017 22:49:57 +0000 Subject: [PATCH] Potential fix for the DB locking issue #1720 --- .../Agents/TelegramNotification.cs | 2 +- .../Jobs/Plex/PlexAvailabilityChecker.cs | 6 +- .../Jobs/Plex/PlexEpisodeCacher.cs | 5 +- src/Ombi.Settings/Settings/SettingsService.cs | 69 ++++++++++++------- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/Ombi.Notifications/Agents/TelegramNotification.cs b/src/Ombi.Notifications/Agents/TelegramNotification.cs index 92c46ab89..5773b95d7 100644 --- a/src/Ombi.Notifications/Agents/TelegramNotification.cs +++ b/src/Ombi.Notifications/Agents/TelegramNotification.cs @@ -101,7 +101,7 @@ namespace Ombi.Notifications.Agents var notification = new NotificationMessage { - Message = parsed.Message, + Message = parsed.Message ?? string.Empty, }; await Send(notification, settings); diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs index f53be1f58..d9101742a 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs @@ -38,8 +38,8 @@ namespace Ombi.Schedule.Jobs.Plex private async Task ProcessTv() { - var tv = _tvRepo.GetChild().Where(x => !x.Available); - var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series); + var tv = await _tvRepo.GetChild().Where(x => !x.Available).ToListAsync(); + var plexEpisodes = await _repo.GetAllEpisodes().Include(x => x.Series).ToListAsync(); foreach (var child in tv) { @@ -106,7 +106,7 @@ namespace Ombi.Schedule.Jobs.Plex private async Task ProcessMovies() { // Get all non available - var movies = _movieRepo.GetAll().Where(x => !x.Available); + var movies = await _movieRepo.GetAll().Where(x => !x.Available).ToListAsync(); foreach (var movie in movies) { diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeCacher.cs b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeCacher.cs index 261d4328d..635f54905 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeCacher.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeCacher.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Hangfire; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Ombi.Api.Plex; using Ombi.Api.Plex.Models; @@ -98,7 +99,7 @@ namespace Ombi.Schedule.Jobs.Plex var currentPosition = 0; var resultCount = settings.EpisodeBatchSize == 0 ? 50 : settings.EpisodeBatchSize; var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); - var currentData = _repo.GetAllEpisodes(); + var currentData = await _repo.GetAllEpisodes().ToListAsync(); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}"); await ProcessEpsiodes(episodes, currentData); @@ -118,7 +119,7 @@ namespace Ombi.Schedule.Jobs.Plex await _repo.SaveChangesAsync(); } - private async Task ProcessEpsiodes(PlexContainer episodes, IQueryable currentEpisodes) + private async Task ProcessEpsiodes(PlexContainer episodes, IEnumerable currentEpisodes) { var ep = new HashSet(); diff --git a/src/Ombi.Settings/Settings/SettingsService.cs b/src/Ombi.Settings/Settings/SettingsService.cs index 305c3f3a7..7e791e2cf 100644 --- a/src/Ombi.Settings/Settings/SettingsService.cs +++ b/src/Ombi.Settings/Settings/SettingsService.cs @@ -1,10 +1,11 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Newtonsoft.Json; using Ombi.Core.Settings; using Ombi.Helpers; using Ombi.Store.Entities; using Ombi.Store.Repository; -using Microsoft.AspNetCore.DataProtection; +using Microsoft.Extensions.Caching.Memory; namespace Ombi.Settings.Settings { @@ -12,49 +13,63 @@ namespace Ombi.Settings.Settings where T : Models.Settings, new() { - public SettingsService(ISettingsRepository repo, IDataProtectionProvider provider) + public SettingsService(ISettingsRepository repo, IMemoryCache cache) { Repo = repo; EntityName = typeof(T).Name; - _protector = provider.CreateProtector(GetType().FullName); + _cache = cache; } private ISettingsRepository Repo { get; } private string EntityName { get; } - private readonly IDataProtector _protector; + private string CacheName => $"Settings{EntityName}"; + private readonly IMemoryCache _cache; public T GetSettings() { - var result = Repo.Get(EntityName); - if (result == null) + return _cache.GetOrCreate(CacheName, entry => { - return new T(); - } - result.Content = DecryptSettings(result); - var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject(result.Content, SerializerSettings.Settings); - - var model = obj; - - return model; + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2); + var result = Repo.Get(EntityName); + if (result == null) + { + return new T(); + } + result.Content = DecryptSettings(result); + var obj = string.IsNullOrEmpty(result.Content) + ? null + : JsonConvert.DeserializeObject(result.Content, SerializerSettings.Settings); + + var model = obj; + + return model; + }); } public async Task GetSettingsAsync() { - var result = await Repo.GetAsync(EntityName); - if (result == null) + return await _cache.GetOrCreateAsync(CacheName, async entry => { - return new T(); - } - result.Content = DecryptSettings(result); - var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject(result.Content, SerializerSettings.Settings); - - var model = obj; - - return model; + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2); + var result = await Repo.GetAsync(EntityName); + if (result == null) + { + return new T(); + } + result.Content = DecryptSettings(result); + var obj = string.IsNullOrEmpty(result.Content) + ? null + : JsonConvert.DeserializeObject(result.Content, SerializerSettings.Settings); + + var model = obj; + + return model; + }); } public bool SaveSettings(T model) { + _cache.Remove(CacheName); var entity = Repo.Get(EntityName); if (entity == null) @@ -81,6 +96,7 @@ namespace Ombi.Settings.Settings public async Task SaveSettingsAsync(T model) { + _cache.Remove(CacheName); var entity = await Repo.GetAsync(EntityName); if (entity == null) @@ -107,16 +123,17 @@ namespace Ombi.Settings.Settings public void Delete(T model) { + _cache.Remove(CacheName); var entity = Repo.Get(EntityName); if (entity != null) { Repo.Delete(entity); } - } public async Task DeleteAsync(T model) { + _cache.Remove(CacheName); var entity = Repo.Get(EntityName); if (entity != null) {