| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Database.Implementations; |
| | | 9 | | using Jellyfin.Server.Implementations.Item; |
| | | 10 | | using MediaBrowser.Model.Globalization; |
| | | 11 | | using MediaBrowser.Model.Tasks; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Emby.Server.Implementations.ScheduledTasks.Tasks; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Task to clean up any detached userdata from the database. |
| | | 19 | | /// </summary> |
| | | 20 | | public class CleanupUserDataTask : IScheduledTask |
| | | 21 | | { |
| | | 22 | | private readonly ILocalizationManager _localization; |
| | | 23 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 24 | | private readonly ILogger<CleanupUserDataTask> _logger; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="CleanupUserDataTask"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="localization">The localisation Provider.</param> |
| | | 30 | | /// <param name="dbProvider">The DB context factory.</param> |
| | | 31 | | /// <param name="logger">A logger.</param> |
| | | 32 | | public CleanupUserDataTask(ILocalizationManager localization, IDbContextFactory<JellyfinDbContext> dbProvider, ILogg |
| | | 33 | | { |
| | 21 | 34 | | _localization = localization; |
| | 21 | 35 | | _dbProvider = dbProvider; |
| | 21 | 36 | | _logger = logger; |
| | 21 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 0 | 40 | | public string Name => _localization.GetLocalizedString("CleanupUserDataTask"); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 0 | 43 | | public string Description => _localization.GetLocalizedString("CleanupUserDataTaskDescription"); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 0 | 46 | | public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 0 | 49 | | public string Key => nameof(CleanupUserDataTask); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc/> |
| | | 52 | | public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 53 | | { |
| | | 54 | | const int LimitDays = 90; |
| | | 55 | | var userDataDate = DateTime.UtcNow.AddDays(LimitDays * -1); |
| | | 56 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | | 57 | | await using (dbContext.ConfigureAwait(false)) |
| | | 58 | | { |
| | | 59 | | var detachedUserData = dbContext.UserData.Where(e => e.ItemId == BaseItemRepository.PlaceholderId); |
| | | 60 | | _logger.LogInformation("There are {NoDetached} detached UserData entries.", detachedUserData.Count()); |
| | | 61 | | |
| | | 62 | | detachedUserData = detachedUserData.Where(e => e.RetentionDate < userDataDate); |
| | | 63 | | |
| | | 64 | | _logger.LogInformation("{NoDetached} are older then {Limit} days.", detachedUserData.Count(), LimitDays); |
| | | 65 | | |
| | | 66 | | await detachedUserData.ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | progress.Report(100); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc/> |
| | | 73 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | | 74 | | { |
| | | 75 | | yield break; |
| | | 76 | | } |
| | | 77 | | } |