< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.ScheduledTasks.Tasks.OptimizeDatabaseTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs
Line coverage
42%
Covered lines: 12
Uncovered lines: 16
Coverable lines: 28
Total lines: 96
Line coverage: 42.8%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 3/26/2026 - 12:14:14 AM Line coverage: 45.4% (5/11) Total lines: 824/19/2026 - 12:14:27 AM Line coverage: 45.8% (11/24) Total lines: 826/28/2026 - 12:15:35 AM Line coverage: 42.8% (12/28) Branch coverage: 0% (0/2) Total lines: 96 6/28/2026 - 12:15:35 AM Line coverage: 42.8% (12/28) Branch coverage: 0% (0/2) Total lines: 96

Coverage delta

Coverage delta 3 -3

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%11100%
get_Description()100%210%
get_Category()100%210%
get_Key()100%210%
get_IsHidden()100%210%
get_IsEnabled()100%210%
get_IsLogged()100%210%
GetDefaultTriggers()100%11100%
ExecuteAsync()0%620%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Jellyfin.Database.Implementations;
 6using MediaBrowser.Controller.Library;
 7using MediaBrowser.Model.Globalization;
 8using MediaBrowser.Model.Tasks;
 9using Microsoft.Extensions.Logging;
 10
 11namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
 12
 13/// <summary>
 14/// Optimizes Jellyfin's database by issuing a VACUUM command.
 15/// </summary>
 16public class OptimizeDatabaseTask : IScheduledTask, IConfigurableScheduledTask
 17{
 18    private readonly ILogger<OptimizeDatabaseTask> _logger;
 19    private readonly ILocalizationManager _localization;
 20    private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider;
 21    private readonly ILibraryManager _libraryManager;
 22
 23    /// <summary>
 24    /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
 25    /// </summary>
 26    /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
 27    /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 28    /// <param name="jellyfinDatabaseProvider">Instance of the JellyfinDatabaseProvider that can be used for provider sp
 29    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 30    public OptimizeDatabaseTask(
 31        ILogger<OptimizeDatabaseTask> logger,
 32        ILocalizationManager localization,
 33        IJellyfinDatabaseProvider jellyfinDatabaseProvider,
 34        ILibraryManager libraryManager)
 35    {
 2236        _logger = logger;
 2237        _localization = localization;
 2238        _jellyfinDatabaseProvider = jellyfinDatabaseProvider;
 2239        _libraryManager = libraryManager;
 2240    }
 41
 42    /// <inheritdoc />
 2243    public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
 44
 45    /// <inheritdoc />
 046    public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
 47
 48    /// <inheritdoc />
 049    public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
 50
 51    /// <inheritdoc />
 052    public string Key => "OptimizeDatabaseTask";
 53
 54    /// <inheritdoc />
 055    public bool IsHidden => false;
 56
 57    /// <inheritdoc />
 058    public bool IsEnabled => true;
 59
 60    /// <inheritdoc />
 061    public bool IsLogged => true;
 62
 63    /// <inheritdoc />
 64    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 65    {
 2266        yield return new TaskTriggerInfo
 2267        {
 2268            Type = TaskTriggerInfoType.IntervalTrigger,
 2269            IntervalTicks = TimeSpan.FromHours(6).Ticks
 2270        };
 2271    }
 72
 73    /// <inheritdoc />
 74    public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 75    {
 76        // Vacuuming/checkpointing requires an exclusive lock on the database. Running it while a library scan is in
 77        // progress causes both operations to contend for the database and can stall the scan, so defer optimization
 78        // until no scan is running. The task will run again on its next trigger.
 079        if (_libraryManager.IsScanRunning)
 80        {
 081            _logger.LogInformation("Skipping database optimization because a library scan is currently running.");
 082            return;
 83        }
 84
 085        _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
 86
 87        try
 88        {
 089            await _jellyfinDatabaseProvider.RunScheduledOptimisation(cancellationToken).ConfigureAwait(false);
 090        }
 091        catch (Exception e)
 92        {
 093            _logger.LogError(e, "Error while optimizing jellyfin.db");
 094        }
 095    }
 96}