< 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
62%
Covered lines: 10
Uncovered lines: 6
Coverable lines: 16
Total lines: 97
Line coverage: 62.5%
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%
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%

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.Server.Implementations;
 6using MediaBrowser.Model.Globalization;
 7using MediaBrowser.Model.Tasks;
 8using Microsoft.EntityFrameworkCore;
 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>
 16    public class OptimizeDatabaseTask : IScheduledTask, IConfigurableScheduledTask
 17    {
 18        private readonly ILogger<OptimizeDatabaseTask> _logger;
 19        private readonly ILocalizationManager _localization;
 20        private readonly IDbContextFactory<JellyfinDbContext> _provider;
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
 24        /// </summary>
 25        /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
 26        /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
 27        /// <param name="provider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
 28        public OptimizeDatabaseTask(
 29            ILogger<OptimizeDatabaseTask> logger,
 30            ILocalizationManager localization,
 31            IDbContextFactory<JellyfinDbContext> provider)
 32        {
 2233            _logger = logger;
 2234            _localization = localization;
 2235            _provider = provider;
 2236        }
 37
 38        /// <inheritdoc />
 2239        public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
 40
 41        /// <inheritdoc />
 042        public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
 43
 44        /// <inheritdoc />
 045        public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
 46
 47        /// <inheritdoc />
 048        public string Key => "OptimizeDatabaseTask";
 49
 50        /// <inheritdoc />
 051        public bool IsHidden => false;
 52
 53        /// <inheritdoc />
 054        public bool IsEnabled => true;
 55
 56        /// <inheritdoc />
 057        public bool IsLogged => true;
 58
 59        /// <inheritdoc />
 60        public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 61        {
 2262            return
 2263            [
 2264                // Every so often
 2265                new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Tic
 2266            ];
 67        }
 68
 69        /// <inheritdoc />
 70        public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
 71        {
 72            _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
 73
 74            try
 75            {
 76                var context = await _provider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
 77                await using (context.ConfigureAwait(false))
 78                {
 79                    if (context.Database.IsSqlite())
 80                    {
 81                        await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(f
 82                        await context.Database.ExecuteSqlRawAsync("VACUUM", cancellationToken).ConfigureAwait(false);
 83                        _logger.LogInformation("jellyfin.db optimized successfully!");
 84                    }
 85                    else
 86                    {
 87                        _logger.LogInformation("This database doesn't support optimization");
 88                    }
 89                }
 90            }
 91            catch (Exception e)
 92            {
 93                _logger.LogError(e, "Error while optimizing jellyfin.db");
 94            }
 95        }
 96    }
 97}