PluginUpdateTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Tasks
  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. /// <inheritdoc />
  58. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  59. {
  60. progress.Report(0);
  61. var packageFetchTask = _installationManager.GetAvailablePluginUpdates(cancellationToken);
  62. var packagesToInstall = (await packageFetchTask.ConfigureAwait(false)).ToList();
  63. progress.Report(10);
  64. var numComplete = 0;
  65. foreach (var package in packagesToInstall)
  66. {
  67. cancellationToken.ThrowIfCancellationRequested();
  68. try
  69. {
  70. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  71. }
  72. catch (OperationCanceledException)
  73. {
  74. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  75. if (cancellationToken.IsCancellationRequested)
  76. {
  77. throw;
  78. }
  79. }
  80. catch (HttpRequestException ex)
  81. {
  82. _logger.LogError(ex, "Error downloading {0}", package.Name);
  83. }
  84. catch (IOException ex)
  85. {
  86. _logger.LogError(ex, "Error updating {0}", package.Name);
  87. }
  88. // Update progress
  89. lock (progress)
  90. {
  91. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  92. }
  93. }
  94. progress.Report(100);
  95. }
  96. }
  97. }