fixed #3581 (merge)

pull/3598/head
tidusjar 5 years ago
commit f2013c063b

@ -38,7 +38,7 @@ namespace Ombi.Helpers
return Scheduler; return Scheduler;
} }
public static async Task<bool> IsJobRunnung(string jobName) public static async Task<bool> IsJobRunning(string jobName)
{ {
var running = await Scheduler.GetCurrentlyExecutingJobs(); var running = await Scheduler.GetCurrentlyExecutingJobs();
return running.Any(x => x.JobDetail.Key.Name.Equals(jobName, StringComparison.InvariantCultureIgnoreCase)); return running.Any(x => x.JobDetail.Key.Name.Equals(jobName, StringComparison.InvariantCultureIgnoreCase));
@ -82,9 +82,12 @@ namespace Ombi.Helpers
} }
public static async Task TriggerJob(string jobName, string group) public static async Task TriggerJob(string jobName, string group)
{
if (!(await IsJobRunning(jobName)))
{ {
await Scheduler.TriggerJob(new JobKey(jobName, group)); await Scheduler.TriggerJob(new JobKey(jobName, group));
} }
}
public static async Task TriggerJob(string jobName, string group, IDictionary<string, object> data) public static async Task TriggerJob(string jobName, string group, IDictionary<string, object> data)
{ {

@ -39,6 +39,7 @@ using Ombi.Helpers;
using Ombi.Store.Entities; using Ombi.Store.Entities;
using Ombi.Store.Repository; using Ombi.Store.Repository;
using Quartz; using Quartz;
using Ombi.Schedule.Jobs.Ombi;
namespace Ombi.Schedule.Jobs.Emby namespace Ombi.Schedule.Jobs.Emby
{ {
@ -77,7 +78,7 @@ namespace Ombi.Schedule.Jobs.Emby
await _notification.Clients.Clients(NotificationHub.AdminConnectionIds) await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
.SendAsync(NotificationHub.NotificationEvent, "Emby Episode Sync Finished"); .SendAsync(NotificationHub.NotificationEvent, "Emby Episode Sync Finished");
await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker), "Emby"); await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata), "System");
} }
private async Task CacheEpisodes(EmbyServers server) private async Task CacheEpisodes(EmbyServers server)

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ombi.Api.Emby; using Ombi.Api.Emby;
using Ombi.Api.TheMovieDb; using Ombi.Api.TheMovieDb;
@ -59,12 +61,16 @@ namespace Ombi.Schedule.Jobs.Ombi
if (settings.Enable) if (settings.Enable)
{ {
await StartPlex(); await StartPlex();
await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex");
} }
var embySettings = await _embySettings.GetSettingsAsync(); var embySettings = await _embySettings.GetSettingsAsync();
if (embySettings.Enable) if (embySettings.Enable)
{ {
await StartEmby(embySettings); await StartEmby(embySettings);
await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker), "Emby");
} }
} }
catch (Exception e) catch (Exception e)
@ -83,13 +89,13 @@ namespace Ombi.Schedule.Jobs.Ombi
private async Task StartPlex() private async Task StartPlex()
{ {
// Ensure we check that we have not linked this item to a request // Ensure we check that we have not linked this item to a request
var allMovies = _plexRepo.GetAll().Where(x => var allMovies = await _plexRepo.GetAll().Where(x =>
x.Type == PlexMediaTypeEntity.Movie && x.RequestId == null && (x.TheMovieDbId == null || x.ImdbId == null)); x.Type == PlexMediaTypeEntity.Movie && x.RequestId == null && (x.TheMovieDbId == null || x.ImdbId == null)).ToListAsync();
await StartPlexMovies(allMovies); await StartPlexMovies(allMovies);
// Now Tv // Now Tv
var allTv = _plexRepo.GetAll().Where(x => var allTv = await _plexRepo.GetAll().Where(x =>
x.Type == PlexMediaTypeEntity.Show && x.RequestId == null && (x.TheMovieDbId == null || x.ImdbId == null || x.TvDbId == null)); x.Type == PlexMediaTypeEntity.Show && x.RequestId == null && (x.TheMovieDbId == null || x.ImdbId == null || x.TvDbId == null)).ToListAsync();
await StartPlexTv(allTv); await StartPlexTv(allTv);
} }
@ -100,7 +106,7 @@ namespace Ombi.Schedule.Jobs.Ombi
await StartEmbyTv(); await StartEmbyTv();
} }
private async Task StartPlexTv(IQueryable<PlexServerContent> allTv) private async Task StartPlexTv(List<PlexServerContent> allTv)
{ {
foreach (var show in allTv) foreach (var show in allTv)
{ {
@ -138,8 +144,8 @@ namespace Ombi.Schedule.Jobs.Ombi
private async Task StartEmbyTv() private async Task StartEmbyTv()
{ {
var allTv = _embyRepo.GetAll().Where(x => var allTv = await _embyRepo.GetAll().Where(x =>
x.Type == EmbyMediaType.Series && (x.TheMovieDbId == null || x.ImdbId == null || x.TvDbId == null)); x.Type == EmbyMediaType.Series && (x.TheMovieDbId == null || x.ImdbId == null || x.TvDbId == null)).ToListAsync();
foreach (var show in allTv) foreach (var show in allTv)
{ {
@ -171,7 +177,7 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
} }
private async Task StartPlexMovies(IQueryable<PlexServerContent> allMovies) private async Task StartPlexMovies(List<PlexServerContent> allMovies)
{ {
foreach (var movie in allMovies) foreach (var movie in allMovies)
{ {
@ -203,8 +209,8 @@ namespace Ombi.Schedule.Jobs.Ombi
private async Task StartEmbyMovies(EmbySettings settings) private async Task StartEmbyMovies(EmbySettings settings)
{ {
var allMovies = _embyRepo.GetAll().Where(x => var allMovies = await _embyRepo.GetAll().Where(x =>
x.Type == EmbyMediaType.Movie && (x.TheMovieDbId == null || x.ImdbId == null)); x.Type == EmbyMediaType.Movie && (x.TheMovieDbId == null || x.ImdbId == null)).ToListAsync();
foreach (var movie in allMovies) foreach (var movie in allMovies)
{ {
movie.ImdbId.HasValue(); movie.ImdbId.HasValue();

@ -60,15 +60,15 @@ namespace Ombi.Schedule.Jobs.Plex
.SendAsync(NotificationHub.NotificationEvent, "Plex Availability Check Finished"); .SendAsync(NotificationHub.NotificationEvent, "Plex Availability Check Finished");
} }
private Task ProcessTv() private async Task ProcessTv()
{ {
var tv = _tvRepo.GetChild().Where(x => !x.Available).AsNoTracking(); var tv = await _tvRepo.GetChild().Where(x => !x.Available).ToListAsync();
return ProcessTv(tv); await ProcessTv(tv);
} }
private async Task ProcessTv(IQueryable<ChildRequests> tv) private async Task ProcessTv(List<ChildRequests> tv)
{ {
var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series).AsNoTracking(); var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);
foreach (var child in tv) foreach (var child in tv)
{ {
@ -119,11 +119,11 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
continue; continue;
} }
var foundEp = await seriesEpisodes.FirstOrDefaultAsync( var foundEp = await seriesEpisodes.AnyAsync(
x => x.EpisodeNumber == episode.EpisodeNumber && x => x.EpisodeNumber == episode.EpisodeNumber &&
x.SeasonNumber == episode.Season.SeasonNumber); x.SeasonNumber == episode.Season.SeasonNumber);
if (foundEp != null) if (foundEp)
{ {
availableEpisode.Add(new AvailabilityModel availableEpisode.Add(new AvailabilityModel
{ {
@ -135,18 +135,24 @@ namespace Ombi.Schedule.Jobs.Plex
} }
//TODO Partial avilability notifications here //TODO Partial avilability notifications here
foreach(var c in availableEpisode) if (availableEpisode.Any())
{ {
await _tvRepo.MarkEpisodeAsAvailable(c.Id); await _tvRepo.Save();
} }
//foreach(var c in availableEpisode)
//{
// await _tvRepo.MarkEpisodeAsAvailable(c.Id);
//}
// Check to see if all of the episodes in all seasons are available for this request // Check to see if all of the episodes in all seasons are available for this request
var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available)); var allAvailable = child.SeasonRequests.All(x => x.Episodes.All(c => c.Available));
if (allAvailable) if (allAvailable)
{ {
child.Available = true;
child.MarkedAsAvailable = DateTime.UtcNow;
_log.LogInformation("[PAC] - Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}"); _log.LogInformation("[PAC] - Child request {0} is now available, sending notification", $"{child.Title} - {child.Id}");
// We have ful-fulled this request! // We have ful-fulled this request!
await _tvRepo.MarkChildAsAvailable(child.Id); await _tvRepo.Save();
await _notificationService.Notify(new NotificationOptions await _notificationService.Notify(new NotificationOptions
{ {
DateTime = DateTime.Now, DateTime = DateTime.Now,
@ -164,7 +170,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().Include(x => x.RequestedUser).Where(x => !x.Available).AsNoTracking(); var movies = _movieRepo.GetAll().Include(x => x.RequestedUser).Where(x => !x.Available);
var itemsForAvailbility = new List<AvailabilityModel>(); var itemsForAvailbility = new List<AvailabilityModel>();
foreach (var movie in movies) foreach (var movie in movies)
@ -193,6 +199,8 @@ namespace Ombi.Schedule.Jobs.Plex
} }
_log.LogInformation("[PAC] - Movie request {0} is now available, sending notification", $"{movie.Title} - {movie.Id}"); _log.LogInformation("[PAC] - Movie request {0} is now available, sending notification", $"{movie.Title} - {movie.Id}");
movie.Available = true;
movie.MarkedAsAvailable = DateTime.UtcNow;
itemsForAvailbility.Add(new AvailabilityModel itemsForAvailbility.Add(new AvailabilityModel
{ {
Id = movie.Id, Id = movie.Id,
@ -200,9 +208,12 @@ namespace Ombi.Schedule.Jobs.Plex
}); });
} }
if (itemsForAvailbility.Any())
{
await _movieRepo.SaveChangesAsync();
}
foreach (var i in itemsForAvailbility) foreach (var i in itemsForAvailbility)
{ {
await _movieRepo.MarkAsAvailable(i.Id);
await _notificationService.Notify(new NotificationOptions await _notificationService.Notify(new NotificationOptions
{ {
@ -214,7 +225,7 @@ namespace Ombi.Schedule.Jobs.Plex
}); });
} }
await _repo.SaveChangesAsync(); //await _repo.SaveChangesAsync();
} }
private bool _disposed; private bool _disposed;

@ -116,7 +116,7 @@ namespace Ombi.Schedule.Jobs.Plex
if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch)
{ {
// Ensure it's not already running // Ensure it's not already running
if (await OmbiQuartz.IsJobRunnung(nameof(IPlexAvailabilityChecker))) if (await OmbiQuartz.IsJobRunning(nameof(IPlexAvailabilityChecker)))
{ {
Logger.LogInformation("Availability checker already running"); Logger.LogInformation("Availability checker already running");
} }
@ -131,7 +131,7 @@ namespace Ombi.Schedule.Jobs.Plex
if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch)
{ {
// Ensure it's not already running // Ensure it's not already running
if (await OmbiQuartz.IsJobRunnung(nameof(IPlexAvailabilityChecker))) if (await OmbiQuartz.IsJobRunning(nameof(IPlexAvailabilityChecker)))
{ {
Logger.LogInformation("Availability checker already running"); Logger.LogInformation("Availability checker already running");
} }

@ -63,16 +63,9 @@ namespace Ombi.Schedule.Jobs.Plex
_log.LogError(LoggingEvents.Cacher, e, "Caching Episodes Failed"); _log.LogError(LoggingEvents.Cacher, e, "Caching Episodes Failed");
} }
await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata), "System"); await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata), "System");
// Ensure it's not already running
if (await OmbiQuartz.IsJobRunnung(nameof(IPlexAvailabilityChecker)))
{
_log.LogInformation("Availability checker already running");
}
else
{
await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex");
}
await _notification.Clients.Clients(NotificationHub.AdminConnectionIds) await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
.SendAsync(NotificationHub.NotificationEvent, "Plex Episode Sync Finished"); .SendAsync(NotificationHub.NotificationEvent, "Plex Episode Sync Finished");
} }
@ -172,7 +165,7 @@ namespace Ombi.Schedule.Jobs.Plex
{ {
// Ok let's try and match it to a title. TODO (This is experimental) // Ok let's try and match it to a title. TODO (This is experimental)
seriesExists = await _repo.GetAll().FirstOrDefaultAsync(x => seriesExists = await _repo.GetAll().FirstOrDefaultAsync(x =>
x.Title.Equals(episode.grandparentTitle, StringComparison.CurrentCultureIgnoreCase)); x.Title == episode.grandparentTitle);
if (seriesExists == null) if (seriesExists == null)
{ {
_log.LogWarning( _log.LogWarning(

@ -120,7 +120,7 @@ namespace Ombi.Store.Context
notificationToAdd = new NotificationTemplates notificationToAdd = new NotificationTemplates
{ {
NotificationType = notificationType, NotificationType = notificationType,
Message = "Hello! Your request for {Title} on {ApplicationName}! This is now available! :)", Message = "Hello! Your request for {Title} on {ApplicationName} is now available! :)",
Subject = "{ApplicationName}: {Title} is now available!", Subject = "{ApplicationName}: {Title} is now available!",
Agent = agent, Agent = agent,
Enabled = true, Enabled = true,

@ -80,31 +80,14 @@ namespace Ombi.Store.Repository
public async Task ExecuteSql(string sql) public async Task ExecuteSql(string sql)
{ {
await _ctx.Database.ExecuteSqlCommandAsync(sql); await _ctx.Database.ExecuteSqlRawAsync(sql);
} }
protected async Task<int> InternalSaveChanges() protected async Task<int> InternalSaveChanges()
{
var policy = Policy
.Handle<SqliteException>()
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)
});
var result = await policy.ExecuteAndCaptureAsync(async () =>
{
using (var tran = await _ctx.Database.BeginTransactionAsync())
{ {
var r = await _ctx.SaveChangesAsync(); var r = await _ctx.SaveChangesAsync();
tran.Commit();
return r; return r;
} }
});
return result.Result;
}
//private bool _disposed; //private bool _disposed;

Loading…
Cancel
Save