| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Jellyfin.Server.Implementations; |
| | 6 | | using MediaBrowser.Model.Globalization; |
| | 7 | | using MediaBrowser.Model.Tasks; |
| | 8 | | using Microsoft.EntityFrameworkCore; |
| | 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 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 | | { |
| 22 | 33 | | _logger = logger; |
| 22 | 34 | | _localization = localization; |
| 22 | 35 | | _provider = provider; |
| 22 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc /> |
| 22 | 39 | | public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase"); |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| 0 | 42 | | public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription"); |
| | 43 | |
|
| | 44 | | /// <inheritdoc /> |
| 0 | 45 | | public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| 0 | 48 | | public string Key => "OptimizeDatabaseTask"; |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| 0 | 51 | | public bool IsHidden => false; |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| 0 | 54 | | public bool IsEnabled => true; |
| | 55 | |
|
| | 56 | | /// <inheritdoc /> |
| 0 | 57 | | public bool IsLogged => true; |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | 61 | | { |
| 22 | 62 | | return |
| 22 | 63 | | [ |
| 22 | 64 | | // Every so often |
| 22 | 65 | | new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Tic |
| 22 | 66 | | ]; |
| | 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 | | } |