< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Data.CleanDatabaseScheduledTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 84
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Jellyfin.Database.Implementations;
 8using MediaBrowser.Controller;
 9using MediaBrowser.Controller.Entities;
 10using MediaBrowser.Controller.Library;
 11using MediaBrowser.Controller.Trickplay;
 12using Microsoft.EntityFrameworkCore;
 13using Microsoft.Extensions.Logging;
 14
 15namespace Emby.Server.Implementations.Data;
 16
 17public 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    {
 2128        _libraryManager = libraryManager;
 2129        _logger = logger;
 2130        _dbProvider = dbProvider;
 2131    }
 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}