From 2a44efaa42e17bbaea591fc602a93972d1cb9303 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 13 Oct 2013 13:52:57 -0400 Subject: fixes #585 - Use tmdb updates api for people --- .../Library/LibraryManager.cs | 52 ++------ .../Library/Validators/PeopleValidator.cs | 147 +++++++++++++++++++++ 2 files changed, 159 insertions(+), 40 deletions(-) create mode 100644 MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs (limited to 'MediaBrowser.Server.Implementations/Library') diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index e9c824198..b632792f6 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -15,7 +15,6 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.Library.Validators; using MediaBrowser.Server.Implementations.ScheduledTasks; -using MoreLinq; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -44,6 +43,12 @@ namespace MediaBrowser.Server.Implementations.Library /// The prescan tasks. private IEnumerable PrescanTasks { get; set; } + /// + /// Gets or sets the people prescan tasks. + /// + /// The people prescan tasks. + private IEnumerable PeoplePrescanTasks { get; set; } + /// /// Gets the intro providers. /// @@ -197,6 +202,7 @@ namespace MediaBrowser.Server.Implementations.Library /// The item comparers. /// The prescan tasks. /// The postscan tasks. + /// The people prescan tasks. /// The savers. public void AddParts(IEnumerable rules, IEnumerable pluginFolders, @@ -205,6 +211,7 @@ namespace MediaBrowser.Server.Implementations.Library IEnumerable itemComparers, IEnumerable prescanTasks, IEnumerable postscanTasks, + IEnumerable peoplePrescanTasks, IEnumerable savers) { EntityResolutionIgnoreRules = rules; @@ -214,6 +221,7 @@ namespace MediaBrowser.Server.Implementations.Library Comparers = itemComparers; PrescanTasks = prescanTasks; PostscanTasks = postscanTasks; + PeoplePrescanTasks = peoplePrescanTasks; _savers = savers; } @@ -771,45 +779,9 @@ namespace MediaBrowser.Server.Implementations.Library /// The cancellation token. /// The progress. /// Task. - public async Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) + public Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) { - var people = RootFolder.GetRecursiveChildren() - .SelectMany(c => c.People) - .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase) - .ToList(); - - var numComplete = 0; - - foreach (var person in people) - { - cancellationToken.ThrowIfCancellationRequested(); - - try - { - var item = GetPerson(person.Name); - - await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error validating IBN entry {0}", ex, person.Name); - } - - // Update progress - numComplete++; - double percent = numComplete; - percent /= people.Count; - - progress.Report(100 * percent); - } - - progress.Report(100); - - _logger.Info("People validation complete"); - - // Bad practice, i know. But we keep a lot in memory, unfortunately. - GC.Collect(2, GCCollectionMode.Forced, true); - GC.Collect(2, GCCollectionMode.Forced, true); + return new PeopleValidator(this, PeoplePrescanTasks, _logger).ValidatePeople(cancellationToken, progress); } /// @@ -1153,7 +1125,7 @@ namespace MediaBrowser.Server.Implementations.Library private Video ResolveIntro(IntroInfo info) { Video video = null; - + if (info.ItemId.HasValue) { // Get an existing item by Id diff --git a/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs new file mode 100644 index 000000000..9554290ed --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs @@ -0,0 +1,147 @@ +using MediaBrowser.Common.Progress; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Logging; +using MoreLinq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Server.Implementations.Library.Validators +{ + /// + /// Class PeopleValidator + /// + public class PeopleValidator + { + /// + /// The _library manager + /// + private readonly ILibraryManager _libraryManager; + /// + /// The _logger + /// + private readonly ILogger _logger; + + private readonly IEnumerable _prescanTasks; + + /// + /// Initializes a new instance of the class. + /// + /// The library manager. + /// The prescan tasks. + /// The logger. + public PeopleValidator(ILibraryManager libraryManager, IEnumerable prescanTasks, ILogger logger) + { + _libraryManager = libraryManager; + _logger = logger; + _prescanTasks = prescanTasks; + } + + /// + /// Validates the people. + /// + /// The cancellation token. + /// The progress. + /// Task. + public async Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) + { + var innerProgress = new ActionableProgress(); + + innerProgress.RegisterAction(pct => progress.Report(pct * .15)); + + // Run prescan tasks + await RunPrescanTasks(innerProgress, cancellationToken).ConfigureAwait(false); + + progress.Report(15); + + var people = _libraryManager.RootFolder.GetRecursiveChildren() + .SelectMany(c => c.People) + .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase) + .ToList(); + + var numComplete = 0; + + foreach (var person in people) + { + cancellationToken.ThrowIfCancellationRequested(); + + try + { + var item = _libraryManager.GetPerson(person.Name); + + await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error validating IBN entry {0}", ex, person.Name); + } + + // Update progress + numComplete++; + double percent = numComplete; + percent /= people.Count; + + progress.Report(15 + 85 * percent); + } + + progress.Report(100); + + _logger.Info("People validation complete"); + + // Bad practice, i know. But we keep a lot in memory, unfortunately. + GC.Collect(2, GCCollectionMode.Forced, true); + GC.Collect(2, GCCollectionMode.Forced, true); + } + + /// + /// Runs the prescan tasks. + /// + /// The progress. + /// The cancellation token. + /// Task. + private async Task RunPrescanTasks(IProgress progress, CancellationToken cancellationToken) + { + var tasks = _prescanTasks.ToList(); + + var numComplete = 0; + var numTasks = tasks.Count; + + foreach (var task in tasks) + { + var innerProgress = new ActionableProgress(); + + // Prevent access to modified closure + var currentNumComplete = numComplete; + + innerProgress.RegisterAction(pct => + { + double innerPercent = (currentNumComplete * 100) + pct; + innerPercent /= numTasks; + progress.Report(innerPercent); + }); + + try + { + await task.Run(innerProgress, cancellationToken); + } + catch (OperationCanceledException) + { + _logger.Info("Pre-scan task cancelled: {0}", task.GetType().Name); + } + catch (Exception ex) + { + _logger.ErrorException("Error running pre-scan task", ex); + } + + numComplete++; + double percent = numComplete; + percent /= numTasks; + progress.Report(percent * 100); + } + + progress.Report(100); + } + } +} -- cgit v1.2.3