| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Jellyfin.Database.Implementations; |
| | 8 | | using MediaBrowser.Controller; |
| | 9 | | using MediaBrowser.Controller.Entities; |
| | 10 | | using MediaBrowser.Controller.Library; |
| | 11 | | using MediaBrowser.Controller.Trickplay; |
| | 12 | | using Microsoft.EntityFrameworkCore; |
| | 13 | | using Microsoft.Extensions.Logging; |
| | 14 | |
|
| | 15 | | namespace Emby.Server.Implementations.Data; |
| | 16 | |
|
| | 17 | | public class CleanDatabaseScheduledTask : ILibraryPostScanTask |
| | 18 | | { |
| | 19 | | private readonly ILibraryManager _libraryManager; |
| | 20 | | private readonly ILogger<CleanDatabaseScheduledTask> _logger; |
| | 21 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | 22 | |
|
| | 23 | | public CleanDatabaseScheduledTask( |
| | 24 | | ILibraryManager libraryManager, |
| | 25 | | ILogger<CleanDatabaseScheduledTask> logger, |
| | 26 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | 27 | | { |
| 21 | 28 | | _libraryManager = libraryManager; |
| 21 | 29 | | _logger = logger; |
| 21 | 30 | | _dbProvider = dbProvider; |
| 21 | 31 | | } |
| | 32 | |
|
| | 33 | | public async Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | 34 | | { |
| | 35 | | await CleanDeadItems(cancellationToken, progress).ConfigureAwait(false); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress) |
| | 39 | | { |
| | 40 | | var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 41 | | { |
| | 42 | | HasDeadParentId = true |
| | 43 | | }); |
| | 44 | |
|
| | 45 | | var numComplete = 0; |
| | 46 | | var numItems = itemIds.Count + 1; |
| | 47 | |
|
| | 48 | | _logger.LogDebug("Cleaning {Number} items with dead parent links", numItems); |
| | 49 | |
|
| | 50 | | foreach (var itemId in itemIds) |
| | 51 | | { |
| | 52 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 53 | |
|
| | 54 | | var item = _libraryManager.GetItemById(itemId); |
| | 55 | | if (item is not null) |
| | 56 | | { |
| | 57 | | _logger.LogInformation("Cleaning item {Item} type: {Type} path: {Path}", item.Name, item.GetType().Name, |
| | 58 | |
|
| | 59 | | _libraryManager.DeleteItem(item, new DeleteOptions |
| | 60 | | { |
| | 61 | | DeleteFileLocation = false |
| | 62 | | }); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | numComplete++; |
| | 66 | | double percent = numComplete; |
| | 67 | | percent /= numItems; |
| | 68 | | progress.Report(percent * 100); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 72 | | await using (context.ConfigureAwait(false)) |
| | 73 | | { |
| | 74 | | var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false); |
| | 75 | | await using (transaction.ConfigureAwait(false)) |
| | 76 | | { |
| | 77 | | await context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDeleteAsync(cancellationToken).Co |
| | 78 | | await transaction.CommitAsync(cancellationToken).ConfigureAwait(false); |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| | 82 | | progress.Report(100); |
| | 83 | | } |
| | 84 | | } |