PeopleValidator.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Logging;
  5. using MoreLinq;
  6. using System;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Server.Implementations.Library.Validators
  11. {
  12. /// <summary>
  13. /// Class PeopleValidator
  14. /// </summary>
  15. public class PeopleValidator
  16. {
  17. /// <summary>
  18. /// The _library manager
  19. /// </summary>
  20. private readonly ILibraryManager _libraryManager;
  21. /// <summary>
  22. /// The _logger
  23. /// </summary>
  24. private readonly ILogger _logger;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
  27. /// </summary>
  28. /// <param name="libraryManager">The library manager.</param>
  29. /// <param name="logger">The logger.</param>
  30. public PeopleValidator(ILibraryManager libraryManager, ILogger logger)
  31. {
  32. _libraryManager = libraryManager;
  33. _logger = logger;
  34. }
  35. /// <summary>
  36. /// Validates the people.
  37. /// </summary>
  38. /// <param name="cancellationToken">The cancellation token.</param>
  39. /// <param name="options">The options.</param>
  40. /// <param name="progress">The progress.</param>
  41. /// <returns>Task.</returns>
  42. public async Task ValidatePeople(CancellationToken cancellationToken, MetadataRefreshOptions options, IProgress<double> progress)
  43. {
  44. var innerProgress = new ActionableProgress<double>();
  45. innerProgress.RegisterAction(pct => progress.Report(pct * .15));
  46. var people = _libraryManager.RootFolder.GetRecursiveChildren()
  47. .SelectMany(c => c.People)
  48. .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
  49. .ToList();
  50. var numComplete = 0;
  51. foreach (var person in people)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. try
  55. {
  56. var item = _libraryManager.GetPerson(person.Name);
  57. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  58. }
  59. catch (Exception ex)
  60. {
  61. _logger.ErrorException("Error validating IBN entry {0}", ex, person.Name);
  62. }
  63. // Update progress
  64. numComplete++;
  65. double percent = numComplete;
  66. percent /= people.Count;
  67. progress.Report(15 + 85 * percent);
  68. }
  69. progress.Report(100);
  70. _logger.Info("People validation complete");
  71. // Bad practice, i know. But we keep a lot in memory, unfortunately.
  72. GC.Collect(2, GCCollectionMode.Forced, true);
  73. GC.Collect(2, GCCollectionMode.Forced, true);
  74. }
  75. }
  76. }