PluginUpdateTask.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Net;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.ScheduledTasks
  13. {
  14. /// <summary>
  15. /// Plugin Update Task
  16. /// </summary>
  17. public class PluginUpdateTask : IScheduledTask
  18. {
  19. /// <summary>
  20. /// The _kernel
  21. /// </summary>
  22. private readonly Kernel _kernel;
  23. /// <summary>
  24. /// The _logger
  25. /// </summary>
  26. private readonly ILogger _logger;
  27. private IInstallationManager _installationManager;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  30. /// </summary>
  31. /// <param name="kernel">The kernel.</param>
  32. /// <param name="logger">The logger.</param>
  33. public PluginUpdateTask(Kernel kernel, ILogger logger, IInstallationManager installationManager)
  34. {
  35. _kernel = kernel;
  36. _logger = logger;
  37. _installationManager = installationManager;
  38. }
  39. /// <summary>
  40. /// Creates the triggers that define when the task will run
  41. /// </summary>
  42. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  43. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  44. {
  45. return new ITaskTrigger[] {
  46. // 1:30am
  47. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1.5) },
  48. new IntervalTrigger { Interval = TimeSpan.FromHours(2)}
  49. };
  50. }
  51. /// <summary>
  52. /// Update installed plugins
  53. /// </summary>
  54. /// <param name="cancellationToken">The cancellation token.</param>
  55. /// <param name="progress">The progress.</param>
  56. /// <returns>Task.</returns>
  57. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  58. {
  59. progress.Report(0);
  60. var packagesToInstall = (await _installationManager.GetAvailablePluginUpdates(true, cancellationToken).ConfigureAwait(false)).ToList();
  61. progress.Report(10);
  62. var numComplete = 0;
  63. // Create tasks for each one
  64. var tasks = packagesToInstall.Select(i => Task.Run(async () =>
  65. {
  66. cancellationToken.ThrowIfCancellationRequested();
  67. try
  68. {
  69. await _installationManager.InstallPackage(i, new Progress<double> { }, cancellationToken).ConfigureAwait(false);
  70. }
  71. catch (OperationCanceledException)
  72. {
  73. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  74. if (cancellationToken.IsCancellationRequested)
  75. {
  76. throw;
  77. }
  78. }
  79. catch (HttpException ex)
  80. {
  81. _logger.ErrorException("Error downloading {0}", ex, i.name);
  82. }
  83. catch (IOException ex)
  84. {
  85. _logger.ErrorException("Error updating {0}", ex, i.name);
  86. }
  87. // Update progress
  88. lock (progress)
  89. {
  90. numComplete++;
  91. double percent = numComplete;
  92. percent /= packagesToInstall.Count;
  93. progress.Report((90 * percent) + 10);
  94. }
  95. }));
  96. cancellationToken.ThrowIfCancellationRequested();
  97. await Task.WhenAll(tasks).ConfigureAwait(false);
  98. progress.Report(100);
  99. }
  100. /// <summary>
  101. /// Gets the name of the task
  102. /// </summary>
  103. /// <value>The name.</value>
  104. public string Name
  105. {
  106. get { return "Check for plugin updates"; }
  107. }
  108. /// <summary>
  109. /// Gets the description.
  110. /// </summary>
  111. /// <value>The description.</value>
  112. public string Description
  113. {
  114. get { return "Downloads and installs updates for plugins that are configured to update automatically."; }
  115. }
  116. public string Category
  117. {
  118. get { return "Application"; }
  119. }
  120. }
  121. }