2017-10-27 03:21:06 +00:00
using System.Collections.Generic ;
2014-02-14 05:31:49 +00:00
using System.Linq ;
2017-10-27 03:21:06 +00:00
using FluentValidation.Results ;
2014-02-14 05:31:49 +00:00
using NLog ;
using NzbDrone.Common.Composition ;
2014-02-26 05:40:47 +00:00
using NzbDrone.Core.Messaging.Events ;
2014-02-14 05:31:49 +00:00
using NzbDrone.Core.ThingiProvider ;
namespace NzbDrone.Core.Download
{
public interface IDownloadClientFactory : IProviderFactory < IDownloadClient , DownloadClientDefinition >
{
2017-10-27 03:21:06 +00:00
List < IDownloadClient > DownloadHandlingEnabled ( bool filterBlockedClients = true ) ;
2014-02-14 05:31:49 +00:00
}
public class DownloadClientFactory : ProviderFactory < IDownloadClient , DownloadClientDefinition > , IDownloadClientFactory
{
2017-10-27 03:21:06 +00:00
private readonly IDownloadClientStatusService _downloadClientStatusService ;
private readonly Logger _logger ;
2014-02-14 05:31:49 +00:00
2017-10-27 03:21:06 +00:00
public DownloadClientFactory ( IDownloadClientStatusService downloadClientStatusService ,
IDownloadClientRepository providerRepository ,
IEnumerable < IDownloadClient > providers ,
IContainer container ,
IEventAggregator eventAggregator ,
Logger logger )
2014-02-26 05:40:47 +00:00
: base ( providerRepository , providers , container , eventAggregator , logger )
2014-02-14 05:31:49 +00:00
{
2017-10-27 03:21:06 +00:00
_downloadClientStatusService = downloadClientStatusService ;
_logger = logger ;
2014-02-14 05:31:49 +00:00
}
2014-04-19 15:09:22 +00:00
protected override List < DownloadClientDefinition > Active ( )
{
return base . Active ( ) . Where ( c = > c . Enable ) . ToList ( ) ;
}
2016-03-25 00:56:29 +00:00
public override void SetProviderCharacteristics ( IDownloadClient provider , DownloadClientDefinition definition )
2014-02-14 05:31:49 +00:00
{
2016-03-25 00:56:29 +00:00
base . SetProviderCharacteristics ( provider , definition ) ;
2014-04-19 15:09:22 +00:00
definition . Protocol = provider . Protocol ;
2014-02-14 05:31:49 +00:00
}
2017-10-27 03:21:06 +00:00
public List < IDownloadClient > DownloadHandlingEnabled ( bool filterBlockedClients = true )
{
var enabledClients = GetAvailableProviders ( ) ;
if ( filterBlockedClients )
{
return FilterBlockedClients ( enabledClients ) . ToList ( ) ;
}
return enabledClients . ToList ( ) ;
}
private IEnumerable < IDownloadClient > FilterBlockedClients ( IEnumerable < IDownloadClient > clients )
{
var blockedIndexers = _downloadClientStatusService . GetBlockedProviders ( ) . ToDictionary ( v = > v . ProviderId , v = > v ) ;
foreach ( var client in clients )
{
DownloadClientStatus downloadClientStatus ;
if ( blockedIndexers . TryGetValue ( client . Definition . Id , out downloadClientStatus ) )
{
_logger . Debug ( "Temporarily ignoring download client {0} till {1} due to recent failures." , client . Definition . Name , downloadClientStatus . DisabledTill . Value . ToLocalTime ( ) ) ;
continue ;
}
yield return client ;
}
}
public override ValidationResult Test ( DownloadClientDefinition definition )
{
var result = base . Test ( definition ) ;
if ( ( result = = null | | result . IsValid ) & & definition . Id ! = 0 )
{
_downloadClientStatusService . RecordSuccess ( definition . Id ) ;
}
return result ;
}
2014-02-14 05:31:49 +00:00
}
2017-10-27 03:21:06 +00:00
}