PeopleValidator.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. .Where(i => !string.IsNullOrWhiteSpace(i.Name))
  49. .Select(i => i.Name)
  50. .Distinct(StringComparer.OrdinalIgnoreCase)
  51. .ToList();
  52. var numComplete = 0;
  53. foreach (var person in people)
  54. {
  55. cancellationToken.ThrowIfCancellationRequested();
  56. try
  57. {
  58. var item = _libraryManager.GetPerson(person);
  59. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.ErrorException("Error validating IBN entry {0}", ex, person);
  64. }
  65. // Update progress
  66. numComplete++;
  67. double percent = numComplete;
  68. percent /= people.Count;
  69. progress.Report(15 + 85 * percent);
  70. }
  71. progress.Report(100);
  72. _logger.Info("People validation complete");
  73. // Bad practice, i know. But we keep a lot in memory, unfortunately.
  74. GC.Collect(2, GCCollectionMode.Forced, true);
  75. GC.Collect(2, GCCollectionMode.Forced, true);
  76. }
  77. }
  78. }