PluginUpdateTask.cs 4.2 KB

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