PeopleValidator.cs 2.8 KB

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