pull/3700/head
Jamie Rees 4 years ago
parent 8cf7eb0e7b
commit 63fd43b1d8

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Ombi.Core;
using Ombi.Helpers;
using Ombi.Hubs;
using Ombi.Notifications.Models;
using Ombi.Schedule.Jobs.Plex.Models;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
using Quartz;
namespace Ombi.Schedule.Jobs.Radarr
{
public class ArrAvailabilityChecker
{
public ArrAvailabilityChecker(
IExternalRepository<RadarrCache> radarrRepo,
IExternalRepository<SonarrCache> sonarrRepo,
IExternalRepository<SonarrEpisodeCache> sonarrEpisodeRepo,
INotificationHelper notification, IHubContext<NotificationHub> hub,
ITvRequestRepository tvRequest, IMovieRequestRepository movies,
ILogger<ArrAvailabilityChecker> log)
{
_radarrRepo = radarrRepo;
_sonarrRepo = sonarrRepo;
_sonarrEpisodeRepo = sonarrEpisodeRepo;
_notification = notification;
_hub = hub;
_tvRequest = tvRequest;
_movies = movies;
_logger = log;
}
private readonly IExternalRepository<RadarrCache> _radarrRepo;
private readonly IExternalRepository<SonarrCache> _sonarrRepo;
private readonly ILogger<ArrAvailabilityChecker> _logger;
private readonly IExternalRepository<SonarrEpisodeCache> _sonarrEpisodeRepo;
private readonly INotificationHelper _notification;
private readonly IHubContext<NotificationHub> _hub;
private readonly ITvRequestRepository _tvRequest;
private readonly IMovieRequestRepository _movies;
public async Task Execute(IJobExecutionContext job)
{
await ProcessMovies();
await ProcessTvShows();
}
private async Task ProcessMovies()
{
var availableRadarrMovies = _radarrRepo.GetAll().Where(x => x.HasFile).ToImmutableHashSet();
var unavailableMovieRequests = _movies.GetAll().Where(x => !x.Available).ToImmutableHashSet();
var itemsForAvailability = new List<AvailabilityModel>();
foreach (var movieRequest in unavailableMovieRequests)
{
// Do we have an item in the radarr list
var available = availableRadarrMovies.Any(x => x.TheMovieDbId == movieRequest.TheMovieDbId);
if (available)
{
movieRequest.Available = true;
movieRequest.MarkedAsAvailable = DateTime.UtcNow;
itemsForAvailability.Add(new AvailabilityModel
{
Id = movieRequest.Id,
RequestedUser = movieRequest.RequestedUser != null ? movieRequest.RequestedUser.Email : string.Empty
});
}
}
if (itemsForAvailability.Any())
{
await _movies.SaveChangesAsync();
}
foreach (var item in itemsForAvailability)
{
await _notification.Notify(new NotificationOptions
{
DateTime = DateTime.Now,
NotificationType = NotificationType.RequestAvailable,
RequestId = item.Id,
RequestType = RequestType.Movie,
Recipient = item.RequestedUser
});
}
}
public async Task ProcessTvShows()
{
}
}
}

@ -49,29 +49,29 @@ namespace Ombi.Schedule.Jobs.Radarr
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM RadarrCache");
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM RadarrCache");
tran.Commit();
}
var movieIds = new List<RadarrCache>();
foreach (var m in movies)
{
if(m.monitored)
{
if (m.tmdbId > 0)
if (m.monitored || m.hasFile)
{
movieIds.Add(new RadarrCache
if (m.tmdbId > 0)
{
TheMovieDbId = m.tmdbId,
HasFile = m.hasFile
});
}
else
{
Logger.LogError("TMDBId is not > 0 for movie {0}", m.title);
movieIds.Add(new RadarrCache
{
TheMovieDbId = m.tmdbId,
HasFile = m.hasFile
});
}
else
{
Logger.LogError("TMDBId is not > 0 for movie {0}", m.title);
}
}
}
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{

@ -33,7 +33,7 @@ namespace Ombi.Schedule.Jobs.Sonarr
private readonly ISonarrApi _api;
private readonly ILogger<SonarrSync> _log;
private readonly ExternalContext _ctx;
public async Task Execute(IJobExecutionContext job)
{
try
@ -50,48 +50,81 @@ namespace Ombi.Schedule.Jobs.Sonarr
var ids = sonarrSeries.Select(x => x.tvdbId);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrCache");
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrCache");
tran.Commit();
}
var existingSeries = await _ctx.SonarrCache.Select(x => x.TvDbId).ToListAsync();
//var entites = ids.Except(existingSeries).Select(id => new SonarrCache { TvDbId = id }).ToImmutableHashSet();
var entites = ids.Select(id => new SonarrCache { TvDbId = id }).ToImmutableHashSet();
await _ctx.SonarrCache.AddRangeAsync(entites);
entites.Clear();
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrEpisodeCache");
await _ctx.Database.ExecuteSqlRawAsync("DELETE FROM SonarrEpisodeCache");
tran.Commit();
}
foreach (var s in sonarrSeries)
{
if (!s.monitored)
if (!s.monitored || s.episodeFileCount > 0) // We have files
{
continue;
}
_log.LogDebug("Syncing series: {0}", s.title);
var episodes = await _api.GetEpisodes(s.id, settings.ApiKey, settings.FullUri);
var monitoredEpisodes = episodes.Where(x => x.monitored || x.hasFile);
var allExistingEpisodes = await _ctx.SonarrEpisodeCache.Where(x => x.TvDbId == s.tvdbId).ToListAsync();
// Add to DB
_log.LogDebug("We have the episodes, adding to db transaction");
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SonarrEpisodeCache.AddRangeAsync(monitoredEpisodes.Select(episode =>
var episodesToAdd = monitoredEpisodes.Select(episode =>
new SonarrEpisodeCache
{
EpisodeNumber = episode.episodeNumber,
SeasonNumber = episode.seasonNumber,
TvDbId = s.tvdbId,
HasFile = episode.hasFile
}));
});
//var episodesToAdd = new List<SonarrEpisodeCache>();
//foreach (var monitored in monitoredEpisodes)
//{
// var existing = allExistingEpisodes.FirstOrDefault(x => x.SeasonNumber == monitored.seasonNumber && x.EpisodeNumber == monitored.episodeNumber);
// if (existing == null)
// {
// // Just add a new one
// episodesToAdd.Add(new SonarrEpisodeCache
// {
// EpisodeNumber = monitored.episodeNumber,
// SeasonNumber = monitored.seasonNumber,
// TvDbId = s.tvdbId,
// HasFile = monitored.hasFile
// });
// }
// else
// {
// // Do we need to update the availability?
// if (monitored.hasFile != existing.HasFile)
// {
// existing.HasFile = monitored.hasFile;
// }
// }
//}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SonarrEpisodeCache.AddRangeAsync(episodesToAdd);
_log.LogDebug("Commiting the transaction");
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
}
catch (Exception e)

Loading…
Cancel
Save