public IDownloadClient GetDownloadClient(DownloadProtocol downloadProtocol, int indexerId = 0) { var availableProviders = _downloadClientFactory.GetAvailableProviders().Where(v => v.Protocol == downloadProtocol).ToList(); if (!availableProviders.Any()) { return(null); } if (indexerId > 0) { var indexer = _indexerFactory.Find(indexerId); if (indexer != null && indexer.DownloadClientId > 0) { var client = availableProviders.SingleOrDefault(d => d.Definition.Id == indexer.DownloadClientId); return(client ?? throw new DownloadClientUnavailableException($"Indexer specified download client is not available")); } } var blockedProviders = new HashSet <int>(_downloadClientStatusService.GetBlockedProviders().Select(v => v.ProviderId)); if (blockedProviders.Any()) { var nonBlockedProviders = availableProviders.Where(v => !blockedProviders.Contains(v.Definition.Id)).ToList(); if (nonBlockedProviders.Any()) { availableProviders = nonBlockedProviders; } else { _logger.Trace("No non-blocked Download Client available, retrying blocked one."); } } // Use the first priority clients first availableProviders = availableProviders.GroupBy(v => (v.Definition as DownloadClientDefinition).Priority) .OrderBy(v => v.Key) .First().OrderBy(v => v.Definition.Id).ToList(); var lastId = _lastUsedDownloadClient.Find(downloadProtocol.ToString()); var provider = availableProviders.FirstOrDefault(v => v.Definition.Id > lastId) ?? availableProviders.First(); _lastUsedDownloadClient.Set(downloadProtocol.ToString(), provider.Definition.Id); return(provider); }
public IDownloadClient GetDownloadClient(DownloadProtocol downloadProtocol) { var availableProviders = _downloadClientFactory.GetAvailableProviders().Where(v => v.Protocol == downloadProtocol).ToList(); if (!availableProviders.Any()) { return(null); } var blockedProviders = new HashSet <int>(_downloadClientStatusService.GetBlockedProviders().Select(v => v.ProviderId)); if (blockedProviders.Any()) { var nonBlockedProviders = availableProviders.Where(v => !blockedProviders.Contains(v.Definition.Id)).ToList(); if (nonBlockedProviders.Any()) { availableProviders = nonBlockedProviders; } else { _logger.Trace("No non-blocked Download Client available, retrying blocked one."); } } // Use the first priority clients first availableProviders = availableProviders.GroupBy(v => (v.Definition as DownloadClientDefinition).Priority) .OrderBy(v => v.Key) .First().OrderBy(v => v.Definition.Id).ToList(); var lastId = _lastUsedDownloadClient.Find(downloadProtocol.ToString()); var provider = availableProviders.FirstOrDefault(v => v.Definition.Id > lastId) ?? availableProviders.First(); _lastUsedDownloadClient.Set(downloadProtocol.ToString(), provider.Definition.Id); return(provider); }