Potential fix for the DB locking issue #1720

This commit is contained in:
tidusjar 2017-11-17 22:49:57 +00:00
parent 1329a765ab
commit 6d5a823353
4 changed files with 46 additions and 28 deletions

View file

@ -101,7 +101,7 @@ namespace Ombi.Notifications.Agents
var notification = new NotificationMessage var notification = new NotificationMessage
{ {
Message = parsed.Message, Message = parsed.Message ?? string.Empty,
}; };
await Send(notification, settings); await Send(notification, settings);

View file

@ -38,8 +38,8 @@ namespace Ombi.Schedule.Jobs.Plex
private async Task ProcessTv() private async Task ProcessTv()
{ {
var tv = _tvRepo.GetChild().Where(x => !x.Available); var tv = await _tvRepo.GetChild().Where(x => !x.Available).ToListAsync();
var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series); var plexEpisodes = await _repo.GetAllEpisodes().Include(x => x.Series).ToListAsync();
foreach (var child in tv) foreach (var child in tv)
{ {
@ -106,7 +106,7 @@ namespace Ombi.Schedule.Jobs.Plex
private async Task ProcessMovies() private async Task ProcessMovies()
{ {
// Get all non available // 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) foreach (var movie in movies)
{ {

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Hangfire; using Hangfire;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ombi.Api.Plex; using Ombi.Api.Plex;
using Ombi.Api.Plex.Models; using Ombi.Api.Plex.Models;
@ -98,7 +99,7 @@ namespace Ombi.Schedule.Jobs.Plex
var currentPosition = 0; var currentPosition = 0;
var resultCount = settings.EpisodeBatchSize == 0 ? 50 : settings.EpisodeBatchSize; var resultCount = settings.EpisodeBatchSize == 0 ? 50 : settings.EpisodeBatchSize;
var episodes = await _api.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, section.key, currentPosition, resultCount); 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}"); _log.LogInformation(LoggingEvents.PlexEpisodeCacher, $"Total Epsiodes found for {episodes.MediaContainer.librarySectionTitle} = {episodes.MediaContainer.totalSize}");
await ProcessEpsiodes(episodes, currentData); await ProcessEpsiodes(episodes, currentData);
@ -118,7 +119,7 @@ namespace Ombi.Schedule.Jobs.Plex
await _repo.SaveChangesAsync(); await _repo.SaveChangesAsync();
} }
private async Task ProcessEpsiodes(PlexContainer episodes, IQueryable<PlexEpisode> currentEpisodes) private async Task ProcessEpsiodes(PlexContainer episodes, IEnumerable<PlexEpisode> currentEpisodes)
{ {
var ep = new HashSet<PlexEpisode>(); var ep = new HashSet<PlexEpisode>();

View file

@ -1,10 +1,11 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Ombi.Core.Settings; using Ombi.Core.Settings;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Repository; using Ombi.Store.Repository;
using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Caching.Memory;
namespace Ombi.Settings.Settings namespace Ombi.Settings.Settings
{ {
@ -12,49 +13,63 @@ namespace Ombi.Settings.Settings
where T : Models.Settings, new() where T : Models.Settings, new()
{ {
public SettingsService(ISettingsRepository repo, IDataProtectionProvider provider) public SettingsService(ISettingsRepository repo, IMemoryCache cache)
{ {
Repo = repo; Repo = repo;
EntityName = typeof(T).Name; EntityName = typeof(T).Name;
_protector = provider.CreateProtector(GetType().FullName); _cache = cache;
} }
private ISettingsRepository Repo { get; } private ISettingsRepository Repo { get; }
private string EntityName { get; } private string EntityName { get; }
private readonly IDataProtector _protector; private string CacheName => $"Settings{EntityName}";
private readonly IMemoryCache _cache;
public T GetSettings() public T GetSettings()
{ {
return _cache.GetOrCreate(CacheName, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2);
var result = Repo.Get(EntityName); var result = Repo.Get(EntityName);
if (result == null) if (result == null)
{ {
return new T(); return new T();
} }
result.Content = DecryptSettings(result); result.Content = DecryptSettings(result);
var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings); var obj = string.IsNullOrEmpty(result.Content)
? null
: JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings);
var model = obj; var model = obj;
return model; return model;
});
} }
public async Task<T> GetSettingsAsync() public async Task<T> GetSettingsAsync()
{ {
return await _cache.GetOrCreateAsync(CacheName, async entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(2);
var result = await Repo.GetAsync(EntityName); var result = await Repo.GetAsync(EntityName);
if (result == null) if (result == null)
{ {
return new T(); return new T();
} }
result.Content = DecryptSettings(result); result.Content = DecryptSettings(result);
var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings); var obj = string.IsNullOrEmpty(result.Content)
? null
: JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings);
var model = obj; var model = obj;
return model; return model;
});
} }
public bool SaveSettings(T model) public bool SaveSettings(T model)
{ {
_cache.Remove(CacheName);
var entity = Repo.Get(EntityName); var entity = Repo.Get(EntityName);
if (entity == null) if (entity == null)
@ -81,6 +96,7 @@ namespace Ombi.Settings.Settings
public async Task<bool> SaveSettingsAsync(T model) public async Task<bool> SaveSettingsAsync(T model)
{ {
_cache.Remove(CacheName);
var entity = await Repo.GetAsync(EntityName); var entity = await Repo.GetAsync(EntityName);
if (entity == null) if (entity == null)
@ -107,16 +123,17 @@ namespace Ombi.Settings.Settings
public void Delete(T model) public void Delete(T model)
{ {
_cache.Remove(CacheName);
var entity = Repo.Get(EntityName); var entity = Repo.Get(EntityName);
if (entity != null) if (entity != null)
{ {
Repo.Delete(entity); Repo.Delete(entity);
} }
} }
public async Task DeleteAsync(T model) public async Task DeleteAsync(T model)
{ {
_cache.Remove(CacheName);
var entity = Repo.Get(EntityName); var entity = Repo.Get(EntityName);
if (entity != null) if (entity != null)
{ {