PluginUpdateTask.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. {
  14. /// <summary>
  15. /// Plugin Update Task.
  16. /// </summary>
  17. public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
  18. {
  19. private readonly ILogger<PluginUpdateTask> _logger;
  20. private readonly IInstallationManager _installationManager;
  21. private readonly ILocalizationManager _localization;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  24. /// </summary>
  25. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  26. /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
  27. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  28. public PluginUpdateTask(ILogger<PluginUpdateTask> logger, IInstallationManager installationManager, ILocalizationManager localization)
  29. {
  30. _logger = logger;
  31. _installationManager = installationManager;
  32. _localization = localization;
  33. }
  34. /// <inheritdoc />
  35. public string Name => _localization.GetLocalizedString("TaskUpdatePlugins");
  36. /// <inheritdoc />
  37. public string Description => _localization.GetLocalizedString("TaskUpdatePluginsDescription");
  38. /// <inheritdoc />
  39. public string Category => _localization.GetLocalizedString("TasksApplicationCategory");
  40. /// <inheritdoc />
  41. public string Key => "PluginUpdates";
  42. /// <inheritdoc />
  43. public bool IsHidden => false;
  44. /// <inheritdoc />
  45. public bool IsEnabled => true;
  46. /// <inheritdoc />
  47. public bool IsLogged => true;
  48. /// <inheritdoc />
  49. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  50. {
  51. // At startup
  52. yield return new TaskTriggerInfo { Type = TaskTriggerInfoType.StartupTrigger };
  53. // Every so often
  54. yield return new TaskTriggerInfo { Type = TaskTriggerInfoType.IntervalTrigger, IntervalTicks = TimeSpan.FromHours(24).Ticks };
  55. }
  56. /// <inheritdoc />
  57. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  58. {
  59. progress.Report(0);
  60. var packageFetchTask = _installationManager.GetAvailablePluginUpdates(cancellationToken);
  61. var packagesToInstall = (await packageFetchTask.ConfigureAwait(false)).ToList();
  62. progress.Report(10);
  63. var numComplete = 0;
  64. foreach (var package in packagesToInstall)
  65. {
  66. cancellationToken.ThrowIfCancellationRequested();
  67. try
  68. {
  69. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  70. }
  71. catch (OperationCanceledException)
  72. {
  73. // InstallPackage has its own inner cancellation token, so only throw this if it's ours
  74. if (cancellationToken.IsCancellationRequested)
  75. {
  76. throw;
  77. }
  78. }
  79. catch (HttpRequestException ex)
  80. {
  81. _logger.LogError(ex, "Error downloading {0}", package.Name);
  82. }
  83. catch (IOException ex)
  84. {
  85. _logger.LogError(ex, "Error updating {0}", package.Name);
  86. }
  87. catch (InvalidDataException ex)
  88. {
  89. _logger.LogError(ex, "Error updating {0}", package.Name);
  90. }
  91. // Update progress
  92. lock (progress)
  93. {
  94. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  95. }
  96. }
  97. progress.Report(100);
  98. }
  99. }
  100. }