Files
jellyfin-jellyfin-1/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs

85 lines
3.0 KiB
C#
Raw Normal View History

2019-11-01 18:38:54 +01:00
#pragma warning disable CS1591
using System;
2024-10-10 14:32:49 +00:00
using System.Linq;
2015-08-26 01:10:04 -04:00
using System.Threading;
using System.Threading.Tasks;
2024-10-10 14:32:49 +00:00
using Jellyfin.Server.Implementations;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2024-10-10 14:32:49 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
2015-08-26 01:10:04 -04:00
2016-11-18 03:39:20 -05:00
namespace Emby.Server.Implementations.Data
2015-08-26 01:10:04 -04:00
{
2017-07-02 14:58:56 -04:00
public class CleanDatabaseScheduledTask : ILibraryPostScanTask
2015-08-26 01:10:04 -04:00
{
private readonly ILibraryManager _libraryManager;
2020-06-05 18:15:56 -06:00
private readonly ILogger<CleanDatabaseScheduledTask> _logger;
2024-10-10 14:32:49 +00:00
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
2015-08-26 01:10:04 -04:00
2024-10-10 14:32:49 +00:00
public CleanDatabaseScheduledTask(
ILibraryManager libraryManager,
ILogger<CleanDatabaseScheduledTask> logger,
IDbContextFactory<JellyfinDbContext> dbProvider)
2015-08-26 01:10:04 -04:00
{
_libraryManager = libraryManager;
_logger = logger;
2024-10-10 14:32:49 +00:00
_dbProvider = dbProvider;
2015-08-26 01:10:04 -04:00
}
2024-11-17 11:03:43 +00:00
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
2015-08-26 01:10:04 -04:00
{
2024-11-17 11:03:43 +00:00
await CleanDeadItems(cancellationToken, progress).ConfigureAwait(false);
2015-08-26 01:10:04 -04:00
}
2024-10-22 10:52:34 +00:00
private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
2015-08-28 00:19:08 -04:00
{
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
{
HasDeadParentId = true
});
var numComplete = 0;
2024-10-10 14:32:49 +00:00
var numItems = itemIds.Count + 1;
2015-08-28 00:19:08 -04:00
_logger.LogDebug("Cleaning {0} items with dead parent links", numItems);
2015-08-28 00:19:08 -04:00
foreach (var itemId in itemIds)
{
cancellationToken.ThrowIfCancellationRequested();
var item = _libraryManager.GetItemById(itemId);
2022-12-05 15:01:13 +01:00
if (item is not null)
2015-08-28 00:19:08 -04:00
{
_logger.LogInformation("Cleaning item {0} type: {1} path: {2}", item.Name, item.GetType().Name, item.Path ?? string.Empty);
2015-08-28 00:19:08 -04:00
2018-09-12 19:26:21 +02:00
_libraryManager.DeleteItem(item, new DeleteOptions
2015-08-28 00:19:08 -04:00
{
DeleteFileLocation = false
2018-09-12 19:26:21 +02:00
});
2015-08-28 00:19:08 -04:00
}
numComplete++;
double percent = numComplete;
percent /= numItems;
progress.Report(percent * 100);
}
2025-01-15 20:12:41 +00:00
var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
await using (context.ConfigureAwait(false))
{
var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
await using (transaction.ConfigureAwait(false))
{
await context.ItemValues.Where(e => e.BaseItemsMap!.Count == 0).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
}
}
2024-10-10 14:32:49 +00:00
2015-08-28 00:19:08 -04:00
progress.Report(100);
}
2015-08-26 01:10:04 -04:00
}
}