PluginUpdateTask.cs 4.0 KB

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