@ -2,28 +2,29 @@
using System.Linq ;
using System.Linq ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore ;
using Ombi.Core ;
using Microsoft.Extensions.Logging ;
using Ombi.Core.Senders ;
using Ombi.Core.Settings ;
using Ombi.Core.Settings ;
using Ombi.Settings.Settings.Models ;
using Ombi.Settings.Settings.Models ;
using Ombi.Store.Entities ;
using Ombi.Store.Repository ;
using Ombi.Store.Repository.Requests ;
using Ombi.Store.Repository.Requests ;
using Quartz ;
using Quartz ;
namespace Ombi.Schedule.Jobs.Ombi
namespace Ombi.Schedule.Jobs.Ombi
{
{
public interface IAutoDeleteRequests : IBaseJob { }
public class AutoDeleteRequests : IAutoDeleteRequests
public class AutoDeleteRequests : IAutoDeleteRequests
{
{
private readonly ISettingsService < OmbiSettings > _ombiSettings ;
private readonly ISettingsService < OmbiSettings > _ombiSettings ;
private readonly IMovieRequestRepository _movieRequests ;
private readonly IMovieRequestRepository _movieRequests ;
private readonly ITvRequestRepository _tvRequestRepository ;
private readonly ILogger < AutoDeleteRequests > _logger ;
public AutoDeleteRequests ( ISettingsService < OmbiSettings > ombiSettings , IMovieRequestRepository movieRequest )
public AutoDeleteRequests ( ISettingsService < OmbiSettings > ombiSettings , IMovieRequestRepository movieRequest ,
ILogger < AutoDeleteRequests > logger , ITvRequestRepository tvRequestRepository )
{
{
_ombiSettings = ombiSettings ;
_ombiSettings = ombiSettings ;
_movieRequests = movieRequest ;
_movieRequests = movieRequest ;
_tvRequestRepository = tvRequestRepository ;
_logger = logger ;
}
}
public async Task Execute ( IJobExecutionContext job )
public async Task Execute ( IJobExecutionContext job )
@ -33,18 +34,36 @@ namespace Ombi.Schedule.Jobs.Ombi
{
{
return ;
return ;
}
}
await ProcessMovieRequests ( settings . AutoDeleteAfterDays ) ;
var date = DateTime . UtcNow . AddDays ( - settings . AutoDeleteAfterDays ) . Date ;
await ProcessMovieRequests ( date ) ;
await ProcessTvRequests ( date ) ;
}
}
private async Task ProcessMovieRequests ( int deleteAfterDays )
private async Task ProcessMovieRequests ( DateTime date )
{
{
var date = DateTime . UtcNow . AddDays ( - deleteAfterDays ) . Date ;
var requestsToDelete = await _movieRequests . GetAll ( ) . Where ( x = > x . Available & & x . MarkedAsAvailable . HasValue & & x . MarkedAsAvailable . Value < date ) . ToListAsync ( ) ;
var requestsToDelete = await _movieRequests . GetAll ( ) . Where ( x = > x . Available & & x . MarkedAsAvailable . HasValue & & x . MarkedAsAvailable . Value < date ) . ToListAsync ( ) ;
foreach ( var request in requestsToDelete )
_logger . LogInformation ( $"Deleting {requestsToDelete.Count} movie requests that have now been scheduled for deletion, All available requests before {date::MM/dd/yyyy} will be deleted" ) ;
foreach ( var r in requestsToDelete )
{
{
_logger . LogInformation ( $"Deleting movie title {r.Title} as it was approved on {r.MarkedAsApproved:MM/dd/yyyy hh:mm tt}" ) ;
}
await _movieRequests . DeleteRange ( requestsToDelete ) ;
}
}
private async Task ProcessTvRequests ( DateTime date )
{
var requestsToDelete = await _tvRequestRepository . GetChild ( ) . Where ( x = > x . Available & & x . MarkedAsAvailable . HasValue & & x . MarkedAsAvailable . Value < date ) . ToListAsync ( ) ;
_logger . LogInformation ( $"Deleting {requestsToDelete.Count} episode requests that have now been scheduled for deletion, All available requests before {date::MM/dd/yyyy} will be deleted" ) ;
await _tvRequestRepository . DeleteChildRange ( requestsToDelete ) ;
// Check if we have parent requests without any child requests now
var parentRequests = await _tvRequestRepository . Get ( ) . Where ( x = > ! x . ChildRequests . Any ( ) ) . ToListAsync ( ) ;
await _tvRequestRepository . DeleteRange ( parentRequests ) ;
}
}
private bool _disposed ;
private bool _disposed ;