OptimizeDatabaseTask.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.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. private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="OptimizeDatabaseTask" /> class.
  23. /// </summary>
  24. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  25. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  26. /// <param name="provider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
  27. /// <param name="jellyfinDatabaseProvider">Instance of the JellyfinDatabaseProvider that can be used for provider specific operations.</param>
  28. public OptimizeDatabaseTask(
  29. ILogger<OptimizeDatabaseTask> logger,
  30. ILocalizationManager localization,
  31. IDbContextFactory<JellyfinDbContext> provider,
  32. IJellyfinDatabaseProvider jellyfinDatabaseProvider)
  33. {
  34. _logger = logger;
  35. _localization = localization;
  36. _provider = provider;
  37. _jellyfinDatabaseProvider = jellyfinDatabaseProvider;
  38. }
  39. /// <inheritdoc />
  40. public string Name => _localization.GetLocalizedString("TaskOptimizeDatabase");
  41. /// <inheritdoc />
  42. public string Description => _localization.GetLocalizedString("TaskOptimizeDatabaseDescription");
  43. /// <inheritdoc />
  44. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  45. /// <inheritdoc />
  46. public string Key => "OptimizeDatabaseTask";
  47. /// <inheritdoc />
  48. public bool IsHidden => false;
  49. /// <inheritdoc />
  50. public bool IsEnabled => true;
  51. /// <inheritdoc />
  52. public bool IsLogged => true;
  53. /// <inheritdoc />
  54. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  55. {
  56. return
  57. [
  58. // Every so often
  59. new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromHours(24).Ticks }
  60. ];
  61. }
  62. /// <inheritdoc />
  63. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  64. {
  65. _logger.LogInformation("Optimizing and vacuuming jellyfin.db...");
  66. try
  67. {
  68. await _jellyfinDatabaseProvider.RunScheduledOptimisation(cancellationToken).ConfigureAwait(false);
  69. }
  70. catch (Exception e)
  71. {
  72. _logger.LogError(e, "Error while optimizing jellyfin.db");
  73. }
  74. }
  75. }
  76. }