OptimizeDatabaseTask.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 JellyfinDbProvider _provider;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
  22. /// </summary>
  23. /// <param name="logger">The logger.</param>
  24. /// <param name="localization">The localization manager.</param>
  25. /// <param name="provider">The jellyfin DB context provider.</param>
  26. public OptimizeDatabaseTask(
  27. ILogger<OptimizeDatabaseTask> logger,
  28. ILocalizationManager localization,
  29. JellyfinDbProvider 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. /// <summary>
  50. /// Creates the triggers that define when the task will run.
  51. /// </summary>
  52. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  53. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  54. {
  55. return new[]
  56. {
  57. // Every so often
  58. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  59. };
  60. }
  61. /// <summary>
  62. /// Returns the task to be executed.
  63. /// </summary>
  64. /// <param name="cancellationToken">The cancellation token.</param>
  65. /// <param name="progress">The progress.</param>
  66. /// <returns>Task.</returns>
  67. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  68. {
  69. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  70. try
  71. {
  72. using var context = _provider.CreateContext();
  73. if (context.Database.IsSqlite())
  74. {
  75. context.Database.ExecuteSqlRaw("PRAGMA optimize");
  76. context.Database.ExecuteSqlRaw("VACUUM");
  77. _logger.LogInformation("jellyfin.db optimized successfully!");
  78. }
  79. else
  80. {
  81. _logger.LogInformation("This database doesn't support optimization");
  82. }
  83. }
  84. catch (Exception e)
  85. {
  86. _logger.LogError(e, "Error while optimizing jellyfin.db");
  87. }
  88. return Task.CompletedTask;
  89. }
  90. }
  91. }