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.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Updates;
  9. using MediaBrowser.Model.Net;
  10. using MediaBrowser.Model.Tasks;
  11. using Microsoft.Extensions.Logging;
  12. using MediaBrowser.Model.Globalization;
  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. /// <summary>
  33. /// Creates the triggers that define when the task will run.
  34. /// </summary>
  35. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  36. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  37. {
  38. // At startup
  39. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerStartup };
  40. // Every so often
  41. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks };
  42. }
  43. /// <summary>
  44. /// Update installed plugins.
  45. /// </summary>
  46. /// <param name="cancellationToken">The cancellation token.</param>
  47. /// <param name="progress">The progress.</param>
  48. /// <returns><see cref="Task" />.</returns>
  49. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  50. {
  51. progress.Report(0);
  52. var packageFetchTask = _installationManager.GetAvailablePluginUpdates(cancellationToken);
  53. var packagesToInstall = (await packageFetchTask.ConfigureAwait(false)).ToList();
  54. progress.Report(10);
  55. var numComplete = 0;
  56. foreach (var package in packagesToInstall)
  57. {
  58. cancellationToken.ThrowIfCancellationRequested();
  59. try
  60. {
  61. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  62. }
  63. catch (OperationCanceledException)
  64. {
  65. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  66. if (cancellationToken.IsCancellationRequested)
  67. {
  68. throw;
  69. }
  70. }
  71. catch (HttpException ex)
  72. {
  73. _logger.LogError(ex, "Error downloading {0}", package.Name);
  74. }
  75. catch (IOException ex)
  76. {
  77. _logger.LogError(ex, "Error updating {0}", package.Name);
  78. }
  79. // Update progress
  80. lock (progress)
  81. {
  82. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  83. }
  84. }
  85. progress.Report(100);
  86. }
  87. /// <inheritdoc />
  88. public string Name => _localization.GetLocalizedString("TaskUpdatePlugins");
  89. /// <inheritdoc />
  90. public string Description => _localization.GetLocalizedString("TaskUpdatePluginsDescription");
  91. /// <inheritdoc />
  92. public string Category => _localization.GetLocalizedString("TasksApplicationCategory");
  93. /// <inheritdoc />
  94. public string Key => "PluginUpdates";
  95. /// <inheritdoc />
  96. public bool IsHidden => false;
  97. /// <inheritdoc />
  98. public bool IsEnabled => true;
  99. /// <inheritdoc />
  100. public bool IsLogged => true;
  101. }
  102. }