@ -21,12 +21,13 @@ namespace NzbDrone.Core.Download.Pending
public interface IPendingReleaseService
public interface IPendingReleaseService
{
{
void Add ( DownloadDecision decision , PendingReleaseReason reason ) ;
void Add ( DownloadDecision decision , PendingReleaseReason reason ) ;
void AddMany ( List < Tuple < DownloadDecision , PendingReleaseReason > > decisions ) ;
List < ReleaseInfo > GetPending ( ) ;
List < ReleaseInfo > GetPending ( ) ;
List < RemoteAlbum > GetPendingRemoteAlbums ( int artistId ) ;
List < RemoteAlbum > GetPendingRemoteAlbums ( int artistId ) ;
List < Queue . Queue > GetPendingQueue ( ) ;
List < Queue . Queue > GetPendingQueue ( ) ;
Queue . Queue FindPendingQueueItem ( int queueId ) ;
Queue . Queue FindPendingQueueItem ( int queueId ) ;
void RemovePendingQueueItems ( int queueId ) ;
void RemovePendingQueueItems ( int queueId ) ;
RemoteAlbum OldestPendingRelease ( int artistId , IEnumerable < int > albumIds ) ;
RemoteAlbum OldestPendingRelease ( int artistId , int [ ] albumIds ) ;
}
}
public class PendingReleaseService : IPendingReleaseService ,
public class PendingReleaseService : IPendingReleaseService ,
@ -68,8 +69,27 @@ namespace NzbDrone.Core.Download.Pending
public void Add ( DownloadDecision decision , PendingReleaseReason reason )
public void Add ( DownloadDecision decision , PendingReleaseReason reason )
{
{
var alreadyPending = GetPendingReleases( ) ;
var alreadyPending = _repository. AllByArtistId ( decision . RemoteAlbum . Artist . Id ) ;
alreadyPending = IncludeRemoteAlbums ( alreadyPending ) ;
Add ( alreadyPending , decision , reason ) ;
}
public void AddMany ( List < Tuple < DownloadDecision , PendingReleaseReason > > decisions )
{
var alreadyPending = decisions . Select ( v = > v . Item1 . RemoteAlbum . Artist . Id ) . Distinct ( ) . SelectMany ( _repository . AllByArtistId ) . ToList ( ) ;
alreadyPending = IncludeRemoteAlbums ( alreadyPending ) ;
foreach ( var pair in decisions )
{
Add ( alreadyPending , pair . Item1 , pair . Item2 ) ;
}
}
private void Add ( List < PendingRelease > alreadyPending , DownloadDecision decision , PendingReleaseReason reason )
{
var albumIds = decision . RemoteAlbum . Albums . Select ( e = > e . Id ) ;
var albumIds = decision . RemoteAlbum . Albums . Select ( e = > e . Id ) ;
var existingReports = alreadyPending . Where ( r = > r . RemoteAlbum . Albums . Select ( e = > e . Id )
var existingReports = alreadyPending . Where ( r = > r . RemoteAlbum . Albums . Select ( e = > e . Id )
@ -80,22 +100,32 @@ namespace NzbDrone.Core.Download.Pending
if ( matchingReports . Any ( ) )
if ( matchingReports . Any ( ) )
{
{
var sameReason = true ;
var matchingReport = matchingReports . First ( ) ;
foreach ( var matchingReport in matchingReports )
{
if ( matchingReport . Reason ! = reason )
if ( matchingReport . Reason ! = reason )
{
{
_logger . Debug ( "The release {0} is already pending with reason {1}, changing to {2}" , decision . RemoteAlbum , matchingReport . Reason , reason ) ; matchingReport . Reason = reason ;
_logger . Debug ( "The release {0} is already pending with reason {1}, changing to {2}" , decision . RemoteAlbum , matchingReport . Reason , reason ) ;
matchingReport . Reason = reason ;
_repository . Update ( matchingReport ) ;
_repository . Update ( matchingReport ) ;
sameReason = false ;
}
}
}
if ( sameReason )
else
{
{
_logger . Debug ( "The release {0} is already pending with reason {1}, not adding again" , decision . RemoteAlbum , reason ) ; return ;
_logger . Debug ( "The release {0} is already pending with reason {1}, not adding again" , decision . RemoteAlbum , reason ) ; return ;
}
}
if ( matchingReports . Count ( ) > 1 )
{
_logger . Debug ( "The release {0} had {1} duplicate pending, removing duplicates." , decision . RemoteAlbum , matchingReports . Count ( ) - 1 ) ;
foreach ( var duplicate in matchingReports . Skip ( 1 ) )
{
_repository . Delete ( duplicate . Id ) ;
alreadyPending . Remove ( duplicate ) ;
}
}
return ;
}
}
_logger . Debug ( "Adding release {0} to pending releases with reason {1}" , decision . RemoteAlbum , reason ) ; Insert ( decision , reason ) ;
_logger . Debug ( "Adding release {0} to pending releases with reason {1}" , decision . RemoteAlbum , reason ) ; Insert ( decision , reason ) ;
@ -122,7 +152,7 @@ namespace NzbDrone.Core.Download.Pending
public List < RemoteAlbum > GetPendingRemoteAlbums ( int artistId )
public List < RemoteAlbum > GetPendingRemoteAlbums ( int artistId )
{
{
return _repository. AllByArtistId ( artistId ) .Select ( Get RemoteAlbum) . ToList ( ) ;
return IncludeRemoteAlbums( _repository. AllByArtistId ( artistId ) ). Select ( v = > v . RemoteAlbum) . ToList ( ) ;
}
}
public List < Queue . Queue > GetPendingQueue ( )
public List < Queue . Queue > GetPendingQueue ( )
@ -131,7 +161,8 @@ namespace NzbDrone.Core.Download.Pending
var nextRssSync = new Lazy < DateTime > ( ( ) = > _taskManager . GetNextExecution ( typeof ( RssSyncCommand ) ) ) ;
var nextRssSync = new Lazy < DateTime > ( ( ) = > _taskManager . GetNextExecution ( typeof ( RssSyncCommand ) ) ) ;
foreach ( var pendingRelease in GetPendingReleases ( ) . Where ( p = > p . Reason ! = PendingReleaseReason . Fallback ) )
var pendingReleases = IncludeRemoteAlbums ( _repository . WithoutFallback ( ) ) ;
foreach ( var pendingRelease in pendingReleases )
{
{
foreach ( var album in pendingRelease . RemoteAlbum . Albums )
foreach ( var album in pendingRelease . RemoteAlbum . Albums )
{
{
@ -203,53 +234,53 @@ namespace NzbDrone.Core.Download.Pending
_repository . DeleteMany ( releasesToRemove . Select ( c = > c . Id ) ) ;
_repository . DeleteMany ( releasesToRemove . Select ( c = > c . Id ) ) ;
}
}
public RemoteAlbum OldestPendingRelease ( int artistId , IEnumerable < int > albumIds )
public RemoteAlbum OldestPendingRelease ( int artistId , int [ ] albumIds )
{
{
return GetPendingRemoteAlbums ( artistId ) . Where ( r = > r . Albums . Select ( e = > e . Id ) . Intersect ( albumIds ) . Any ( ) )
var artistReleases = GetPendingReleases ( artistId ) ;
return artistReleases . Select ( r = > r . RemoteAlbum )
. Where ( r = > r . Albums . Select ( e = > e . Id ) . Intersect ( albumIds ) . Any ( ) )
. OrderByDescending ( p = > p . Release . AgeHours )
. OrderByDescending ( p = > p . Release . AgeHours )
. FirstOrDefault ( ) ;
. FirstOrDefault ( ) ;
}
}
private List < PendingRelease > GetPendingReleases ( )
private List < PendingRelease > GetPendingReleases ( )
{
{
var result = new List < PendingRelease > ( ) ;
return IncludeRemoteAlbums ( _repository . All ( ) . ToList ( ) ) ;
foreach ( var release in _repository . All ( ) )
{
var remoteAlbum = GetRemoteAlbum ( release ) ;
if ( remoteAlbum = = null )
{
continue ;
}
release . RemoteAlbum = remoteAlbum ;
result . Add ( release ) ;
}
}
return result ;
private List < PendingRelease > GetPendingReleases ( int artistId )
{
return IncludeRemoteAlbums ( _repository . AllByArtistId ( artistId ) . ToList ( ) ) ;
}
}
private RemoteAlbum GetRemoteAlbum ( PendingRelease release )
private List < PendingRelease > IncludeRemoteAlbums ( List < PendingRelease > releases )
{
{
var artist = _artistService . GetArtist ( release . ArtistId ) ;
var result = new List < PendingRelease > ( ) ;
var artistMap = _artistService . GetArtists ( releases . Select ( v = > v . ArtistId ) . Distinct ( ) )
. ToDictionary ( v = > v . Id ) ;
//Just in case the series was removed, but wasn't cleaned up yet (housekeeper will clean it up)
foreach ( var release in releases )
if ( artist = = null )
{
{
return null ;
var artist = artistMap . GetValueOrDefault ( release . ArtistId ) ;
}
// Just in case the artist was removed, but wasn't cleaned up yet (housekeeper will clean it up)
if ( artist = = null ) return null ;
var album = _parsingService . GetAlbums ( release . ParsedAlbumInfo , artist ) ;
var album s = _parsingService . GetAlbums ( release . ParsedAlbumInfo , artist ) ;
return new RemoteAlbum
release . RemoteAlbum = new RemoteAlbum
{
{
Artist = artist ,
Artist = artist ,
Albums = album ,
Albums = album s ,
ParsedAlbumInfo = release . ParsedAlbumInfo ,
ParsedAlbumInfo = release . ParsedAlbumInfo ,
Release = release . Release
Release = release . Release
} ;
} ;
result . Add ( release ) ;
}
return result ;
}
}
private void Insert ( DownloadDecision decision , PendingReleaseReason reason )
private void Insert ( DownloadDecision decision , PendingReleaseReason reason )
@ -291,7 +322,7 @@ namespace NzbDrone.Core.Download.Pending
private void RemoveGrabbed ( RemoteAlbum remoteAlbum )
private void RemoveGrabbed ( RemoteAlbum remoteAlbum )
{
{
var pendingReleases = GetPendingReleases ( ) ;
var pendingReleases = GetPendingReleases ( remoteAlbum . Artist . Id ) ;
var albumIds = remoteAlbum . Albums . Select ( e = > e . Id ) ;
var albumIds = remoteAlbum . Albums . Select ( e = > e . Id ) ;
var existingReports = pendingReleases . Where ( r = > r . RemoteAlbum . Albums . Select ( e = > e . Id )
var existingReports = pendingReleases . Where ( r = > r . RemoteAlbum . Albums . Select ( e = > e . Id )