OptimizeDatabaseTask.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public OptimizeDatabaseTask(
  24. ILogger<OptimizeDatabaseTask> logger,
  25. ILocalizationManager localization,
  26. JellyfinDbProvider provider)
  27. {
  28. _logger = logger;
  29. _localization = localization;
  30. _provider = provider;
  31. }
  32. /// <inheritdoc />
  33. public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
  34. /// <inheritdoc />
  35. public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
  36. /// <inheritdoc />
  37. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  38. /// <inheritdoc />
  39. public string Key => "OptimizeDatabaseTask";
  40. /// <inheritdoc />
  41. public bool IsHidden => false;
  42. /// <inheritdoc />
  43. public bool IsEnabled => true;
  44. /// <inheritdoc />
  45. public bool IsLogged => true;
  46. /// <summary>
  47. /// Creates the triggers that define when the task will run.
  48. /// </summary>
  49. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  50. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  51. {
  52. return new[]
  53. {
  54. // Every so often
  55. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  56. };
  57. }
  58. /// <summary>
  59. /// Returns the task to be executed.
  60. /// </summary>
  61. /// <param name="cancellationToken">The cancellation token.</param>
  62. /// <param name="progress">The progress.</param>
  63. /// <returns>Task.</returns>
  64. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  65. {
  66. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  67. try
  68. {
  69. using var context = _provider.CreateContext();
  70. if (context.Database.IsSqlite())
  71. {
  72. context.Database.ExecuteSqlRaw("PRAGMA optimize");
  73. context.Database.ExecuteSqlRaw("VACUUM");
  74. _logger.LogInformation("jellyfin.db optimized successfully!");
  75. }
  76. else
  77. {
  78. _logger.LogInformation("This database doesn't support optimization");
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. _logger.LogError(e, "Error while optimizing jellyfin.db");
  84. }
  85. return Task.CompletedTask;
  86. }
  87. }
  88. }