PeopleValidator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Logging;
  4. using MoreLinq;
  5. using System;
  6. using System.Collections.Generic;
  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. private readonly IEnumerable<IPeoplePrescanTask> _prescanTasks;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
  28. /// </summary>
  29. /// <param name="libraryManager">The library manager.</param>
  30. /// <param name="prescanTasks">The prescan tasks.</param>
  31. /// <param name="logger">The logger.</param>
  32. public PeopleValidator(ILibraryManager libraryManager, IEnumerable<IPeoplePrescanTask> prescanTasks, ILogger logger)
  33. {
  34. _libraryManager = libraryManager;
  35. _logger = logger;
  36. _prescanTasks = prescanTasks;
  37. }
  38. /// <summary>
  39. /// Validates the people.
  40. /// </summary>
  41. /// <param name="cancellationToken">The cancellation token.</param>
  42. /// <param name="progress">The progress.</param>
  43. /// <returns>Task.</returns>
  44. public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
  45. {
  46. var innerProgress = new ActionableProgress<double>();
  47. innerProgress.RegisterAction(pct => progress.Report(pct * .15));
  48. // Run prescan tasks
  49. await RunPrescanTasks(innerProgress, cancellationToken).ConfigureAwait(false);
  50. progress.Report(15);
  51. var people = _libraryManager.RootFolder.GetRecursiveChildren()
  52. .SelectMany(c => c.People)
  53. .DistinctBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
  54. .ToList();
  55. var numComplete = 0;
  56. foreach (var person in people)
  57. {
  58. cancellationToken.ThrowIfCancellationRequested();
  59. try
  60. {
  61. var item = _libraryManager.GetPerson(person.Name);
  62. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  63. }
  64. catch (Exception ex)
  65. {
  66. _logger.ErrorException("Error validating IBN entry {0}", ex, person.Name);
  67. }
  68. // Update progress
  69. numComplete++;
  70. double percent = numComplete;
  71. percent /= people.Count;
  72. progress.Report(15 + 85 * percent);
  73. }
  74. progress.Report(100);
  75. _logger.Info("People validation complete");
  76. // Bad practice, i know. But we keep a lot in memory, unfortunately.
  77. GC.Collect(2, GCCollectionMode.Forced, true);
  78. GC.Collect(2, GCCollectionMode.Forced, true);
  79. }
  80. /// <summary>
  81. /// Runs the prescan tasks.
  82. /// </summary>
  83. /// <param name="progress">The progress.</param>
  84. /// <param name="cancellationToken">The cancellation token.</param>
  85. /// <returns>Task.</returns>
  86. private async Task RunPrescanTasks(IProgress<double> progress, CancellationToken cancellationToken)
  87. {
  88. var tasks = _prescanTasks.ToList();
  89. var numComplete = 0;
  90. var numTasks = tasks.Count;
  91. foreach (var task in tasks)
  92. {
  93. var innerProgress = new ActionableProgress<double>();
  94. // Prevent access to modified closure
  95. var currentNumComplete = numComplete;
  96. innerProgress.RegisterAction(pct =>
  97. {
  98. double innerPercent = (currentNumComplete * 100) + pct;
  99. innerPercent /= numTasks;
  100. progress.Report(innerPercent);
  101. });
  102. try
  103. {
  104. await task.Run(innerProgress, cancellationToken);
  105. }
  106. catch (OperationCanceledException)
  107. {
  108. _logger.Info("Pre-scan task cancelled: {0}", task.GetType().Name);
  109. }
  110. catch (Exception ex)
  111. {
  112. _logger.ErrorException("Error running pre-scan task", ex);
  113. }
  114. numComplete++;
  115. double percent = numComplete;
  116. percent /= numTasks;
  117. progress.Report(percent * 100);
  118. }
  119. progress.Report(100);
  120. }
  121. }
  122. }