diff --git a/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs b/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs index c0c486aa4..ab8760a30 100644 --- a/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs +++ b/src/NzbDrone.Core.Test/ImportListTests/ImportListSyncServiceFixture.cs @@ -48,6 +48,14 @@ namespace NzbDrone.Core.Test.ImportListTests Mocker.GetMock() .Setup(v => v.All()) .Returns(new List()); + + Mocker.GetMock() + .Setup(v => v.AddArtists(It.IsAny>(), false)) + .Returns((List artists, bool doRefresh) => artists); + + Mocker.GetMock() + .Setup(v => v.AddAlbums(It.IsAny>())) + .Returns((List albums) => albums); } private void WithAlbum() @@ -176,7 +184,7 @@ namespace NzbDrone.Core.Test.ImportListTests Subject.Execute(new ImportListSyncCommand()); Mocker.GetMock() - .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0))); + .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0), false)); } [Test] @@ -188,7 +196,7 @@ namespace NzbDrone.Core.Test.ImportListTests Subject.Execute(new ImportListSyncCommand()); Mocker.GetMock() - .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0))); + .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0), false)); } [Test] @@ -214,7 +222,7 @@ namespace NzbDrone.Core.Test.ImportListTests Subject.Execute(new ImportListSyncCommand()); Mocker.GetMock() - .Verify(v => v.AddArtists(It.Is>(t => t.Count == 1 && t.First().Monitored == expectedArtistMonitored))); + .Verify(v => v.AddArtists(It.Is>(t => t.Count == 1 && t.First().Monitored == expectedArtistMonitored), false)); } [TestCase(ImportListMonitorType.None, false)] @@ -240,7 +248,7 @@ namespace NzbDrone.Core.Test.ImportListTests Subject.Execute(new ImportListSyncCommand()); Mocker.GetMock() - .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0))); + .Verify(v => v.AddArtists(It.Is>(t => t.Count == 0), false)); } [Test] diff --git a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs index 38e6a1cae..02fc1281b 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListSyncService.cs @@ -8,6 +8,7 @@ using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.MetadataSource; using NzbDrone.Core.Music; +using NzbDrone.Core.Music.Commands; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.ImportLists @@ -24,6 +25,7 @@ namespace NzbDrone.Core.ImportLists private readonly IAddArtistService _addArtistService; private readonly IAddAlbumService _addAlbumService; private readonly IEventAggregator _eventAggregator; + private readonly IManageCommandQueue _commandQueueManager; private readonly Logger _logger; public ImportListSyncService(IImportListFactory importListFactory, @@ -36,6 +38,7 @@ namespace NzbDrone.Core.ImportLists IAddArtistService addArtistService, IAddAlbumService addAlbumService, IEventAggregator eventAggregator, + IManageCommandQueue commandQueueManager, Logger logger) { _importListFactory = importListFactory; @@ -48,6 +51,7 @@ namespace NzbDrone.Core.ImportLists _addArtistService = addArtistService; _addAlbumService = addAlbumService; _eventAggregator = eventAggregator; + _commandQueueManager = commandQueueManager; _logger = logger; } @@ -113,13 +117,19 @@ namespace NzbDrone.Core.ImportLists } } - _addArtistService.AddArtists(artistsToAdd); - _addAlbumService.AddAlbums(albumsToAdd); + var addedArtists = _addArtistService.AddArtists(artistsToAdd, false); + var addedAlbums = _addAlbumService.AddAlbums(albumsToAdd); - var message = string.Format($"Import List Sync Completed. Items found: {reports.Count}, Artists added: {artistsToAdd.Count}, Albums added: {albumsToAdd.Count}"); + var message = string.Format($"Import List Sync Completed. Items found: {reports.Count}, Artists added: {addedArtists.Count}, Albums added: {addedAlbums.Count}"); _logger.ProgressInfo(message); + var toRefresh = addedArtists.Select(x => x.Id).Concat(addedAlbums.Select(x => x.Artist.Value.Id)).Distinct().ToList(); + if (toRefresh.Any()) + { + _commandQueueManager.Push(new BulkRefreshArtistCommand(toRefresh, true)); + } + return processed; } diff --git a/src/NzbDrone.Core/Music/Events/ArtistsImportedEvent.cs b/src/NzbDrone.Core/Music/Events/ArtistsImportedEvent.cs index 0b3833b15..cf1b7786e 100644 --- a/src/NzbDrone.Core/Music/Events/ArtistsImportedEvent.cs +++ b/src/NzbDrone.Core/Music/Events/ArtistsImportedEvent.cs @@ -6,10 +6,12 @@ namespace NzbDrone.Core.Music.Events public class ArtistsImportedEvent : IEvent { public List ArtistIds { get; private set; } + public bool DoRefresh { get; private set; } - public ArtistsImportedEvent(List artistIds) + public ArtistsImportedEvent(List artistIds, bool doRefresh = true) { ArtistIds = artistIds; + DoRefresh = doRefresh; } } } diff --git a/src/NzbDrone.Core/Music/Handlers/ArtistAddedHandler.cs b/src/NzbDrone.Core/Music/Handlers/ArtistAddedHandler.cs index a7cad3030..a72a39a63 100644 --- a/src/NzbDrone.Core/Music/Handlers/ArtistAddedHandler.cs +++ b/src/NzbDrone.Core/Music/Handlers/ArtistAddedHandler.cs @@ -1,4 +1,3 @@ -using System.Linq; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Music.Commands; @@ -26,7 +25,10 @@ namespace NzbDrone.Core.Music public void Handle(ArtistsImportedEvent message) { - _commandQueueManager.PushMany(message.ArtistIds.Select(s => new RefreshArtistCommand(s, true)).ToList()); + if (message.DoRefresh) + { + _commandQueueManager.Push(new BulkRefreshArtistCommand(message.ArtistIds, true)); + } } } } diff --git a/src/NzbDrone.Core/Music/Services/AddArtistService.cs b/src/NzbDrone.Core/Music/Services/AddArtistService.cs index 7a7bf033d..2abf43199 100644 --- a/src/NzbDrone.Core/Music/Services/AddArtistService.cs +++ b/src/NzbDrone.Core/Music/Services/AddArtistService.cs @@ -17,7 +17,7 @@ namespace NzbDrone.Core.Music public interface IAddArtistService { Artist AddArtist(Artist newArtist, bool doRefresh = true); - List AddArtists(List newArtists); + List AddArtists(List newArtists, bool doRefresh = true); } public class AddArtistService : IAddArtistService @@ -63,7 +63,7 @@ namespace NzbDrone.Core.Music return newArtist; } - public List AddArtists(List newArtists) + public List AddArtists(List newArtists, bool doRefresh = true) { var added = DateTime.UtcNow; var artistsToAdd = new List(); @@ -88,7 +88,7 @@ namespace NzbDrone.Core.Music _artistMetadataService.UpsertMany(artistsToAdd.Select(x => x.Metadata.Value).ToList()); artistsToAdd.ForEach(x => x.ArtistMetadataId = x.Metadata.Value.Id); - return _artistService.AddArtists(artistsToAdd); + return _artistService.AddArtists(artistsToAdd, doRefresh); } private Artist AddSkyhookData(Artist newArtist) diff --git a/src/NzbDrone.Core/Music/Services/ArtistService.cs b/src/NzbDrone.Core/Music/Services/ArtistService.cs index f69206f1b..131920196 100644 --- a/src/NzbDrone.Core/Music/Services/ArtistService.cs +++ b/src/NzbDrone.Core/Music/Services/ArtistService.cs @@ -16,7 +16,7 @@ namespace NzbDrone.Core.Music Artist GetArtistByMetadataId(int artistMetadataId); List GetArtists(IEnumerable artistIds); Artist AddArtist(Artist newArtist, bool doRefresh); - List AddArtists(List newArtists); + List AddArtists(List newArtists, bool doRefresh); Artist FindById(string foreignArtistId); Artist FindByName(string title); Artist FindByNameInexact(string title); @@ -60,11 +60,11 @@ namespace NzbDrone.Core.Music return newArtist; } - public List AddArtists(List newArtists) + public List AddArtists(List newArtists, bool doRefresh) { _cache.Clear(); _artistRepository.InsertMany(newArtists); - _eventAggregator.PublishEvent(new ArtistsImportedEvent(newArtists.Select(s => s.Id).ToList())); + _eventAggregator.PublishEvent(new ArtistsImportedEvent(newArtists.Select(s => s.Id).ToList(), doRefresh)); return newArtists; }