PluginUpdateTask.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace Emby.Server.Implementations.ScheduledTasks
  12. {
  13. /// <summary>
  14. /// Plugin Update Task.
  15. /// </summary>
  16. public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
  17. {
  18. /// <summary>
  19. /// The _logger.
  20. /// </summary>
  21. private readonly ILogger _logger;
  22. private readonly IInstallationManager _installationManager;
  23. public PluginUpdateTask(ILogger logger, IInstallationManager installationManager)
  24. {
  25. _logger = logger;
  26. _installationManager = installationManager;
  27. }
  28. /// <summary>
  29. /// Creates the triggers that define when the task will run.
  30. /// </summary>
  31. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  32. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  33. {
  34. // At startup
  35. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerStartup };
  36. // Every so often
  37. yield return new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks };
  38. }
  39. /// <summary>
  40. /// Update installed plugins.
  41. /// </summary>
  42. /// <param name="cancellationToken">The cancellation token.</param>
  43. /// <param name="progress">The progress.</param>
  44. /// <returns><see cref="Task" />.</returns>
  45. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  46. {
  47. progress.Report(0);
  48. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdates(cancellationToken).ConfigureAwait(false)).ToList();
  49. progress.Report(10);
  50. var numComplete = 0;
  51. foreach (var package in packagesToInstall)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. try
  55. {
  56. await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
  57. }
  58. catch (OperationCanceledException)
  59. {
  60. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  61. if (cancellationToken.IsCancellationRequested)
  62. {
  63. throw;
  64. }
  65. }
  66. catch (HttpException ex)
  67. {
  68. _logger.LogError(ex, "Error downloading {0}", package.name);
  69. }
  70. catch (IOException ex)
  71. {
  72. _logger.LogError(ex, "Error updating {0}", package.name);
  73. }
  74. // Update progress
  75. lock (progress)
  76. {
  77. progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
  78. }
  79. }
  80. progress.Report(100);
  81. }
  82. /// <inheritdoc />
  83. public string Name => "Check for plugin updates";
  84. /// <inheritdoc />
  85. public string Description => "Downloads and installs updates for plugins that are configured to update automatically.";
  86. /// <inheritdoc />
  87. public string Category => "Application";
  88. /// <inheritdoc />
  89. public string Key => "PluginUpdates";
  90. /// <inheritdoc />
  91. public bool IsHidden => false;
  92. /// <inheritdoc />
  93. public bool IsEnabled => true;
  94. /// <inheritdoc />
  95. public bool IsLogged => true;
  96. }
  97. }