PeopleValidationTask.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Database.Implementations;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Globalization;
  10. using MediaBrowser.Model.Tasks;
  11. using Microsoft.EntityFrameworkCore;
  12. namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
  13. /// <summary>
  14. /// Class PeopleValidationTask.
  15. /// </summary>
  16. public class PeopleValidationTask : IScheduledTask, IConfigurableScheduledTask
  17. {
  18. private readonly ILibraryManager _libraryManager;
  19. private readonly ILocalizationManager _localization;
  20. private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="PeopleValidationTask" /> class.
  23. /// </summary>
  24. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  25. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  26. /// <param name="dbContextFactory">Instance of the <see cref="IDbContextFactory{TContext}"/> interface.</param>
  27. public PeopleValidationTask(ILibraryManager libraryManager, ILocalizationManager localization, IDbContextFactory<JellyfinDbContext> dbContextFactory)
  28. {
  29. _libraryManager = libraryManager;
  30. _localization = localization;
  31. _dbContextFactory = dbContextFactory;
  32. }
  33. /// <inheritdoc />
  34. public string Name => _localization.GetLocalizedString("TaskRefreshPeople");
  35. /// <inheritdoc />
  36. public string Description => _localization.GetLocalizedString("TaskRefreshPeopleDescription");
  37. /// <inheritdoc />
  38. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  39. /// <inheritdoc />
  40. public string Key => "RefreshPeople";
  41. /// <inheritdoc />
  42. public bool IsHidden => false;
  43. /// <inheritdoc />
  44. public bool IsEnabled => true;
  45. /// <inheritdoc />
  46. public bool IsLogged => true;
  47. /// <summary>
  48. /// Creates the triggers that define when the task will run.
  49. /// </summary>
  50. /// <returns>An <see cref="IEnumerable{TaskTriggerInfo}"/> containing the default trigger infos for this task.</returns>
  51. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  52. {
  53. yield return new TaskTriggerInfo
  54. {
  55. Type = TaskTriggerInfoType.IntervalTrigger,
  56. IntervalTicks = TimeSpan.FromDays(7).Ticks
  57. };
  58. }
  59. /// <inheritdoc />
  60. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  61. {
  62. IProgress<double> subProgress = new Progress<double>((val) => progress.Report(val / 2));
  63. await _libraryManager.ValidatePeopleAsync(subProgress, cancellationToken).ConfigureAwait(false);
  64. subProgress = new Progress<double>((val) => progress.Report((val / 2) + 50));
  65. var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  66. await using (context.ConfigureAwait(false))
  67. {
  68. var dupQuery = context.Peoples
  69. .GroupBy(e => new { e.Name, e.PersonType })
  70. .Where(e => e.Count() > 1)
  71. .Select(e => e.Select(f => f.Id).ToArray());
  72. var total = dupQuery.Count();
  73. const int PartitionSize = 100;
  74. var iterator = 0;
  75. int itemCounter;
  76. var buffer = ArrayPool<Guid[]>.Shared.Rent(PartitionSize)!;
  77. try
  78. {
  79. do
  80. {
  81. itemCounter = 0;
  82. await foreach (var item in dupQuery
  83. .Take(PartitionSize)
  84. .AsAsyncEnumerable()
  85. .WithCancellation(cancellationToken)
  86. .ConfigureAwait(false))
  87. {
  88. buffer[itemCounter++] = item;
  89. }
  90. for (int i = 0; i < itemCounter; i++)
  91. {
  92. var item = buffer[i];
  93. var reference = item[0];
  94. var dups = item[1..];
  95. await context.PeopleBaseItemMap.WhereOneOrMany(dups, e => e.PeopleId)
  96. .ExecuteUpdateAsync(e => e.SetProperty(f => f.PeopleId, reference), cancellationToken)
  97. .ConfigureAwait(false);
  98. await context.Peoples.Where(e => dups.Contains(e.Id)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
  99. subProgress.Report(100f / total * ((iterator * PartitionSize) + i));
  100. }
  101. iterator++;
  102. } while (itemCounter == PartitionSize && !cancellationToken.IsCancellationRequested);
  103. }
  104. finally
  105. {
  106. ArrayPool<Guid[]>.Shared.Return(buffer);
  107. }
  108. subProgress.Report(100);
  109. }
  110. }
  111. }