OptimizeDatabaseTask.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /// <inheritdoc />
  62. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  63. {
  64. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  65. try
  66. {
  67. using var context = _provider.CreateContext();
  68. if (context.Database.IsSqlite())
  69. {
  70. context.Database.ExecuteSqlRaw("PRAGMA optimize");
  71. context.Database.ExecuteSqlRaw("VACUUM");
  72. _logger.LogInformation("jellyfin.db optimized successfully!");
  73. }
  74. else
  75. {
  76. _logger.LogInformation("This database doesn't support optimization");
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. _logger.LogError(e, "Error while optimizing jellyfin.db");
  82. }
  83. return Task.CompletedTask;
  84. }
  85. }
  86. }