using Common.Configuration.Arr; using Common.Configuration.QueueCleaner; using Domain.Enums; using Domain.Models.Arr; using Domain.Models.Arr.Queue; using Infrastructure.Verticals.Arr; using Infrastructure.Verticals.DownloadClient; using Infrastructure.Verticals.Jobs; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Infrastructure.Verticals.QueueCleaner; public sealed class QueueCleaner : GenericHandler { public QueueCleaner( ILogger logger, IOptions sonarrConfig, IOptions radarrConfig, SonarrClient sonarrClient, RadarrClient radarrClient, ArrQueueIterator arrArrQueueIterator, DownloadServiceFactory downloadServiceFactory ) : base(logger, sonarrConfig.Value, radarrConfig.Value, sonarrClient, radarrClient, arrArrQueueIterator, downloadServiceFactory) { } protected override async Task ProcessInstanceAsync(ArrInstance instance, InstanceType instanceType) { HashSet itemsToBeRefreshed = []; ArrClient arrClient = GetClient(instanceType); ArrConfig arrConfig = GetConfig(instanceType); await _arrArrQueueIterator.Iterate(arrClient, instance, async items => { var groups = items .GroupBy(x => x.DownloadId) .ToList(); foreach (var group in groups) { if (group.Any(x => !arrClient.IsRecordValid(x))) { continue; } QueueRecord record = group.First(); if (record.Protocol is not "torrent") { continue; } if (!arrClient.IsRecordValid(record)) { continue; } if (!arrClient.ShouldRemoveFromQueue(record) && !await _downloadService.ShouldRemoveFromArrQueueAsync(record.DownloadId)) { _logger.LogInformation("skip | {title}", record.Title); continue; } itemsToBeRefreshed.Add(GetRecordSearchItem(instanceType, record, group.Count() > 1)); await arrClient.DeleteQueueItemAsync(instance, record); } }); await arrClient.RefreshItemsAsync(instance, arrConfig, itemsToBeRefreshed); } }