PluginUpdateTask.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 packagesToInstall = await _installationManager.GetAvailablePluginUpdates(cancellationToken)
  52. .ToListAsync(cancellationToken)
  53. .ConfigureAwait(false);
  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("TasksCategoryApplication");
  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. }