OptimizeDatabaseTask.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Database.Implementations;
  6. using MediaBrowser.Model.Globalization;
  7. using MediaBrowser.Model.Tasks;
  8. using Microsoft.Extensions.Logging;
  9. namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
  10. /// <summary>
  11. /// Optimizes Jellyfin's database by issuing a VACUUM command.
  12. /// </summary>
  13. public class OptimizeDatabaseTask : IScheduledTask, IConfigurableScheduledTask
  14. {
  15. private readonly ILogger<OptimizeDatabaseTask> _logger;
  16. private readonly ILocalizationManager _localization;
  17. private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
  20. /// </summary>
  21. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  22. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  23. /// <param name="jellyfinDatabaseProvider">Instance of the JellyfinDatabaseProvider that can be used for provider specific operations.</param>
  24. public OptimizeDatabaseTask(
  25. ILogger<OptimizeDatabaseTask> logger,
  26. ILocalizationManager localization,
  27. IJellyfinDatabaseProvider jellyfinDatabaseProvider)
  28. {
  29. _logger = logger;
  30. _localization = localization;
  31. _jellyfinDatabaseProvider = jellyfinDatabaseProvider;
  32. }
  33. /// <inheritdoc />
  34. public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
  35. /// <inheritdoc />
  36. public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
  37. /// <inheritdoc />
  38. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  39. /// <inheritdoc />
  40. public string Key => "OptimizeDatabaseTask";
  41. /// <inheritdoc />
  42. public bool IsHidden => false;
  43. /// <inheritdoc />
  44. public bool IsEnabled => true;
  45. /// <inheritdoc />
  46. public bool IsLogged => true;
  47. /// <inheritdoc />
  48. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  49. {
  50. yield return new TaskTriggerInfo
  51. {
  52. Type = TaskTriggerInfoType.IntervalTrigger,
  53. IntervalTicks = TimeSpan.FromHours(24).Ticks
  54. };
  55. }
  56. /// <inheritdoc />
  57. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  58. {
  59. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  60. try
  61. {
  62. await _jellyfinDatabaseProvider.RunScheduledOptimisation(cancellationToken).ConfigureAwait(false);
  63. }
  64. catch (Exception e)
  65. {
  66. _logger.LogError(e, "Error while optimizing jellyfin.db");
  67. }
  68. }
  69. }