using MediaBrowser.Common.Progress;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Logging;
using MoreLinq;
using System;
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;
///
/// Initializes a new instance of the class.
///
/// The library manager.
/// The logger.
public PeopleValidator(ILibraryManager libraryManager, ILogger logger)
{
_libraryManager = libraryManager;
_logger = logger;
}
///
/// Validates the people.
///
/// The cancellation token.
/// The options.
/// The progress.
/// Task.
public async Task ValidatePeople(CancellationToken cancellationToken, MetadataRefreshOptions options, IProgress progress)
{
var innerProgress = new ActionableProgress();
innerProgress.RegisterAction(pct => progress.Report(pct * .15));
var people = _libraryManager.RootFolder.GetRecursiveChildren()
.SelectMany(c => c.People)
.Where(i => !string.IsNullOrWhiteSpace(i.Name))
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
var numComplete = 0;
foreach (var person in people)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var item = _libraryManager.GetPerson(person);
await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error validating IBN entry {0}", ex, person);
}
// 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);
}
}
}