OptimizeDatabaseTask.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 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 IDbContextFactory<JellyfinDbContext> _provider;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
  22. /// </summary>
  23. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  24. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  25. /// <param name="provider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
  26. public OptimizeDatabaseTask(
  27. ILogger<OptimizeDatabaseTask> logger,
  28. ILocalizationManager localization,
  29. IDbContextFactory<JellyfinDbContext> provider)
  30. {
  31. _logger = logger;
  32. _localization = localization;
  33. _provider = provider;
  34. }
  35. /// <inheritdoc />
  36. public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
  37. /// <inheritdoc />
  38. public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
  39. /// <inheritdoc />
  40. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  41. /// <inheritdoc />
  42. public string Key => "OptimizeDatabaseTask";
  43. /// <inheritdoc />
  44. public bool IsHidden => false;
  45. /// <inheritdoc />
  46. public bool IsEnabled => true;
  47. /// <inheritdoc />
  48. public bool IsLogged => true;
  49. /// <inheritdoc />
  50. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  51. {
  52. return
  53. [
  54. // Every so often
  55. new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  56. ];
  57. }
  58. /// <inheritdoc />
  59. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  60. {
  61. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  62. try
  63. {
  64. var context = await _provider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  65. await using (context.ConfigureAwait(false))
  66. {
  67. if (context.Database.IsSqlite())
  68. {
  69. await context.Database.ExecuteSqlRawAsync("PRAGMA optimize", cancellationToken).ConfigureAwait(false);
  70. await context.Database.ExecuteSqlRawAsync("VACUUM", cancellationToken).ConfigureAwait(false);
  71. _logger.LogInformation("jellyfin.db optimized successfully!");
  72. }
  73. else
  74. {
  75. _logger.LogInformation("This database doesn't support optimization");
  76. }
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. _logger.LogError(e, "Error while optimizing jellyfin.db");
  82. }
  83. }
  84. }
  85. }