| | 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.Model.Globalization; |
| | 7 | | using MediaBrowser.Model.Tasks; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | |
|
| | 10 | | namespace Emby.Server.Implementations.ScheduledTasks.Tasks; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Optimizes Jellyfin's database by issuing a VACUUM command. |
| | 14 | | /// </summary> |
| | 15 | | public class OptimizeDatabaseTask : IScheduledTask, IConfigurableScheduledTask |
| | 16 | | { |
| | 17 | | private readonly ILogger<OptimizeDatabaseTask> _logger; |
| | 18 | | private readonly ILocalizationManager _localization; |
| | 19 | | private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param> |
| | 25 | | /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | 26 | | /// <param name="jellyfinDatabaseProvider">Instance of the JellyfinDatabaseProvider that can be used for provider sp |
| | 27 | | public OptimizeDatabaseTask( |
| | 28 | | ILogger<OptimizeDatabaseTask> logger, |
| | 29 | | ILocalizationManager localization, |
| | 30 | | IJellyfinDatabaseProvider jellyfinDatabaseProvider) |
| | 31 | | { |
| 21 | 32 | | _logger = logger; |
| 21 | 33 | | _localization = localization; |
| 21 | 34 | | _jellyfinDatabaseProvider = jellyfinDatabaseProvider; |
| 21 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <inheritdoc /> |
| 21 | 38 | | public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase"); |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| 0 | 41 | | public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription"); |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| 0 | 44 | | public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); |
| | 45 | |
|
| | 46 | | /// <inheritdoc /> |
| 0 | 47 | | public string Key => "OptimizeDatabaseTask"; |
| | 48 | |
|
| | 49 | | /// <inheritdoc /> |
| 0 | 50 | | public bool IsHidden => false; |
| | 51 | |
|
| | 52 | | /// <inheritdoc /> |
| 0 | 53 | | public bool IsEnabled => true; |
| | 54 | |
|
| | 55 | | /// <inheritdoc /> |
| 0 | 56 | | public bool IsLogged => true; |
| | 57 | |
|
| | 58 | | /// <inheritdoc /> |
| | 59 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | 60 | | { |
| | 61 | | yield return new TaskTriggerInfo |
| | 62 | | { |
| | 63 | | Type = TaskTriggerInfoType.IntervalTrigger, |
| | 64 | | IntervalTicks = TimeSpan.FromHours(24).Ticks |
| | 65 | | }; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | /// <inheritdoc /> |
| | 69 | | public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| | 70 | | { |
| | 71 | | _logger.LogInformation("Optimizing and vacuuming jellyfin.db..."); |
| | 72 | |
|
| | 73 | | try |
| | 74 | | { |
| | 75 | | await _jellyfinDatabaseProvider.RunScheduledOptimisation(cancellationToken).ConfigureAwait(false); |
| | 76 | | } |
| | 77 | | catch (Exception e) |
| | 78 | | { |
| | 79 | | _logger.LogError(e, "Error while optimizing jellyfin.db"); |
| | 80 | | } |
| | 81 | | } |
| | 82 | | } |