| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Jellyfin.Database.Implementations; |
| | | 6 | | using MediaBrowser.Controller.Library; |
| | | 7 | | using MediaBrowser.Model.Globalization; |
| | | 8 | | using MediaBrowser.Model.Tasks; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | |
| | | 11 | | namespace 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 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 | | { |
| | 22 | 36 | | _logger = logger; |
| | 22 | 37 | | _localization = localization; |
| | 22 | 38 | | _jellyfinDatabaseProvider = jellyfinDatabaseProvider; |
| | 22 | 39 | | _libraryManager = libraryManager; |
| | 22 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 22 | 43 | | public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase"); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 0 | 46 | | public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription"); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 0 | 49 | | public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 0 | 52 | | public string Key => "OptimizeDatabaseTask"; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 0 | 55 | | public bool IsHidden => false; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 0 | 58 | | public bool IsEnabled => true; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 0 | 61 | | public bool IsLogged => true; |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | | 65 | | { |
| | 22 | 66 | | yield return new TaskTriggerInfo |
| | 22 | 67 | | { |
| | 22 | 68 | | Type = TaskTriggerInfoType.IntervalTrigger, |
| | 22 | 69 | | IntervalTicks = TimeSpan.FromHours(6).Ticks |
| | 22 | 70 | | }; |
| | 22 | 71 | | } |
| | | 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. |
| | 0 | 79 | | if (_libraryManager.IsScanRunning) |
| | | 80 | | { |
| | 0 | 81 | | _logger.LogInformation("Skipping database optimization because a library scan is currently running."); |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | _logger.LogInformation("Optimizing and vacuuming jellyfin.db..."); |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 0 | 89 | | await _jellyfinDatabaseProvider.RunScheduledOptimisation(cancellationToken).ConfigureAwait(false); |
| | 0 | 90 | | } |
| | 0 | 91 | | catch (Exception e) |
| | | 92 | | { |
| | 0 | 93 | | _logger.LogError(e, "Error while optimizing jellyfin.db"); |
| | 0 | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | } |