New: Sync Lists on Add and Update (#342)

pull/359/head
Qstick 6 years ago committed by GitHub
parent ad6e651090
commit 5a8e79eec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,7 @@ namespace NzbDrone.Core.ImportLists
public interface IFetchAndParseImportList
{
List<ImportListItemInfo> Fetch();
List<ImportListItemInfo> FetchSingleList(ImportListDefinition definition);
}
public class FetchAndParseImportListService : IFetchAndParseImportList
@ -76,5 +77,51 @@ namespace NzbDrone.Core.ImportLists
return result;
}
public List<ImportListItemInfo> FetchSingleList(ImportListDefinition definition)
{
var result = new List<ImportListItemInfo>();
var importList = _importListFactory.GetInstance(definition);
if (importList == null || !definition.EnableAutomaticAdd)
{
_logger.Warn("No available import lists. check your configuration.");
return result;
}
var taskList = new List<Task>();
var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
var importListLocal = importList;
var task = taskFactory.StartNew(() =>
{
try
{
var importListReports = importListLocal.Fetch();
lock (result)
{
_logger.Debug("Found {0} from {1}", importListReports.Count, importList.Name);
result.AddRange(importListReports);
}
}
catch (Exception e)
{
_logger.Error(e, "Error during Import List Sync");
}
}).LogExceptions();
taskList.Add(task);
Task.WaitAll(taskList.ToArray());
result = result.DistinctBy(r => new { r.Artist, r.Album }).ToList();
return result;
}
}
}

@ -4,6 +4,19 @@ namespace NzbDrone.Core.ImportLists
{
public class ImportListSyncCommand : Command
{
public int? DefinitionId { get; set; }
public ImportListSyncCommand()
{
}
public ImportListSyncCommand(int? definition)
{
DefinitionId = definition;
}
public override bool SendUpdatesToClient => true;
public override bool UpdateScheduledTask => !DefinitionId.HasValue;
}
}

@ -7,6 +7,7 @@ using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.MetadataSource;
using NzbDrone.Core.Music;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists
{
@ -44,13 +45,32 @@ namespace NzbDrone.Core.ImportLists
}
private List<Album> Sync()
private List<Album> SyncAll()
{
_logger.ProgressInfo("Starting Import List Sync");
var rssReleases = _listFetcherAndParser.Fetch();
var reports = rssReleases.ToList();
return ProcessReports(reports);
}
private List<Album> SyncList(ImportListDefinition definition)
{
_logger.ProgressInfo(string.Format("Starting Import List Refresh for List {0}", definition.Name));
var rssReleases = _listFetcherAndParser.FetchSingleList(definition);
var reports = rssReleases.ToList();
return ProcessReports(reports);
}
private List<Album> ProcessReports(List<ImportListItemInfo> reports)
{
var processed = new List<Album>();
var artistsToAdd = new List<Artist>();
@ -106,19 +126,19 @@ namespace NzbDrone.Core.ImportLists
LanguageProfileId = importList.LanguageProfileId,
MetadataProfileId = importList.MetadataProfileId,
AlbumFolder = true,
AddOptions = new AddArtistOptions{SearchForMissingAlbums = true, Monitored = importList.ShouldMonitor, SelectedOption = 0}
AddOptions = new AddArtistOptions { SearchForMissingAlbums = true, Monitored = importList.ShouldMonitor, SelectedOption = 0 }
});
}
// Add Album so we know what to monitor
if (report.AlbumMusicBrainzId.IsNotNullOrWhiteSpace() && artistsToAdd.Any(s=>s.ForeignArtistId == report.ArtistMusicBrainzId) && importList.ShouldMonitor)
if (report.AlbumMusicBrainzId.IsNotNullOrWhiteSpace() && artistsToAdd.Any(s => s.ForeignArtistId == report.ArtistMusicBrainzId) && importList.ShouldMonitor)
{
artistsToAdd.Find(s => s.ForeignArtistId == report.ArtistMusicBrainzId).AddOptions.AlbumsToMonitor.Add(report.AlbumMusicBrainzId);
}
}
_addArtistService.AddArtists(artistsToAdd);
var message = string.Format("Import List Sync Completed. Reports found: {0}, Reports grabbed: {1}", reports.Count, processed.Count);
_logger.ProgressInfo(message);
@ -128,7 +148,16 @@ namespace NzbDrone.Core.ImportLists
public void Execute(ImportListSyncCommand message)
{
var processed = Sync();
List<Album> processed;
if (message.DefinitionId.HasValue)
{
processed = SyncList(_importListFactory.Get(message.DefinitionId.Value));
}
else
{
processed = SyncAll();
}
_eventAggregator.PublishEvent(new ImportListSyncCompleteEvent(processed));
}

@ -0,0 +1,26 @@
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.ImportLists
{
public class ImportListUpdatedHandler : IHandle<ProviderUpdatedEvent<IImportList>>, IHandle<ProviderAddedEvent<IImportList>>
{
private readonly IManageCommandQueue _commandQueueManager;
public ImportListUpdatedHandler(IManageCommandQueue commandQueueManager)
{
_commandQueueManager = commandQueueManager;
}
public void Handle(ProviderUpdatedEvent<IImportList> message)
{
_commandQueueManager.Push(new ImportListSyncCommand(message.Definition.Id));
}
public void Handle(ProviderAddedEvent<IImportList> message)
{
_commandQueueManager.Push(new ImportListSyncCommand(message.Definition.Id));
}
}
}

@ -547,6 +547,7 @@
<Compile Include="ImportLists\ImportListBase.cs" />
<Compile Include="ImportLists\ImportListPageableRequestChain.cs" />
<Compile Include="ImportLists\ImportListPageableRequest.cs" />
<Compile Include="ImportLists\ImportListUpdatedHandler.cs" />
<Compile Include="ImportLists\IProcessImportListResponse.cs" />
<Compile Include="ImportLists\ImportListSyncService.cs" />
<Compile Include="ImportLists\ImportListSyncCompleteEvent.cs" />
@ -1111,6 +1112,7 @@
<Compile Include="ThingiProvider\ConfigContractNotFoundException.cs" />
<Compile Include="ThingiProvider\Events\ProviderDeletedEvent.cs" />
<Compile Include="ThingiProvider\Events\ProviderStatusChangedEvent.cs" />
<Compile Include="ThingiProvider\Events\ProviderAddedEvent.cs" />
<Compile Include="ThingiProvider\Events\ProviderUpdatedEvent.cs" />
<Compile Include="ThingiProvider\IProvider.cs" />
<Compile Include="ThingiProvider\IProviderConfig.cs" />

@ -0,0 +1,14 @@
using NzbDrone.Common.Messaging;
namespace NzbDrone.Core.ThingiProvider.Events
{
public class ProviderAddedEvent<TProvider> : IEvent
{
public ProviderDefinition Definition { get; private set; }
public ProviderAddedEvent(ProviderDefinition definition)
{
Definition = definition;
}
}
}

@ -98,7 +98,9 @@ namespace NzbDrone.Core.ThingiProvider
public virtual TProviderDefinition Create(TProviderDefinition definition)
{
return _providerRepository.Insert(definition);
var addedDefinition = _providerRepository.Insert(definition);
_eventAggregator.PublishEvent(new ProviderAddedEvent<TProvider>(definition));
return addedDefinition;
}
public virtual void Update(TProviderDefinition definition)

Loading…
Cancel
Save