PluginUpdateTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Updates;
  9. using MediaBrowser.Model.Globalization;
  10. using MediaBrowser.Model.Tasks;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
  13. /// <summary>
  14. /// Plugin Update Task.
  15. /// </summary>
  16. public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
  17. {
  18. private readonly ILogger<PluginUpdateTask> _logger;
  19. private readonly IInstallationManager _installationManager;
  20. private readonly ILocalizationManager _localization;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  23. /// </summary>
  24. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  25. /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
  26. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  27. public PluginUpdateTask(ILogger<PluginUpdateTask> logger, IInstallationManager installationManager, ILocalizationManager localization)
  28. {
  29. _logger = logger;
  30. _installationManager = installationManager;
  31. _localization = localization;
  32. }
  33. /// <inheritdoc />
  34. public string Name => _localization.GetLocalizedString("TaskUpdatePlugins");
  35. /// <inheritdoc />
  36. public string Description => _localization.GetLocalizedString("TaskUpdatePluginsDescription");
  37. /// <inheritdoc />
  38. public string Category => _localization.GetLocalizedString("TasksApplicationCategory");
  39. /// <inheritdoc />
  40. public string Key => "PluginUpdates";
  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.StartupTrigger
  53. };
  54. yield return new TaskTriggerInfo
  55. {
  56. Type = TaskTriggerInfoType.IntervalTrigger,
  57. IntervalTicks = TimeSpan.FromHours(24).Ticks
  58. };
  59. }
  60. /// <inheritdoc />
  61. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  62. {
  63. progress.Report(0);
  64. var packageFetchTask = _installationManager.GetAvailablePluginUpdates(cancellationToken);
  65. var packagesToInstall = (await packageFetchTask.ConfigureAwait(false)).ToList();
  66. progress.Report(10);
  67. var numComplete = 0;
  68. foreach (var package in packagesToInstall)
  69. {
  70. cancellationToken.ThrowIfCancellationRequested();
  71. try
  72. {
  73. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  74. }
  75. catch (OperationCanceledException)
  76. {
  77. // InstallPackage has its own inner cancellation token, so only throw this if it's ours
  78. if (cancellationToken.IsCancellationRequested)
  79. {
  80. throw;
  81. }
  82. }
  83. catch (HttpRequestException ex)
  84. {
  85. _logger.LogError(ex, "Error downloading {Name}", package.Name);
  86. }
  87. catch (IOException ex)
  88. {
  89. _logger.LogError(ex, "Error updating {Name}", package.Name);
  90. }
  91. catch (InvalidDataException ex)
  92. {
  93. _logger.LogError(ex, "Error updating {Name}", package.Name);
  94. }
  95. // Update progress
  96. lock (progress)
  97. {
  98. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  99. }
  100. }
  101. progress.Report(100);
  102. }
  103. }