diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index feb3c55be..50189aa40 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -231,6 +231,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } diff --git a/src/Ombi.Schedule/Jobs/Ombi/AutoDeleteRequests.cs b/src/Ombi.Schedule/Jobs/Ombi/AutoDeleteRequests.cs index 4d561c4ca..6625a3bbb 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/AutoDeleteRequests.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/AutoDeleteRequests.cs @@ -2,28 +2,29 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using Ombi.Core; -using Ombi.Core.Senders; +using Microsoft.Extensions.Logging; using Ombi.Core.Settings; using Ombi.Settings.Settings.Models; -using Ombi.Store.Entities; -using Ombi.Store.Repository; using Ombi.Store.Repository.Requests; using Quartz; namespace Ombi.Schedule.Jobs.Ombi { - public interface IAutoDeleteRequests : IBaseJob { } public class AutoDeleteRequests : IAutoDeleteRequests { private readonly ISettingsService _ombiSettings; private readonly IMovieRequestRepository _movieRequests; + private readonly ITvRequestRepository _tvRequestRepository; + private readonly ILogger _logger; - public AutoDeleteRequests(ISettingsService ombiSettings, IMovieRequestRepository movieRequest) + public AutoDeleteRequests(ISettingsService ombiSettings, IMovieRequestRepository movieRequest, + ILogger logger, ITvRequestRepository tvRequestRepository) { _ombiSettings = ombiSettings; _movieRequests = movieRequest; + _tvRequestRepository = tvRequestRepository; + _logger = logger; } public async Task Execute(IJobExecutionContext job) @@ -33,18 +34,36 @@ namespace Ombi.Schedule.Jobs.Ombi { 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(); - 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; diff --git a/src/Ombi.Schedule/Jobs/Ombi/IAutoDeleteRequests.cs b/src/Ombi.Schedule/Jobs/Ombi/IAutoDeleteRequests.cs new file mode 100644 index 000000000..9c23e2c8b --- /dev/null +++ b/src/Ombi.Schedule/Jobs/Ombi/IAutoDeleteRequests.cs @@ -0,0 +1,4 @@ +namespace Ombi.Schedule.Jobs.Ombi +{ + public interface IAutoDeleteRequests : IBaseJob { } +} \ No newline at end of file diff --git a/src/Ombi.Schedule/OmbiScheduler.cs b/src/Ombi.Schedule/OmbiScheduler.cs index d3c8db860..4a91052c7 100644 --- a/src/Ombi.Schedule/OmbiScheduler.cs +++ b/src/Ombi.Schedule/OmbiScheduler.cs @@ -67,6 +67,7 @@ namespace Ombi.Schedule await OmbiQuartz.Instance.AddJob(nameof(INewsletterJob), "System", JobSettingsHelper.Newsletter(s)); await OmbiQuartz.Instance.AddJob(nameof(IResendFailedRequests), "System", JobSettingsHelper.ResendFailedRequests(s)); await OmbiQuartz.Instance.AddJob(nameof(IMediaDatabaseRefresh), "System", JobSettingsHelper.MediaDatabaseRefresh(s)); + await OmbiQuartz.Instance.AddJob(nameof(IAutoDeleteRequests), "System", JobSettingsHelper.AutoDeleteRequests(s)); } private static async Task AddDvrApps(JobSettings s) diff --git a/src/Ombi.Settings/Settings/Models/JobSettings.cs b/src/Ombi.Settings/Settings/Models/JobSettings.cs index bd8b81db0..9bdad5e2b 100644 --- a/src/Ombi.Settings/Settings/Models/JobSettings.cs +++ b/src/Ombi.Settings/Settings/Models/JobSettings.cs @@ -16,5 +16,6 @@ public string IssuesPurge { get; set; } public string RetryRequests { get; set; } public string MediaDatabaseRefresh { get; set; } + public string AutoDeleteRequests { get; set; } } } \ No newline at end of file diff --git a/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs b/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs index 2001b5086..2778ba478 100644 --- a/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs +++ b/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs @@ -76,6 +76,11 @@ namespace Ombi.Settings.Settings.Models return ValidateCron(Get(s.MediaDatabaseRefresh, Cron.DayInterval(5))); } + public static string AutoDeleteRequests(JobSettings s) + { + return ValidateCron(Get(s.AutoDeleteRequests, Cron.Daily())); + } + private static string Get(string settings, string defaultCron) { return settings.HasValue() ? settings : defaultCron; diff --git a/src/Ombi.Store/Repository/Requests/ITvRequestRepository.cs b/src/Ombi.Store/Repository/Requests/ITvRequestRepository.cs index 56fdb6fba..fa480a2f8 100644 --- a/src/Ombi.Store/Repository/Requests/ITvRequestRepository.cs +++ b/src/Ombi.Store/Repository/Requests/ITvRequestRepository.cs @@ -12,6 +12,7 @@ namespace Ombi.Store.Repository.Requests Task Add(TvRequests request); Task AddChild(ChildRequests request); Task Delete(TvRequests request); + Task DeleteRange(IEnumerable request); Task DeleteChild(ChildRequests request); IQueryable Get(); IQueryable GetLite(); diff --git a/src/Ombi/ClientApp/src/app/interfaces/ISettings.ts b/src/Ombi/ClientApp/src/app/interfaces/ISettings.ts index 731cb28bf..764ac25e1 100644 --- a/src/Ombi/ClientApp/src/app/interfaces/ISettings.ts +++ b/src/Ombi/ClientApp/src/app/interfaces/ISettings.ts @@ -17,6 +17,8 @@ export interface IOmbiSettings extends ISettings { hideRequestsUsers: boolean; defaultLanguageCode: string; disableHealthChecks: boolean; + autoDeleteAvailableRequests: boolean; + autoDeleteAfterDays: number; } export interface IUpdateSettings extends ISettings { @@ -146,6 +148,7 @@ export interface IJobSettings { issuesPurge: string; retryRequests: string; mediaDatabaseRefresh: string; + autoDeleteRequests: string; } export interface IIssueSettings extends ISettings { diff --git a/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.html b/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.html index 2d6a2cf5a..ec2a42c99 100644 --- a/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.html +++ b/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.html @@ -105,6 +105,13 @@ The Media Database Refresh is required + +
+ + + Auto Available Request Deletion is required + +
diff --git a/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.ts b/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.ts index acc6272f1..914d126d0 100644 --- a/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.ts +++ b/src/Ombi/ClientApp/src/app/settings/jobs/jobs.component.ts @@ -34,7 +34,8 @@ export class JobsComponent implements OnInit { lidarrArtistSync: [x.lidarrArtistSync, Validators.required], issuesPurge: [x.issuesPurge, Validators.required], retryRequests: [x.retryRequests, Validators.required], - mediaDatabaseRefresh: [x.mediaDatabaseRefresh, Validators.required] + mediaDatabaseRefresh: [x.mediaDatabaseRefresh, Validators.required], + autoDeleteRequests: [x.autoDeleteRequests, Validators.required] }); }); } diff --git a/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.html b/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.html index aed82c05f..9932e137e 100644 --- a/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.html +++ b/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.html @@ -35,6 +35,17 @@ Hide requests from other users
+
+ + Auto Delete Available Requests + +
+
+ + Delete After Days of Availbility + + +
Ignore any certificate errors (Please restart after changing) diff --git a/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.ts b/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.ts index 51365fc94..d2b7ba942 100644 --- a/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.ts +++ b/src/Ombi/ClientApp/src/app/settings/ombi/ombi.component.ts @@ -30,10 +30,11 @@ export class OmbiComponent implements OnInit { doNotSendNotificationsForAutoApprove: [x.doNotSendNotificationsForAutoApprove], hideRequestsUsers: [x.hideRequestsUsers], defaultLanguageCode: [x.defaultLanguageCode], - disableHealthChecks: [x.disableHealthChecks] + disableHealthChecks: [x.disableHealthChecks], + autoDeleteAvailableRequests: [x.autoDeleteAvailableRequests], + autoDeleteAfterDays: [x.autoDeleteAfterDays] }); }); - debugger; this.langauges = languageData } diff --git a/src/Ombi/Controllers/V1/JobController.cs b/src/Ombi/Controllers/V1/JobController.cs index b9f029194..6ffbf8486 100644 --- a/src/Ombi/Controllers/V1/JobController.cs +++ b/src/Ombi/Controllers/V1/JobController.cs @@ -146,6 +146,14 @@ namespace Ombi.Controllers.V1 return true; } + + [HttpPost("autodeleterequests")] + public async Task StartAutoDeleteRequests() + { + await OmbiQuartz.TriggerJob(nameof(IAutoDeleteRequests), "System"); + return true; + } + /// /// Runs the newsletter /// diff --git a/src/Ombi/Controllers/V1/SettingsController.cs b/src/Ombi/Controllers/V1/SettingsController.cs index 1e77704db..32cafd904 100644 --- a/src/Ombi/Controllers/V1/SettingsController.cs +++ b/src/Ombi/Controllers/V1/SettingsController.cs @@ -575,6 +575,7 @@ namespace Ombi.Controllers.V1 j.IssuesPurge = j.IssuesPurge.HasValue() ? j.IssuesPurge : JobSettingsHelper.IssuePurge(j); j.RetryRequests = j.RetryRequests.HasValue() ? j.RetryRequests : JobSettingsHelper.ResendFailedRequests(j); j.MediaDatabaseRefresh = j.MediaDatabaseRefresh.HasValue() ? j.MediaDatabaseRefresh : JobSettingsHelper.MediaDatabaseRefresh(j); + j.AutoDeleteRequests = j.AutoDeleteRequests.HasValue() ? j.AutoDeleteRequests : JobSettingsHelper.AutoDeleteRequests(j); return j; }