PluginUpdateTask.cs 4.1 KB

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